3 Commits
0.1.7 ... 0.1.9

Author SHA1 Message Date
647fe34d11 pagination 2024-07-31 14:51:39 +03:00
1714afcf3f pagination 2024-07-31 14:26:26 +03:00
d6d703aaa6 meta at hooks 2024-07-31 12:48:26 +03:00
5 changed files with 53 additions and 9 deletions

18
composer.lock generated Normal file
View File

@ -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"
}

View File

@ -6,6 +6,10 @@
"description": "Описание 1", "description": "Описание 1",
"description2": "Описание 2" "description2": "Описание 2"
}, },
"pagination": true,
"perPage": "5",
"currentPage": "1",
"total": 10,
"params": {"class": "table table-bordered", "border": "1"} "params": {"class": "table table-bordered", "border": "1"}
}, },
"data": [ "data": [
@ -17,6 +21,7 @@
{"email":"fas6@mail.ru","description":"fafddfgdhvgdfs","description2":"ffdghdas"}, {"email":"fas6@mail.ru","description":"fafddfgdhvgdfs","description2":"ffdghdas"},
{"email":"fas7@mail.ru","description":"fafdfgnfdgdfs","description2":"ffdghdas"}, {"email":"fas7@mail.ru","description":"fafdfgnfdgdfs","description2":"ffdghdas"},
{"email":"fas8@mail.ru","description":"fafdfghdfgdfs","description2":"ffdghdas"}, {"email":"fas8@mail.ru","description":"fafdfghdfgdfs","description2":"ffdghdas"},
{"email":"dfdfd@mail.ru","description":"sdffhdfhggsdfg","description2":"ffdgdfgsdfghdas"} {"email":"dfdfd@mail.ru","description":"sdffhdfhggsdfg","description2":"ffdgdfgsdfghdas"},
{"email":"dfsdfsddfd@mail.ru","description":"sdffhdfsdfshggsdfg","description2":"ffdgdsdffgsdfghdas"}
] ]
} }

View File

@ -9,7 +9,7 @@ $table = new ListJsonTable($json);
$table->setBeforePrintCell(function ($key, $data) { $table->setBeforePrintCell(function ($key, $data) {
return $key == "email" ? "<span style='color: aqua'>$data</span>" : $data; return $key == "email" ? "<span style='color: aqua'>$data</span>" : $data;
}); });
$table->afterPrint(function () { $table->afterPrint(function ($meta) {
return "<div>After Print</div>"; return "<div>After Print</div>";
}); });
$table->create(); $table->create();

View File

@ -23,6 +23,8 @@ class ListJsonTable
private string $baseUrl; private string $baseUrl;
private array $data; private array $data;
private bool $pagination = true;
private array $actionsArray = []; private array $actionsArray = [];
private array $customActionsArray = []; private array $customActionsArray = [];
@ -32,6 +34,7 @@ class ListJsonTable
$this->json = $json; $this->json = $json;
$this->data = json_decode($this->json, true); $this->data = json_decode($this->json, true);
$this->baseUrl = $this->data['meta']['baseUrl'] ?? ''; $this->baseUrl = $this->data['meta']['baseUrl'] ?? '';
$this->pagination = $this->data['meta']['pagination'] ?? true;
$this->beforePrintHook = function () { $this->beforePrintHook = function () {
}; };
$this->afterPrintHook = function () { $this->afterPrintHook = function () {
@ -42,7 +45,7 @@ class ListJsonTable
{ {
$paramsStr = $this->createParams($this->data['meta']['params']); $paramsStr = $this->createParams($this->data['meta']['params']);
$hookBefore = $this->beforePrintHook; $hookBefore = $this->beforePrintHook;
$this->html .= $hookBefore(); $this->html .= $hookBefore($this->data['meta']);
$this->html .= "<table $paramsStr>"; $this->html .= "<table $paramsStr>";
} }
@ -80,7 +83,8 @@ class ListJsonTable
foreach ($this->data['data'] as $col) { foreach ($this->data['data'] as $col) {
$this->html .= "<tr>"; $this->html .= "<tr>";
$this->count += 1; $this->count += 1;
$this->html .= '<td><a href=' . $this->baseUrl . "/" . $col["id"] . '>' . $this->count . '</a></td>'; $id = $col["id"] ?? $this->count;
$this->html .= '<td><a href=' . $this->baseUrl . "/" . $id . '>' . $id . '</a></td>';
foreach ($col as $key => $row) { foreach ($col as $key => $row) {
if ($this->issetColumn($key) and $this->is_fillable($key)) { if ($this->issetColumn($key) and $this->is_fillable($key)) {
if ($this->beforePrintCell) { if ($this->beforePrintCell) {
@ -192,7 +196,18 @@ class ListJsonTable
{ {
$this->html .= "</table>"; $this->html .= "</table>";
$hookAfter = $this->afterPrintHook; $hookAfter = $this->afterPrintHook;
$this->html .= $hookAfter(); $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
);
$pagination->create();
$this->html .= $pagination->fetch();
}
} }
public function render(): void public function render(): void

View File

@ -15,16 +15,17 @@ class Pagination
private string $baseUrl; 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->countItem = $countItem;
$this->perPage = $perPage; $this->perPage = $perPage;
$this->currentPage = $currentPage; $this->currentPage = $currentPage;
$this->baseUrl = $baseUrl; $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; $prev = $this->currentPage - 1 >= 1 ? $this->currentPage - 1 : null;
$next = $this->currentPage + 1 <= $this->countPages ? $this->currentPage + 1 : null; $next = $this->currentPage + 1 <= $this->countPages ? $this->currentPage + 1 : null;
@ -42,12 +43,17 @@ class Pagination
echo $this->html; echo $this->html;
} }
public function fetch() public function fetch(): string
{ {
return $this->html; return $this->html;
} }
private function getTemplate() public function hookBefore(): void
{
}
private function getTemplate(): string
{ {
return '<nav aria-label="Page navigation example"> return '<nav aria-label="Page navigation example">
<ul class="pagination"> <ul class="pagination">