hellow everyone, I am creating ajax pagination with CI4 Pager library,
but I found it difficult to catch id of the pagination counter. instead of using full URI path like
"localhost/view-user/?page=1", I want to be able to catch only "1" so I can pass it using javascript as post variable
that will allow me to path inside paginate method as parameter like this( paginate(5, 'test', $page = 1)).
How can I do that and make it work with ajax?
//html pagination links
<nav aria-label="<?= lang('Pager.pageNavigation') ?>">
<ul class="pagination">
<?php foreach ($pager->links() as $link) : ?>
<li <?= $link['active'] ? 'class="active"' : '' ?>>
<a href="<?= $link['uri'] ?>">
<?= $link['title'] ?>
</a>
</li>
<?php endforeach ?>
</ul>
</nav>
//dataset like this inside anchor tag "data-page"
where the counter variable it will be an integer
<a href="<?= $link['uri'] ?>" data-page="<?= $counter ?>"></a>
// my controller
public function view(){
if ($this->request->isAJAX()) {
$page = $this->request->getPost('page');
$result = $this->model->getKanda()->paginate(20,'default',$page);
$pager = $this->model->pager;
$output = array();
foreach($result as $row){
$output[] = [
'id' => $row['userid'],
'username' => $row['username'],
];
}
$json = [
'result'=> $output,
'pager' => $pager->links(),
];
return $this->response->setJSON($json);
}
}
my js
$(document).on('click', 'ul.pagination li a', function(e){
e.preventDefault();
let page = $(this).data('page');
viewUser(page);
});
function viewUser(page){
let ajax = $.ajax({
url: 'view-user',
method:'post',
data: { page: page },
dataType: 'json',
});
ajax.done(function(data){
let html = '';
$.each(data.result, function(k, v){
let dataSet = `
data-id='${v.id}'
data-kanda='${v.username}'
`;
html += `<div>${v.username}</div>`;
});
$('.result').html(html);
$('.paginator').html(data.pager);
});
}
question from:
https://stackoverflow.com/questions/65884552/how-to-paginate-with-ajax-using-pager-library-in-codeigniter-4 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…