From 1714afcf3f2d62b99331497279814abbd1a41da8 Mon Sep 17 00:00:00 2001 From: stasbilay02 Date: Wed, 31 Jul 2024 14:26:26 +0300 Subject: [PATCH] pagination --- composer.lock | 18 ++++++++++++++++++ examples/simple.json | 4 ++++ src/ListJsonTable.php | 5 +++++ src/Pagination.php | 20 +++++++++++++------- 4 files changed, 40 insertions(+), 7 deletions(-) create mode 100644 composer.lock diff --git a/composer.lock b/composer.lock new file mode 100644 index 0000000..ee5b0d9 --- /dev/null +++ b/composer.lock @@ -0,0 +1,18 @@ +{ + "_readme": [ + "This file locks the dependencies of your project to a known state", + "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", + "This file is @generated automatically" + ], + "content-hash": "76e2cbfc39ced89c5f5aa46d0b1474c7", + "packages": [], + "packages-dev": [], + "aliases": [], + "minimum-stability": "dev", + "stability-flags": [], + "prefer-stable": false, + "prefer-lowest": false, + "platform": [], + "platform-dev": [], + "plugin-api-version": "2.6.0" +} diff --git a/examples/simple.json b/examples/simple.json index 8e02240..aede094 100644 --- a/examples/simple.json +++ b/examples/simple.json @@ -8,6 +8,10 @@ }, "params": {"class": "table table-bordered", "border": "1"} }, + "paginationParams": { + "perPage": "5", + "currentPage": "1" + }, "data": [ {"email":"fas1@mail.ru","description":"sdgsdfg","description2":"ffdghdas"}, {"email":"fas2@mail.ru","description":"fafdgdfgsdfdfs","description2":"ffdghdas"}, diff --git a/src/ListJsonTable.php b/src/ListJsonTable.php index 2102b5c..8189eb5 100644 --- a/src/ListJsonTable.php +++ b/src/ListJsonTable.php @@ -191,6 +191,11 @@ class ListJsonTable public function endTable(): void { $this->html .= ""; +// $pagination = new Pagination($this->data['meta']['total'], $this->data['meta']['perPage'], $this->data['meta']['currentPage'], $this->baseUrl); + $pagination = new Pagination(count($this->data['data']), $this->data['paginationParams'], $this->baseUrl); + var_dump(count($this->data['data'])); + $pagination->create(); + $this->html .= $pagination->fetch(); $hookAfter = $this->afterPrintHook; $this->html .= $hookAfter($this->data['meta']); } diff --git a/src/Pagination.php b/src/Pagination.php index 84fe770..2c2a372 100644 --- a/src/Pagination.php +++ b/src/Pagination.php @@ -14,17 +14,18 @@ class Pagination private string $baseUrl; - public function __construct($countItem, $perPage, $currentPage, $baseUrl) +// public function __construct($countItem, $perPage, $currentPage, $baseUrl) + public function __construct(int $countItem, array $params, string $baseUrl) { $this->countItem = $countItem; - $this->perPage = $perPage; - $this->currentPage = $currentPage; + $this->perPage = $params['perPage']; + $this->currentPage = $params['currentPage']; $this->baseUrl = $baseUrl; - $this->countPages = ceil($this->countItem / $perPage); + $this->countPages = ceil($this->countItem / $this->perPage); } - public function create() + public function create(): void { $prev = $this->currentPage - 1 >= 1 ? $this->currentPage - 1 : null; $next = $this->currentPage + 1 <= $this->countPages ? $this->currentPage + 1 : null; @@ -42,12 +43,17 @@ class Pagination echo $this->html; } - public function fetch() + public function fetch(): string { return $this->html; } - private function getTemplate() + public function hookBefore(): void + { + + } + + private function getTemplate(): string { return '