Reputation: 5395
I'm using a custom pagination template with CakePHP 3.7
The template is in config/infinite-scroll-paginator.php
. I want to use this with the Infinite Scroll jquery plugin. Therefore all that's required is 1 link per paginated page which contains the URL to the "next" page (i.e. next set of results).
So in the file above I have:
$config = [
'nextActive' => '<a class="pagination__next" aria-label="Next" href="{{url}}">{{text}}</a>'
];
In my Controller I start by telling it to use the custom pagination template:
public $helpers = [
'Paginator' => ['templates' => 'infinite-scroll-paginator']
];
Then when I want to paginate my data I use a custom finder (findFilters()
which is in Model/Table/FiltersTable.php
):
public $paginate = [
'finder' => 'filters',
];
I can then use this to get paginated data:
$this->loadModel('Filters');
$rf_keywords = trim($this->request->getQuery('rf_keywords'));
// Pagination
$page = $this->request->getQuery('page') ? (int)$this->request->getQuery('page') : 1;
$this->paginate = ['page' => $page, 'limit' => 100];
$finder = $this
->Filters
->find('filters', [
'rf_keywords' => $rf_keywords
]);
$data = $this->paginate($finder);
$this->set('data', $data);
Then in my template I output the paginator HTML:
<?= $this->Paginator->next(); ?>
All of this works fine. I get a "next" link for each page. Say if I have 10 pages I get URL's:
/get-filters/?page=1
/get-filters/?page=2
...
/get-filters/?page=10
The issue is that on the 10th page there is a "next" link and the URL of it is just
/get-filters
There are no more pages to output but I still get a link, albeit an invalid one.
How can I stop it from outputting the link on the last page? I thought that the paginator component would take care of this since it's aware how many pages there are based on my limit
value in $this->paginate
?
Upvotes: 0
Views: 127
Reputation: 5395
Use $this->Paginator->hasNext()
which returns true or false depending on whether there is a "next page".
In the template:
<?php if ($this->Paginator->hasNext()): ?>
<?= $this->Paginator->next(); ?>
<?php endif; ?>
Upvotes: 0