CodeIgniter Forums
Pagination: Add getTotalRecords and getCurrentPageRecordsTotal - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: CodeIgniter 4 (https://forum.codeigniter.com/forumdisplay.php?fid=28)
+--- Forum: CodeIgniter 4 Feature Requests (https://forum.codeigniter.com/forumdisplay.php?fid=29)
+--- Thread: Pagination: Add getTotalRecords and getCurrentPageRecordsTotal (/showthread.php?tid=88174)



Pagination: Add getTotalRecords and getCurrentPageRecordsTotal - Tanveer - 08-02-2023

Hello,

This is my first post

So as we are not getting the total records and current page records in pager library to the template from pagerRenderer.php
I can workaround for total records by adding this to pagerRenderer.php :
Code:
public function getTotalRecordsCount(): int
    {
        return $this->total;
    }
but for current page records count i didn't found any solution
 
please add them so we can show bottom of the table the counts of records 

I also want to ask if there is way list S.No. (numbers) like we do within foreach loop as $i=1 and in the $i++;
sorry if there is already a solution


RE: Pagination: Add getTotalRecords and getCurrentPageRecordsTotal - kenjis - 08-02-2023

$pager->getTotal() and $pager->getCurrentPage() ?


RE: Pagination: Add getTotalRecords and getCurrentPageRecordsTotal - Tanveer - 08-03-2023

(08-02-2023, 11:25 PM)kenjis Wrote: $pager->getTotal() and $pager->getCurrentPage() ?

doesn't work as I am talking about to use them on pager template

and we need $pager->getCurrentPageRecords(); not $pager->getCurrentPage();


RE: Pagination: Add getTotalRecords and getCurrentPageRecordsTotal - ozornick - 08-03-2023

kenjis, we are talking about PageRenderer and not Pager. Rendering really does not contain methods. Only properties

Code:
CodeIgniter\Pager\PagerRenderer {#156 ▼
  #first: 1
  #last: 5
  #current: 3
  #total: 10
  #pageCount: 5
  #uri: CodeIgniter\HTTP\URI {#137 ▶}
  #segment: 0
  #pageSelector: "page"
}



RE: Pagination: Add getTotalRecords and getCurrentPageRecordsTotal - InsiteFX - 08-03-2023

This is how I ended up doing it in my Blog.
PHP Code:
<?php
/**
 * app\Views\Pager\bs_simple.php - Bootstrap 5.3.1 Pager Template.
 * @var \CodeIgniter\Pager\PagerRenderer $pager
 */

$pager->setSurroundCount(0);
?>
<nav aria-label="Page Results">
    <ul class="pager pagination justify-content-center">
        <li <?= $pager->hasPrevious() ? 'class="page-item active"' 'class="page-item disabled"' ?>>
            <a class="page-link" href="<?= $pager->getPrevious() ?? '#' ?>" aria-label="<?= lang('Pager.previous'?>">
                <span aria-hidden="true"><?= lang('Pager.newer'?></span>
            </a>
        </li>
        <li <?= $pager->hasNext() ? 'class="page-item active"' 'class="page-item disabled"' ?>>
            <a class="page-link" href="<?= $pager->getnext() ?? '#' ?>" aria-label="<?= lang('Pager.next'?>">
                <span aria-hidden="true"><?= lang('Pager.older'?></span>
            </a>
        </li>
    </ul>
</nav>

---------------------------------------------

<?php

/**
 * app\Views\Pager\bs_full.php - - Bootstrap 5.3.1 Pager Template.
 * @var \CodeIgniter\Pager\PagerRenderer $pager
 */

$pager->setSurroundCount(2);
?>

<nav aria-label="<?= lang('Pager.pageNavigation'?>">
    <ul class="pager pagination justify-content-center">
        <?php if ($pager->hasPreviousPage()) : ?>
            <li class="page-item">
                <a class="page-link" href="<?= $pager->getFirst() ?>" aria-label="<?= lang('Pager.first'?>">
                    <span aria-hidden="true"><?= lang('Pager.first'?></span>
                </a>
            </li>
            <li class="page-item">
                <a class="page-link" href="<?= $pager->getPreviousPage() ?>" aria-label="<?= lang('Pager.previous'?>">
                    <span aria-hidden="true"><?= lang('Pager.previous'?></span>
                </a>
            </li>
        <?php endif ?>

        <?php foreach ($pager->links() as $link) : ?>
            <li <?= $link['active']  'class="page-item active"' '' ?>>
                <a class="page-link" href="<?= $link['uri'?>">
                    <?= $link['title'?>
                </a>
            </li>
        <?php endforeach ?>

        <?php if ($pager->hasNextPage()) : ?>
            <li class="page-item">
                <a class="page-link" href="<?= $pager->getNextPage() ?>" aria-label="<?= lang('Pager.next'?>">
                    <span aria-hidden="true"><?= lang('Pager.next'?></span>
                </a>
            </li>
            <li class="page-item">
                <a class="page-link" href="<?= $pager->getLast() ?>" aria-label="<?= lang('Pager.last'?>">
                    <span aria-hidden="true"><?= lang('Pager.last'?></span>
                </a>
            </li>
        <?php endif ?>
    </ul>
</nav>


---------------------------------------------------

// Add to your Controller.
$data = [
    'pager'      => $posts->pager,
    'currentPage' => $posts->pager->getCurrentPage('group1'),
    'totalPages'  => $posts->pager->getPageCount('group1'),
];

---------------------------------------------------

<?php
/**
 * app\Views\Pagination.php
 * 
 * I use this in latouts, so do what you need.
 * 
 * @var TYPE_NAME $currentPage
 * @var TYPE_NAME $totalPages
 */
?>

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

    <!-- Bootstrap 5.3.1 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;
        // $currentPage and $totalPages are passed in through $data.
        <button type="button" class="btn btn-light"><?= 'Page ' $currentPage ' of ' $totalPages?></button>
    </div>
</div>

----------------------------------------- 



RE: Pagination: Add getTotalRecords and getCurrentPageRecordsTotal - ozornick - 08-04-2023

We understand that. Fine. Try in bs_simple.php output the total number and the current page. Hm?


RE: Pagination: Add getTotalRecords and getCurrentPageRecordsTotal - InsiteFX - 08-04-2023

Well if you follow my code you will see that it does work.


RE: Pagination: Add getTotalRecords and getCurrentPageRecordsTotal - ozornick - 08-04-2023

I'll repeat it too: You pass values through the controller, but the render does not contain them
PHP Code:
// Add to your Controller.

$data = [
    'pager'      => $posts->pager,
    'currentPage' => $posts->pager->getCurrentPage('group1'),
    'totalPages'  => $posts->pager->getPageCount('group1'),
]; 



RE: Pagination: Add getTotalRecords and getCurrentPageRecordsTotal - InsiteFX - 08-05-2023

If you open up system\Pager you will see what it does.

PHP Code:
public function getTotal(string $group 'default'): int

public function getPageCount(string $group 'default'): int

public function getCurrentPage(string $group 'default'): int

public function getPerPage(string $group 'default'): int

  
/**
    * Returns an array with details about the results, including
    * total, per_page, current_page, last_page, next_url, prev_url, from, to.
    * Does not include the actual data. This data is suitable for adding
    * a 'data' object to with the result set and converting to JSON.
    */
    public function getDetails(string $group 'default'): array 



RE: Pagination: Add getTotalRecords and getCurrentPageRecordsTotal - ozornick - 08-06-2023

No, no, no. Read post. I know how to work with Pager myself. The issue affects CodeIgniter\Pager\PagerRenderer
PHP Code:
<?php
/**
* app\Views\Pager\bs_simple.php - Bootstrap 5.3.1 Pager Template.
* @var \CodeIgniter\Pager\PagerRenderer $pager
*/

$pager->setSurroundCount(0);

/** Get $pager->getTotal() here. Not Pager! */
?>