Welcome Guest, Not a member yet? Register   Sign In
Pagination using ajax in codeigniter 4
#7

(This post was last modified: 06-08-2021, 07:29 PM by coderscvoen. Edit Reason: adding additional information )

(06-08-2021, 01:05 AM)InsiteFX Wrote: You can do it, it has methods for getting that data see the below blog method $data
PHP Code:
/**
 * -------------------------------------------------------------------
 * posts ()
 * -------------------------------------------------------------------
 *
 *
 */
 
public function posts()
 {
 
$pager Services::pager();

 
$posts      = new PostModel($this->request);
 
$categories = new CategoryModel();

 
$data = [
 
'posts'      => $posts->getLivePosts()->paginate(3'group1'),
 
'pager'      => $posts->pager,
 
'currentPage' => $posts->pager->getCurrentPage('group1'),
 
'totalPages'  => $posts->pager->getPageCount('group1'),
 
'categories'  => $categories->getTopCategories(),
 
'title'      => 'Blog Home',
 
'pageHeading' => 'Blog',
 
'subHeading'  => 'Home',
 
'typography'  => Services::typography(),
 ];

 
// Make all variables global to all views. (Empty PHP File)
 
echo view('Insitefx\Blog\Views\view_data'$data);
 echo 
view('Insitefx\Blog\Views\posts\index');
 } 
I use custom Bootstrap Pagers for this which you can find the code in the forums here.

Here is the partial view code I use layouts.

PHP Code:
<!-- Pagination -->
<
div class="pagination justify-content-center mb-4">
 <?
php if ( ! empty($pager)) :
    //echo $pager->simpleLinks('group1', 'bs_simple');
    echo $pager->links('group1''bs_full');
 endif 
?>

 <!-- Bootstrap 4.5.2 code to show page 1 of 4 total pages using a button. -->
 <div class="btn-group pagination justify-content-center mb-4" role="group" aria-label="pager counts">
 &nbsp;&nbsp;&nbsp;
 <button type="button" class="btn btn-light"><?= 'Page '.$currentPage.' of '.$totalPages?></button>
 </div>
</div> 
hope that helps.

Thanks for this; especially the getCurrentPage() and getPageCount(). I also looked up the Pager.php class and found the getTotal() which displays the total of all records. I noticed that when i click on paginate link for example, to go from page 1 to page 2, values which were on page 1 are lost. How do i maintain those values? I was thinking of using session or something like that.
Reply


Messages In This Thread
RE: Pagination using ajax in codeigniter 4 - by coderscvoen - 06-08-2021, 10:58 AM



Theme © iAndrew 2016 - Forum software by © MyBB