How to add pagination to a page
March 27, 2023 ยท View on GitHub
- Add to
app/Classes/WebsiteSettings::addFrontendRoutes:
if($indexedPageUrl = $this->urlService->getUrlByPageKey('PAGE_KEY')){
$frontend->add('{url:' . $newsPageUrl . '}/{page:[0-9]+}', KikCMSConfig::NAMESPACE_PATH_CMS_CONTROLLERS . 'Frontend::page')->setName('PAGE_ROUTE_NAME');
}
- Replace
PAGE_KEYwith the key of the page that needs pagination. ReplacePAGE_ROUTE_NAMEwith a desired route name. - Copy to
app/Classes/TemplateVariables.phpin a function for the corresponding template:
$page = $this->dispatcher->getParam('page') ?: 1;
$pageCount = $this->someService->getPageCount();
$itemMap = $this->someService->getMap($page);
$pages = $this->paginateListService->getPageList($pageCount, $page);
return [
'itemMap' => $itemMap,
'pages' => $pages,
'currentPage' => $page,
];
- Replace
someServicewith your own. - In your created service, make the functions to retrieve the paginated content, for example:
/**
* @param int $page
* @return FullPageMap
*/
public function getMap(int $page): FullPageMap
{
// todo: this is an example, modify with your own logic
$query = $this->getNewsBaseQuery()
->limit(Config::ITEMS_PER_PAGE, ($page - 1) * Config::ITEMS_PER_PAGE);
$pageMap = $this->dbService->getObjectMap($query, PageMap::class);
return $this->fullPageService->getByPageMap($pageMap);
}
/**
* @return Builder
*/
public function getBaseQuery(): Builder
{
// todo: write a query
}
/**
* @return int
*/
public function getPageCount(): int
{
$query = $this->getBaseQuery()->columns('COUNT(id)');
return ceil($this->dbService->getValue($query) / Config::ITEMS_PER_PAGE);
}
- Where you make a config variable
ITEMS_PER_PAGEand you replace, and you add your own logic. - In the template, you add this code to add the pages:
<ul class="pagination">
{% for pageIndex in pages %}
{% if pageIndex %}
<li class="{{ pageIndex == currentPage ? 'active' : '' }}">
<a href="{{ urlPath }}/{{ pageIndex }}" data-page="{{ pageIndex }}">{{ pageIndex }}</a>
</li>
{% else %}
<li class="disabled"><a>...</a></li>
{% endif %}
{% endfor %}
</ul>