filter class
This commit is contained in:
parent
77a306a0f6
commit
b4dc2f6ab1
@ -14,14 +14,12 @@
|
|||||||
"perPage": "5",
|
"perPage": "5",
|
||||||
"currentPage": "1",
|
"currentPage": "1",
|
||||||
"showActionColumn": true,
|
"showActionColumn": true,
|
||||||
|
"showFiltersRow": true,
|
||||||
"filter": true,
|
"filter": true,
|
||||||
"total": 10,
|
"total": 10,
|
||||||
"paginationPrefix": "/page",
|
"paginationPrefix": "/page",
|
||||||
"params": {"class": "table table-bordered", "border": "1"}
|
"params": {"class": "table table-bordered", "border": "1"}
|
||||||
},
|
},
|
||||||
"filters": [
|
|
||||||
"email"
|
|
||||||
],
|
|
||||||
"data": [
|
"data": [
|
||||||
{"id": 1,"email":"fas1@mail.ru","description":"sdgsdfg","description2":"ffdghdas", "created_at": "17.06.2024", "status": "1"},
|
{"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"},
|
{"id": 2,"email":"fas2@mail.ru","description":"fafdgdfgsdfdfs","description2":"ffdghdas", "created_at": "18.06.2024", "status": "1"},
|
||||||
|
@ -10,18 +10,22 @@ $table = new ListJsonTable($json);
|
|||||||
$table->columns([
|
$table->columns([
|
||||||
"created_at" => [
|
"created_at" => [
|
||||||
"format" => "date:Y-m-d",
|
"format" => "date:Y-m-d",
|
||||||
'filter' => "date"
|
'filter' => ["input" => "date"]
|
||||||
],
|
],
|
||||||
'description' => [
|
'description' => [
|
||||||
"format" => "html",
|
"format" => "html",
|
||||||
"style" => ["width" => "300px"],
|
"style" => ["width" => "300px"],
|
||||||
"filter" => "text",
|
"filter" => ["input" => ["radio" => "hello", "bye"]],
|
||||||
"value" => function ($cell) {
|
"value" => function ($cell) {
|
||||||
return "<span style='color: sienna'>$cell</span>";
|
return "<span style='color: sienna'>$cell</span>";
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
|
'description2' => [
|
||||||
|
"filter" => ["input" => "text"],
|
||||||
|
],
|
||||||
'status' => [
|
'status' => [
|
||||||
"format" => "integer",
|
"format" => "integer",
|
||||||
|
"filter" => ["select" => getStatusLabel()],
|
||||||
"value" => function ($cell) {
|
"value" => function ($cell) {
|
||||||
return getStatusLabel()[$cell];
|
return getStatusLabel()[$cell];
|
||||||
}],
|
}],
|
||||||
|
@ -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
19
src/Filter/Filter.php
Normal 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();
|
||||||
|
}
|
23
src/Filter/InputFilter.php
Normal file
23
src/Filter/InputFilter.php
Normal 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>";
|
||||||
|
}
|
||||||
|
}
|
19
src/Filter/SelectFilter.php
Normal file
19
src/Filter/SelectFilter.php
Normal 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;
|
||||||
|
}
|
||||||
|
}
|
@ -91,11 +91,29 @@ class JasonTable
|
|||||||
return $styleStr;
|
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)
|
protected function getFilterFromCustomColumn(string $column)
|
||||||
{
|
{
|
||||||
if (is_array($this->beforePrintCell[$column])) {
|
if (is_array($this->beforePrintCell[$column])) {
|
||||||
if (isset($this->beforePrintCell[$column]['filter'])) {
|
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";
|
return "text";
|
||||||
|
@ -6,6 +6,8 @@ use Exception;
|
|||||||
use Itguild\Tables\ActionColumn\DeleteActionColumn;
|
use Itguild\Tables\ActionColumn\DeleteActionColumn;
|
||||||
use Itguild\Tables\ActionColumn\EditActionColumn;
|
use Itguild\Tables\ActionColumn\EditActionColumn;
|
||||||
use Itguild\Tables\ActionColumn\ViewActionColumn;
|
use Itguild\Tables\ActionColumn\ViewActionColumn;
|
||||||
|
use Itguild\Tables\Filter\InputFilter;
|
||||||
|
use Itguild\Tables\Filter\SelectFilter;
|
||||||
use Itguild\Tables\traits\CreateParams;
|
use Itguild\Tables\traits\CreateParams;
|
||||||
use JetBrains\PhpStorm\NoReturn;
|
use JetBrains\PhpStorm\NoReturn;
|
||||||
|
|
||||||
@ -21,7 +23,8 @@ class ListJsonTable extends JasonTable
|
|||||||
|
|
||||||
private bool $pagination = true;
|
private bool $pagination = true;
|
||||||
private bool $showActionColumn = true;
|
private bool $showActionColumn = true;
|
||||||
private bool|array $filters = false;
|
private bool $showFiltersRow = true;
|
||||||
|
private bool|array $filters = [];
|
||||||
|
|
||||||
private array $actionsArray = [];
|
private array $actionsArray = [];
|
||||||
private array $customActionsArray = [];
|
private array $customActionsArray = [];
|
||||||
@ -35,7 +38,8 @@ class ListJsonTable extends JasonTable
|
|||||||
$this->baseUrl = $this->data['meta']['baseUrl'] ?? '';
|
$this->baseUrl = $this->data['meta']['baseUrl'] ?? '';
|
||||||
$this->pagination = $this->data['meta']['pagination'] ?? true;
|
$this->pagination = $this->data['meta']['pagination'] ?? true;
|
||||||
$this->showActionColumn = $this->data['meta']['showActionColumn'] ?? 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->beforePrintHook = function () {
|
||||||
};
|
};
|
||||||
$this->afterPrintHook = function () {
|
$this->afterPrintHook = function () {
|
||||||
@ -66,10 +70,14 @@ class ListJsonTable extends JasonTable
|
|||||||
$columnKeys = array_merge($columnKeys, $this->getCustomColumnKeys());
|
$columnKeys = array_merge($columnKeys, $this->getCustomColumnKeys());
|
||||||
$this->getCustomHeadColumn();
|
$this->getCustomHeadColumn();
|
||||||
if ($this->showActionColumn) {
|
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->getFilters($columnKeys);
|
||||||
|
$this->html .= "</thead>";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -139,6 +147,15 @@ class ListJsonTable extends JasonTable
|
|||||||
$this->actionsArray = array_merge($this->actionsArray, $this->customActionsArray);
|
$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
|
public function getCustomColumns($id = null): void
|
||||||
{
|
{
|
||||||
foreach ($this->customColumnsArray as $item) {
|
foreach ($this->customColumnsArray as $item) {
|
||||||
@ -251,7 +268,15 @@ class ListJsonTable extends JasonTable
|
|||||||
$this->html .= "<tr><form action='$this->baseUrl/search'>";
|
$this->html .= "<tr><form action='$this->baseUrl/search'>";
|
||||||
foreach ($columnKeys as $key){
|
foreach ($columnKeys as $key){
|
||||||
if ($this->issetFilter($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 {
|
else {
|
||||||
$this->html .= "<td></td>";
|
$this->html .= "<td></td>";
|
||||||
@ -266,6 +291,7 @@ class ListJsonTable extends JasonTable
|
|||||||
public function create(): void
|
public function create(): void
|
||||||
{
|
{
|
||||||
$this->setActions();
|
$this->setActions();
|
||||||
|
// $this->setFilters();
|
||||||
$this->beginTable();
|
$this->beginTable();
|
||||||
$this->createThead();
|
$this->createThead();
|
||||||
$this->createTbody();
|
$this->createTbody();
|
||||||
|
Loading…
Reference in New Issue
Block a user