Silverstripe PaginatedList - How to display record numbers on current page?

642 views Asked by At

In silverstripe PaginatedList we can display Page numbers like 'Showing 1 of 5 Pages'. How to display record number on each page for example "Showing 1-10 record of 100"

2

There are 2 answers

6
wmk On BEST ANSWER

You already have a PaginatedList. Using getTotalItems() you can get the total number of items.

In your Template it's $TotalItems when you're inside the list's scope.

edit: $PageStart gives you the start of the current page, $PageLength the amount of items on the current page, so you can calculate the end-number of the current items.

you can do this with an Extension for PaginatedList, e.g.

class PaginatedListEndNumberExtension extends Extension {

    public function getPageEnd() {
        return $this->owner->getPageStart() + $this->owner->getPageLength()
    }
}

and in your /mysite/_config/config.yml

PaginatedList:
  extensions:
    - PaginatedListEndNumberExtension

So in your template with $PageEnd you get the end number of the current page.

Code untested but should get you started. You'll have to check if the end number is greater the number of total items, but i'll leave this exercise up to you ;)

0
Ruth Taylor On

In case anyone comes across this recently - This is default functionality since at least Silverstripe 3. No need for any extensions. API Docs: https://docs.silverstripe.org/en/4/developer_guides/templates/how_tos/pagination/

Two variables you need:

$FirstItem
$LastItem

e.g.

Showing $Results.FirstItem - $Results.LastItem of $Results.TotalItems