diff --git a/examples/simple.json b/examples/simple.json index 92e9d01..ce30e9c 100644 --- a/examples/simple.json +++ b/examples/simple.json @@ -6,23 +6,28 @@ "description": "Описание 1", "description2": "Описание 2" }, + "actions": ["view"], "pagination": true, "perPage": "5", "currentPage": "1", + "showActionColumn": true, "total": 10, - "prefix": "/page", + "paginationPrefix": "/page", "params": {"class": "table table-bordered", "border": "1"} }, + "filters": [ + "id", "email" + ], "data": [ - {"email":"fas1@mail.ru","description":"sdgsdfg","description2":"ffdghdas"}, - {"email":"fas2@mail.ru","description":"fafdgdfgsdfdfs","description2":"ffdghdas"}, - {"email":"fas3@mail.ru","description":"fafdgdssdfgdfs","description2":"ffdghdas"}, - {"email":"fas4@mail.ru","description":"fafddsfgsdvcbgdfs","description2":"ffdghdas"}, - {"email":"fas5@mail.ru","description":"fafdgghjgfdfs","description2":"ffdghdas"}, - {"email":"fas6@mail.ru","description":"fafddfgdhvgdfs","description2":"ffdghdas"}, - {"email":"fas7@mail.ru","description":"fafdfgnfdgdfs","description2":"ffdghdas"}, - {"email":"fas8@mail.ru","description":"fafdfghdfgdfs","description2":"ffdghdas"}, - {"email":"dfdfd@mail.ru","description":"sdffhdfhggsdfg","description2":"ffdgdfgsdfghdas"}, - {"email":"dfsdfsddfd@mail.ru","description":"sdffhdfsdfshggsdfg","description2":"ffdgdsdffgsdfghdas"} + {"id": 1,"email":"fas1@mail.ru","description":"sdgsdfg","description2":"ffdghdas"}, + {"id": 2,"email":"fas2@mail.ru","description":"fafdgdfgsdfdfs","description2":"ffdghdas"}, + {"id": 3,"email":"fas3@mail.ru","description":"fafdgdssdfgdfs","description2":"ffdghdas"}, + {"id": 4,"email":"fas4@mail.ru","description":"fafddsfgsdvcbgdfs","description2":"ffdghdas"}, + {"id": 5,"email":"fas5@mail.ru","description":"fafdgghjgfdfs","description2":"ffdghdas"}, + {"id": 6,"email":"fas6@mail.ru","description":"fafddfgdhvgdfs","description2":"ffdghdas"}, + {"id": 7,"email":"fas7@mail.ru","description":"fafdfgnfdgdfs","description2":"ffdghdas"}, + {"id": 8,"email":"fas8@mail.ru","description":"fafdfghdfgdfs","description2":"ffdghdas"}, + {"id": 12,"email":"dfdfd@mail.ru","description":"sdffhdfhggsdfg","description2":"ffdgdfgsdfghdas"}, + {"id": 13,"email":"dfsdfsddfd@mail.ru","description":"sdffhdfsdfshggsdfg","description2":"ffdgdsdffgsdfghdas"} ] } \ No newline at end of file diff --git a/examples/simple.php b/examples/simple.php index e447842..65562e9 100644 --- a/examples/simple.php +++ b/examples/simple.php @@ -12,5 +12,11 @@ $table->setBeforePrintCell(function ($key, $data) { $table->afterPrint(function ($meta) { return "
After Print
"; }); +$table->addColumn("Колонка 33", "k33", function ($id){ + return "my ID: " . $id; +}); +$table->addColumn("Колонка 34", "k34", function ($id){ + return "some34"; +}); $table->create(); $table->render(); \ No newline at end of file diff --git a/src/ListJsonTable.php b/src/ListJsonTable.php index d6a0df9..e3a050c 100644 --- a/src/ListJsonTable.php +++ b/src/ListJsonTable.php @@ -25,6 +25,7 @@ class ListJsonTable private array $data; private bool $pagination = true; + private bool $showActionColumn = true; private array $actionsArray = []; private array $customActionsArray = []; @@ -37,6 +38,7 @@ class ListJsonTable $this->data = json_decode($this->json, true); $this->baseUrl = $this->data['meta']['baseUrl'] ?? ''; $this->pagination = $this->data['meta']['pagination'] ?? true; + $this->showActionColumn = $this->data['meta']['showActionColumn'] ?? true; $this->beforePrintHook = function () { }; $this->afterPrintHook = function () { @@ -74,7 +76,10 @@ class ListJsonTable $this->html .= "" . $column . ""; } } - $this->html .= "Действия"; + $this->getCustomHeadColumn(); + if ($this->showActionColumn) { + $this->html .= "Действия"; + } } public function createTbody(): void @@ -82,22 +87,23 @@ class ListJsonTable if ($this->data['data']) { $this->count = $this->data["meta"]["perPage"] * ($this->data['meta']["currentPage"] - 1); - foreach ($this->data['data'] as $col) { + foreach ($this->data['data'] as $row) { $this->html .= ""; $this->count += 1; - $id = $col["id"] ?? $this->count; + $id = $row["id"] ?? $this->count; $this->html .= '' . $id . ''; - foreach ($col as $key => $row) { + foreach ($row as $key => $ceil) { if ($this->issetColumn($key) and $this->is_fillable($key)) { if ($this->beforePrintCell) { $hook = $this->beforePrintCell; - $row = $hook($key, $row); + $ceil = $hook($key, $ceil); } - $this->html .= "" . $row . ""; + $this->html .= "" . $ceil . ""; } } + $this->getCustomColumns($row["id"] ?? null); // if (isset($col['id'])) { // $newColumn = $this->getColumns($col['id']); // @@ -106,14 +112,14 @@ class ListJsonTable // else { // $this->html .= ""; // } + if ($this->showActionColumn) { + if (isset($row["id"])) { + $actions = $this->getActions($row["id"]); - if (isset($col["id"])){ - $actions = $this->getActions($col["id"]); - - $this->html .= "$actions"; - } - else { - $this->html .= ""; + $this->html .= "$actions"; + } else { + $this->html .= ""; + } } } } @@ -124,6 +130,10 @@ class ListJsonTable $this->customActionsArray[] = $actionColumn; } + public function addColumn(string $label, string $key, \Closure $closure): void + { + $this->customColumnsArray[] = ['label' => $label, "key" => $key, "handler" => $closure]; + } private function setActions(): void @@ -146,6 +156,20 @@ class ListJsonTable $this->actionsArray = array_merge($this->actionsArray, $this->customActionsArray); } + public function getCustomColumns($id = null): void + { + foreach ($this->customColumnsArray as $item) { + $this->html .= "".$item['handler']($id).""; + } + } + + public function getCustomHeadColumn(): void + { + foreach ($this->customColumnsArray as $item) { + $this->html .= "".$item['label'].""; + } + } + private function issetColumn($column): bool { if (isset($this->data['meta']['columns'])) { @@ -208,7 +232,6 @@ class ListJsonTable public function tableAction($json): void { - $tableJson = json_decode($json, true); foreach ($tableJson as $key => $value) { @@ -228,13 +251,13 @@ class ListJsonTable $hookAfter = $this->afterPrintHook; $this->html .= $hookAfter($this->data['meta']); - if ($this->pagination){ + if ($this->pagination) { $options = [ 'countItem' => $this->data['meta']['total'], 'perPage' => $this->data['meta']['perPage'], 'currentPage' => $this->data['meta']['currentPage'], 'baseUrl' => $this->baseUrl, - 'prefix' => $this->data['meta']['prefix'], + 'prefix' => $this->data['meta']['paginationPrefix'], ]; $pagination = new Pagination($options); $pagination->create(); diff --git a/src/Pagination.php b/src/Pagination.php index c5149a5..82961f1 100644 --- a/src/Pagination.php +++ b/src/Pagination.php @@ -22,7 +22,7 @@ class Pagination /** * @throws Exception */ - public function __construct(array $options,) + public function __construct(array $options) { if (!$options['countItem']) { throw new Exception(message: "countItem is not valid"); @@ -30,7 +30,7 @@ class Pagination $this->countItem = $options['countItem']; $this->perPage = $options['perPage'] ?? 10; $this->currentPage = $options['currentPage'] ?? 1; - $this->baseUrl = $options['baseUrl']; + $this->baseUrl = $options['baseUrl'] ?? ''; $this->baseUrl .= $options['prefix'] ?? '/page'; $this->countPages = ceil($this->countItem / $this->perPage);