Pagination
The rows fetched from the database according to the submitted query can sometimes be very numerous, and displaying all of them on a single page causes clutter. To improve performance and UX, the rows should be displayed with pagination. To paginate the rows, the packages\base\views\Listview class has been created in the framework.
To use the pagination methods, the view class must either inherit from the packages\base\views\Listview class or use the packages\base\views\traits\ListTrait trait in the class.
By inheriting from the Listview class, when responding to API and JSON requests, the data is automatically converted to JSON and returned together with the information needed for pagination.
Example: Inheriting from the class
<?php
namespace themes\themename\views\packagename\users;
use packages\base\views\Listview;
class UsersList extends Listview {
}
Example: Using the trait
<?php
namespace themes\themename\views\packagename\users;
use packages\base\View;
use packages\base\views\traits\ListTrait;
class Search extends View {
use ListTrait;
}
To paginate the rows, you must specify the data, the number of rows allowed to be displayed, the total count of rows, and the page number.
Specifying the rows
The rows you intend to display are passed to the view using the setDataList method.
Pagination settings
The pagination settings, which include the number of rows displayed on the current page, the page number, and the total count of rows, are configured using the setPaginate method.
The method receives three input arguments: the first argument is the page number, the second is the total count of rows, and the third is the number of rows displayed on the current page.
Example: Example controller file
<?php
namespace packages\packagename\controllers;
use packages\packagename\User as Model;
use packages\base\{Controller, Response, View};
use themes\themename\views\packagename\users as views;
class Users extends Controller {
public function search(): Response {
$view = view::byName(views\Search::class);
$this->response->setView($view);
$inputs = $this->checkinputs(array(
"page" => array(
"type" => "number",
"optional" => true,
"default" => 1,
),
"ipp" => array(
"type" => "number",
"optional" => true,
"default" => 25,
),
));
if ($inputs["page"] < 1) {
$inputs["page"] = 1;
}
if ($inputs["ipp"] < 1) {
$inputs["ipp"] = 1;
}
if ($inputs["ipp"] > 100) {
$inputs["ipp"] = 100;
}
$model = new Model();
$model->pageLimit = $inputs["ipp"];
$users = $model->paginate($inputs["page"]);
$view->setDataList($users);
$view->setPaginate($inputs["page"], $user->totalCount, $inputs["ipp"]);
$this->response->setStatus(true);
return $this->response;
}
}
?>
For more information, refer to the Database page.
Retrieving the rows
After pagination is performed, the rows are retrieved using the getDataList method.
Example: Example view code
namespace themes\themename\views\packagename\users;
use packages\base\views\ListView;
class Search extends ListView {
public function __beforeLoad() {
$this->setTitle(t("users.search"));
}
public function getUsers(): array {
return $this->getDataList(); // Or you can use $this->getDataList() directly in html file
}
}
Pagination in the template
To display all rows, the user must be given a way to select the next pages or even the previous pages. The code below is an example of generating page numbers in HTML.
<table class="table table-striped">
<head>
<th>#</th>
<th>Name</th>
<th>Last name</th>
<th>Email</th>
<th>Mobile</th>
</head>
<tbody>
<?php foreach($this->getDataList() as $user) { ?>
<tr>
<td><?php echo $user->id; ?></td>
<td><?php echo $user->name; ?></td>
<td><?php echo $user->lastname; ?></td>
<td><?php echo $user->email; ?></td>
<td><?php echo $user->cellphone; ?></td>
</tr>
<?php } ?>
</tbody>
</table>
<nav>
<ul class="pagination">
<?php
$urlData = Http::$request['get'];
for ($i = 1; $i <= $this->totalPages; $i++) {
$urlData["page"] = $i;
?>
<li class="page-item<?php echo ($i == $this->currentPage) ? ' active' : ''; ?>">
<a class="page-link" href="?<?php echo http_build_query($urlData); ?>"><?php echo $i; ?></a>
</li>
<?php } ?>
</ul>
</nav>
Using the above approach to build the pagination template is very time-consuming and produces a large amount of code. For convenience, you can use the paginator method of the userpanel package.
To call the paginator method, the themes\clipone\views\ListTrait trait must be used in the view class.
Currently, the userpanel package uses Bootstrap version 3, and the appearance generated for the pagination is based on Bootstrap version 3.
Example: Example view file
<?php
namespace themes\themename\views\packagename\users;
use packages\base\views\ListView;
use themes\clipone\views\ListTrait;
class Search extends ListView {
use ListTrait;
}
<?php
namespace themes\themename\views\packagename\users;
use packages\base\{View, views\traits\ListTrait};
use themes\clipone\views\ListTrait as UserpanelListTrait;
class Search extends View {
use ListTrait, UserpanelListTrait;
}
Example: Example HTML file
<table class="table table-striped">
<head>
<th>#</th>
<th>Name</th>
<th>Last name</th>
<th>Email</th>
<th>Mobile</th>
</head>
<tbody>
<?php foreach($this->getDataList() as $user) { ?>
<tr>
<td><?php echo $user->id; ?></td>
<td><?php echo $user->name; ?></td>
<td><?php echo $user->lastname; ?></td>
<td><?php echo $user->email; ?></td>
<td><?php echo $user->cellphone; ?></td>
</tr>
<?php } ?>
</tbody>
</table>
<?php $this->paginator(); ?>