pagination fix

This commit is contained in:
Билай Станислав 2024-07-31 15:49:40 +03:00
parent 647fe34d11
commit f5f6504545
3 changed files with 33 additions and 12 deletions

View File

@ -10,6 +10,7 @@
"perPage": "5",
"currentPage": "1",
"total": 10,
"prefix": "/page",
"params": {"class": "table table-bordered", "border": "1"}
},
"data": [

View File

@ -2,6 +2,7 @@
namespace Itguild\Tables;
use Exception;
use Itguild\Tables\ActionColumn\ActionColumn;
use Itguild\Tables\ActionColumn\DeleteActionColumn;
use Itguild\Tables\ActionColumn\EditActionColumn;
@ -170,6 +171,9 @@ class ListJsonTable
return $actions;
}
/**
* @throws Exception
*/
public function create(): void
{
$this->setActions();
@ -192,6 +196,9 @@ class ListJsonTable
}
}
/**
* @throws Exception
*/
public function endTable(): void
{
$this->html .= "</table>";
@ -199,12 +206,14 @@ class ListJsonTable
$this->html .= $hookAfter($this->data['meta']);
if ($this->pagination){
$pagination = new Pagination(
$this->data['meta']['total'],
$this->data['meta']['perPage'],
$this->data['meta']['currentPage'],
$this->baseUrl
);
$options = [
'countItem' => $this->data['meta']['total'],
'perPage' => $this->data['meta']['perPage'],
'currentPage' => $this->data['meta']['currentPage'],
'baseUrl' => $this->baseUrl,
'prefix' => $this->data['meta']['prefix'],
];
$pagination = new Pagination($options);
$pagination->create();
$this->html .= $pagination->fetch();
}

View File

@ -3,6 +3,9 @@
namespace Itguild\Tables;
use Exception;
use http\Message;
class Pagination
{
@ -14,13 +17,21 @@ class Pagination
private string $baseUrl;
public function __construct($countItem, $perPage, $currentPage, $baseUrl)
// public function __construct(int $countItem, array $params, string $baseUrl)
// public function __construct($countItem, $perPage, $currentPage, $baseUrl)
/**
* @throws Exception
*/
public function __construct(array $options,)
{
$this->countItem = $countItem;
$this->perPage = $perPage;
$this->currentPage = $currentPage;
$this->baseUrl = $baseUrl;
if (!$options['countItem']) {
throw new Exception(message: "countItem is not valid");
}
$this->countItem = $options['countItem'];
$this->perPage = $options['perPage'] ?? 10;
$this->currentPage = $options['currentPage'] ?? 1;
$this->baseUrl = $options['baseUrl'];
$this->baseUrl .= $options['prefix'] ?? '/page';
$this->countPages = ceil($this->countItem / $this->perPage);
}