From b4dc2f6ab1f0e370d43728745416a58cf12fe9cb Mon Sep 17 00:00:00 2001 From: stasbilay02 Date: Fri, 23 Aug 2024 14:52:22 +0300 Subject: [PATCH] filter class --- examples/simple.json | 4 +--- examples/simple.php | 8 +++++-- src/Filter.php | 48 ------------------------------------- src/Filter/Filter.php | 19 +++++++++++++++ src/Filter/InputFilter.php | 23 ++++++++++++++++++ src/Filter/SelectFilter.php | 19 +++++++++++++++ src/JasonTable.php | 20 +++++++++++++++- src/ListJsonTable.php | 36 ++++++++++++++++++++++++---- 8 files changed, 118 insertions(+), 59 deletions(-) delete mode 100644 src/Filter.php create mode 100644 src/Filter/Filter.php create mode 100644 src/Filter/InputFilter.php create mode 100644 src/Filter/SelectFilter.php diff --git a/examples/simple.json b/examples/simple.json index 1b64c41..7b13196 100644 --- a/examples/simple.json +++ b/examples/simple.json @@ -14,14 +14,12 @@ "perPage": "5", "currentPage": "1", "showActionColumn": true, + "showFiltersRow": true, "filter": true, "total": 10, "paginationPrefix": "/page", "params": {"class": "table table-bordered", "border": "1"} }, - "filters": [ - "email" - ], "data": [ {"id": 1,"email":"fas1@mail.ru","description":"sdgsdfg","description2":"ffdghdas", "created_at": "17.06.2024", "status": "1"}, {"id": 2,"email":"fas2@mail.ru","description":"fafdgdfgsdfdfs","description2":"ffdghdas", "created_at": "18.06.2024", "status": "1"}, diff --git a/examples/simple.php b/examples/simple.php index 0584bd7..edf8606 100644 --- a/examples/simple.php +++ b/examples/simple.php @@ -10,18 +10,22 @@ $table = new ListJsonTable($json); $table->columns([ "created_at" => [ "format" => "date:Y-m-d", - 'filter' => "date" + 'filter' => ["input" => "date"] ], 'description' => [ "format" => "html", "style" => ["width" => "300px"], - "filter" => "text", + "filter" => ["input" => ["radio" => "hello", "bye"]], "value" => function ($cell) { return "$cell"; } ], + 'description2' => [ + "filter" => ["input" => "text"], + ], 'status' => [ "format" => "integer", + "filter" => ["select" => getStatusLabel()], "value" => function ($cell) { return getStatusLabel()[$cell]; }], diff --git a/src/Filter.php b/src/Filter.php deleted file mode 100644 index cee3bb8..0000000 --- a/src/Filter.php +++ /dev/null @@ -1,48 +0,0 @@ -columnsForFilter = $columns; - $this->baseUrl = $baseUrl; - } - - public function create(): void - { - $this->html = str_replace('{action_link}', $this->baseUrl, $this->getTemplate()); - $this->html = str_replace('{column}', 'id', $this->html); - } - - public function render(): void - { - echo $this->html; - } - - public function fetch(): string - { - return $this->html; - } - - public function getColumnsForFilter(): array - { - return $this->columnsForFilter; - } - - public function getTemplate(): string - { - return ''; - } -} \ No newline at end of file diff --git a/src/Filter/Filter.php b/src/Filter/Filter.php new file mode 100644 index 0000000..67c8ea3 --- /dev/null +++ b/src/Filter/Filter.php @@ -0,0 +1,19 @@ +data = $source['data'] ?? ''; +// $this->value = $data['value'] ?? []; + $this->name = $source['name']; + } + + abstract public function fetch(); +} \ No newline at end of file diff --git a/src/Filter/InputFilter.php b/src/Filter/InputFilter.php new file mode 100644 index 0000000..9275a6d --- /dev/null +++ b/src/Filter/InputFilter.php @@ -0,0 +1,23 @@ +data)){ +// $this->html = ""; +//// var_dump(key($this->data)); +// $key = key($this->data); +// foreach ($this->data as $value){ +// $this->html .= ""; +// } +// return $this->html . ""; +// } + return ""; + } +} \ No newline at end of file diff --git a/src/Filter/SelectFilter.php b/src/Filter/SelectFilter.php new file mode 100644 index 0000000..9c9999c --- /dev/null +++ b/src/Filter/SelectFilter.php @@ -0,0 +1,19 @@ +html = ""; + return $this->html; + } +} \ No newline at end of file diff --git a/src/JasonTable.php b/src/JasonTable.php index 53be197..ab979ed 100644 --- a/src/JasonTable.php +++ b/src/JasonTable.php @@ -91,11 +91,29 @@ class JasonTable return $styleStr; } + protected function getTagFromCustomColumn(string $column): string + { + if (is_array($this->beforePrintCell[$column])) { + if (isset($this->beforePrintCell[$column]['filter'])) { + if (is_array($this->beforePrintCell[$column]['filter'])) { + foreach ($this->beforePrintCell[$column]['filter'] as $key => $value) { + return $key; + } + } + } + } + return "text"; + } + protected function getFilterFromCustomColumn(string $column) { if (is_array($this->beforePrintCell[$column])) { if (isset($this->beforePrintCell[$column]['filter'])) { - return $this->beforePrintCell[$column]['filter']; + if (is_array($this->beforePrintCell[$column]['filter'])) { + foreach ($this->beforePrintCell[$column]['filter'] as $value) { + return $value; + } + } } } return "text"; diff --git a/src/ListJsonTable.php b/src/ListJsonTable.php index 20be360..70d904a 100644 --- a/src/ListJsonTable.php +++ b/src/ListJsonTable.php @@ -6,6 +6,8 @@ use Exception; use Itguild\Tables\ActionColumn\DeleteActionColumn; use Itguild\Tables\ActionColumn\EditActionColumn; use Itguild\Tables\ActionColumn\ViewActionColumn; +use Itguild\Tables\Filter\InputFilter; +use Itguild\Tables\Filter\SelectFilter; use Itguild\Tables\traits\CreateParams; use JetBrains\PhpStorm\NoReturn; @@ -21,7 +23,8 @@ class ListJsonTable extends JasonTable private bool $pagination = true; private bool $showActionColumn = true; - private bool|array $filters = false; + private bool $showFiltersRow = true; + private bool|array $filters = []; private array $actionsArray = []; private array $customActionsArray = []; @@ -35,7 +38,8 @@ class ListJsonTable extends JasonTable $this->baseUrl = $this->data['meta']['baseUrl'] ?? ''; $this->pagination = $this->data['meta']['pagination'] ?? true; $this->showActionColumn = $this->data['meta']['showActionColumn'] ?? true; - $this->filters = $this->data['filters'] ?? false; + $this->showFiltersRow = $this->data['meta']['showFiltersRow'] ?? true; + $this->filters = $this->data['filters'] ?? []; $this->beforePrintHook = function () { }; $this->afterPrintHook = function () { @@ -66,10 +70,14 @@ class ListJsonTable extends JasonTable $columnKeys = array_merge($columnKeys, $this->getCustomColumnKeys()); $this->getCustomHeadColumn(); if ($this->showActionColumn) { - $this->html .= "Действия"; + $this->html .= "Действия"; } - if ($this->filters) { +// if ($this->filters) { +// $this->getFilters($columnKeys); +// } + if($this->showFiltersRow){ $this->getFilters($columnKeys); + $this->html .= ""; } } @@ -139,6 +147,15 @@ class ListJsonTable extends JasonTable $this->actionsArray = array_merge($this->actionsArray, $this->customActionsArray); } + private function getCurrentFilter(string $filter): false|string + { + return match ($filter) { + 'input' => InputFilter::class, + 'select' => SelectFilter::class, + default => false, + }; + } + public function getCustomColumns($id = null): void { foreach ($this->customColumnsArray as $item) { @@ -251,7 +268,15 @@ class ListJsonTable extends JasonTable $this->html .= ""; foreach ($columnKeys as $key){ if ($this->issetFilter($key)){ - $this->html .= ""; +// $this->html .= ""; + $tag = $this->getTagFromCustomColumn($key); + $item = $this->getCurrentFilter($tag); + $objItem = new $item([ + 'data' => $this->getFilterFromCustomColumn($key), + 'name' => $key +// 'value' => $this->getFilterFromCustomColumn($key) + ]); + $this->html .= $objItem->fetch(); } else { $this->html .= ""; @@ -266,6 +291,7 @@ class ListJsonTable extends JasonTable public function create(): void { $this->setActions(); +// $this->setFilters(); $this->beginTable(); $this->createThead(); $this->createTbody();