filter class

This commit is contained in:
Билай Станислав 2024-08-23 14:52:22 +03:00
parent 77a306a0f6
commit b4dc2f6ab1
8 changed files with 118 additions and 59 deletions

View File

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

View File

@ -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 "<span style='color: sienna'>$cell</span>";
}
],
'description2' => [
"filter" => ["input" => "text"],
],
'status' => [
"format" => "integer",
"filter" => ["select" => getStatusLabel()],
"value" => function ($cell) {
return getStatusLabel()[$cell];
}],

View File

@ -1,48 +0,0 @@
<?php
namespace Itguild\Tables;
class Filter
{
// private array $columnsForFilter;
private string $column;
private string $html = "";
private string $baseUrl;
public function __construct(array $columns, string $baseUrl)
{
$this->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 '<nav aria-label="Filters example">
<form action="{action_link}">
<input type="text" name="{column}">
</nav>';
}
}

19
src/Filter/Filter.php Normal file
View File

@ -0,0 +1,19 @@
<?php
namespace Itguild\Tables\Filter;
abstract class Filter
{
public string $html = '';
public string|array $data;
public string $name;
// public array|string $value;
public function __construct(array $source)
{
$this->data = $source['data'] ?? '';
// $this->value = $data['value'] ?? [];
$this->name = $source['name'];
}
abstract public function fetch();
}

View File

@ -0,0 +1,23 @@
<?php
namespace Itguild\Tables\Filter;
use Itguild\Tables\Filter\Filter;
class InputFilter extends Filter
{
public function fetch()
{
// if(is_array($this->data)){
// $this->html = "<td>";
//// var_dump(key($this->data));
// $key = key($this->data);
// foreach ($this->data as $value){
// $this->html .= "<input type='$key' name='$this->name' value='$value'>";
// }
// return $this->html . "</td>";
// }
return "<td><input type='$this->data' name='$this->name'></td>";
}
}

View File

@ -0,0 +1,19 @@
<?php
namespace Itguild\Tables\Filter;
use Itguild\Tables\Filter\Filter;
class SelectFilter extends Filter
{
public function fetch()
{
$this->html = "<td><select name='$this->name'>";
foreach ($this->data as $value) {
$this->html .= "<option value='$value'>$value</option>";
}
$this->html .= "</select></td>";
return $this->html;
}
}

View File

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

View File

@ -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 .= "<th>Действия</th></th></tr></thead>";
$this->html .= "<th>Действия</th></th></tr>";
}
if ($this->filters) {
// if ($this->filters) {
// $this->getFilters($columnKeys);
// }
if($this->showFiltersRow){
$this->getFilters($columnKeys);
$this->html .= "</thead>";
}
}
@ -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 .= "<tr><form action='$this->baseUrl/search'>";
foreach ($columnKeys as $key){
if ($this->issetFilter($key)){
$this->html .= "<td><input type='" . $this->getFilterFromCustomColumn($key) . "' name='$key'></td>";
// $this->html .= "<td><input type='" . $this->getFilterFromCustomColumn($key) . "' name='$key'></td>";
$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 .= "<td></td>";
@ -266,6 +291,7 @@ class ListJsonTable extends JasonTable
public function create(): void
{
$this->setActions();
// $this->setFilters();
$this->beginTable();
$this->createThead();
$this->createTbody();