From 433cc85f54bcf772f33ee316317ed8e2caef06d5 Mon Sep 17 00:00:00 2001 From: Kavalar Date: Thu, 1 Aug 2024 13:32:31 +0300 Subject: [PATCH] filters --- examples/simple.json | 26 +++++++++++--------- examples/simple.php | 34 +++++++++++++++++++++----- src/Filter.php | 2 +- src/ListJsonTable.php | 57 ++++++++++++++++++++++++++++++++++--------- 4 files changed, 89 insertions(+), 30 deletions(-) diff --git a/examples/simple.json b/examples/simple.json index fd08bf5..4e1499b 100644 --- a/examples/simple.json +++ b/examples/simple.json @@ -2,9 +2,11 @@ "meta": { "title": "forma 1", "columns": { + "id": "ID", "email": "Email", "description": "Описание 1", - "description2": "Описание 2" + "description2": "Описание 2", + "status": "Статус" }, "actions": ["view"], "pagination": true, @@ -17,18 +19,18 @@ "params": {"class": "table table-bordered", "border": "1"} }, "filters": [ - "id", "email" + "email", "description" ], "data": [ - {"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"} + {"id": 1,"email":"fas1@mail.ru","description":"sdgsdfg","description2":"ffdghdas", "status": 1}, + {"id": 2,"email":"fas2@mail.ru","description":"fafdgdfgsdfdfs","description2":"ffdghdas", "status": 1}, + {"id": 3,"email":"fas3@mail.ru","description":"fafdgdssdfgdfs","description2":"ffdghdas", "status": 2}, + {"id": 4,"email":"fas4@mail.ru","description":"fafddsfgsdvcbgdfs","description2":"ffdghdas", "status": 1}, + {"id": 5,"email":"fas5@mail.ru","description":"fafdgghjgfdfs","description2":"ffdghdas", "status": 1}, + {"id": 6,"email":"fas6@mail.ru","description":"fafddfgdhvgdfs","description2":"ffdghdas", "status": 1}, + {"id": 7,"email":"fas7@mail.ru","description":"fafdfgnfdgdfs","description2":"ffdghdas", "status": 0}, + {"id": 8,"email":"fas8@mail.ru","description":"fafdfghdfgdfs","description2":"ffdghdas", "status": 1}, + {"id": 12,"email":"dfdfd@mail.ru","description":"sdffhdfhggsdfg","description2":"ffdgdfgsdfghdas", "status": 99}, + {"id": 13,"email":"dfsdfsddfd@mail.ru","description":"sdffhdfsdfshggsdfg","description2":"ffdgdsdffgsdfghdas", "status": 1} ] } \ No newline at end of file diff --git a/examples/simple.php b/examples/simple.php index 65562e9..88e15d7 100644 --- a/examples/simple.php +++ b/examples/simple.php @@ -6,17 +6,39 @@ use Itguild\Tables\ListJsonTable; $json = file_get_contents('simple.json'); $table = new ListJsonTable($json); -$table->setBeforePrintCell(function ($key, $data) { - return $key == "email" ? "$data" : $data; -}); +//$table->column("status", function ($ceil){ +// return getStatusLabel()[$ceil]; +//}); +//$table->columns([ +// 'status' => function ($ceil) { +// return getStatusLabel()[$ceil]; +// }, +// 'email' => function ($ceil) { +// return "$ceil"; +// } +//]); +//$table->setBeforePrintCell(function ($key, $data) { +// return $key == "email" ? "$data" : $data; +//}); $table->afterPrint(function ($meta) { return "
After Print
"; }); -$table->addColumn("Колонка 33", "k33", function ($id){ +$table->addColumn("Колонка 33", "k33", function ($id) { return "my ID: " . $id; }); -$table->addColumn("Колонка 34", "k34", function ($id){ +$table->addColumn("Колонка 34", "k34", function ($id) { return "some34"; }); $table->create(); -$table->render(); \ No newline at end of file +$table->render(); + + +function getStatusLabel(): array +{ + return [ + 0 => "На модерации", + 1 => "Активный", + 2 => "Модератор", + 99 => "Удален", + ]; +} \ No newline at end of file diff --git a/src/Filter.php b/src/Filter.php index 2843862..cee3bb8 100644 --- a/src/Filter.php +++ b/src/Filter.php @@ -5,7 +5,7 @@ namespace Itguild\Tables; class Filter { // private array $columnsForFilter; -private string $column; + private string $column; private string $html = ""; private string $baseUrl; diff --git a/src/ListJsonTable.php b/src/ListJsonTable.php index 620160d..b054122 100644 --- a/src/ListJsonTable.php +++ b/src/ListJsonTable.php @@ -26,7 +26,7 @@ class ListJsonTable private bool $pagination = true; private bool $showActionColumn = true; - private bool $filter = true; + private bool|array $filters = false; private array $actionsArray = []; private array $customActionsArray = []; @@ -40,7 +40,7 @@ class ListJsonTable $this->baseUrl = $this->data['meta']['baseUrl'] ?? ''; $this->pagination = $this->data['meta']['pagination'] ?? true; $this->showActionColumn = $this->data['meta']['showActionColumn'] ?? true; - $this->filter = $this->data['meta']['filter'] ?? true; + $this->filters = $this->data['filters'] ?? false; $this->beforePrintHook = function () { }; $this->afterPrintHook = function () { @@ -67,21 +67,40 @@ class ListJsonTable public function createThead(): void { + $columnKeys = []; if (!isset($this->data['meta']['columns'])) { return; } $this->html .= ""; - $this->html .= "" . "ID" . ""; + if (!$this->issetColumn("id")) { + $this->html .= "" . "ID" . ""; + $columnKeys[] = "id"; + } foreach ($this->data['meta']['columns'] as $key => $column) { if ($this->is_fillable($key)) { $this->html .= "" . $column . ""; + $columnKeys[] = $key; } } + $columnKeys = array_merge($columnKeys, $this->getCustomColumnKeys()); $this->getCustomHeadColumn(); if ($this->showActionColumn) { $this->html .= "Действия"; } + + if ($this->filters) { + $this->html .= "
"; + foreach ($columnKeys as $key){ + if ($this->issetFilter($key)){ + $this->html .= ""; + } + else { + $this->html .= ""; + } + } + $this->html .= "
"; + } } public function createTbody(): void @@ -107,14 +126,15 @@ class ListJsonTable $this->html .= ""; $this->count += 1; $id = $row["id"] ?? $this->count; - $this->html .= '' . $id . ''; + if (!$this->issetColumn("id")) { + $this->html .= '' . $id . ''; + } foreach ($row as $key => $ceil) { if ($this->issetColumn($key) and $this->is_fillable($key)) { if ($this->beforePrintCell) { $hook = $this->beforePrintCell; $ceil = $hook($key, $ceil); } - $this->html .= "" . $ceil . ""; } } @@ -168,17 +188,27 @@ class ListJsonTable public function getCustomColumns($id = null): void { foreach ($this->customColumnsArray as $item) { - $this->html .= "".$item['handler']($id).""; + $this->html .= "" . $item['handler']($id) . ""; } } public function getCustomHeadColumn(): void { foreach ($this->customColumnsArray as $item) { - $this->html .= "".$item['label'].""; + $this->html .= "" . $item['label'] . ""; } } + protected function getCustomColumnKeys() + { + $keys = []; + foreach ($this->customColumnsArray as $item) { + $keys[] = $item['key']; + } + + return $keys; + } + private function issetColumn($column): bool { if (isset($this->data['meta']['columns'])) { @@ -194,13 +224,18 @@ class ListJsonTable private function issetFilter($filter): bool { - if (isset($this->data['meta']['filter'])) { - foreach ($this->data['meta']['filter'] as $key => $currentFilter) { - if ($key === $filter) { - return true; + if (isset($this->data['filters'])) { + foreach ($this->data['filters'] as $key => $currentFilter) { + if (is_array($currentFilter)) { + return false; + } elseif (is_string($currentFilter)) { + if ($currentFilter === $filter) { + return true; + } } } } + return false; }