fix filter

This commit is contained in:
Билай Станислав 2024-08-28 14:21:00 +03:00
parent f03ec200ac
commit 5064042929
6 changed files with 83 additions and 63 deletions

View File

@ -2,6 +2,8 @@
require_once "../vendor/autoload.php";
use Itguild\Tables\Filter\InputFilter;
use Itguild\Tables\Filter\SelectFilter;
use Itguild\Tables\ListJsonTable;
$json = file_get_contents('simple.json');
@ -10,32 +12,49 @@ $table = new ListJsonTable($json);
$table->columns([
"created_at" => [
"format" => "date:Y-m-d",
'filter' => ["input" => "date"]
'filter' => [
'class' => InputFilter::class,
'param' => 'date'
]
],
'description' => [
"format" => "html",
"style" => ["width" => "300px"],
"filter" => ["input" => ["radio" => ["hello", "bye"]]],
"filter" => [
'class' => InputFilter::class,
'param' => 'text'
],
"value" => function ($cell) {
return "<span style='color: sienna'>$cell</span>";
}
],
'description2' => [
"filter" => ["input" => "text"],
"format" => "html",
"filter" => [
'class' => SelectFilter::class,
'param' => ['black', 'red', 'green', 'blue', 'yellow']
],
],
'status' => [
"format" => "integer",
"filter" => ["select" => getStatusLabel()],
"filter" => [
'class' => SelectFilter::class,
'param' => getStatusLabel()
],
"value" => function ($cell) {
return getStatusLabel()[$cell];
}],
'k33' => [
"format" => "integer",
'filter' => [
'class' => InputFilter::class,
'param' => 'range'
]
],
'email' => function ($cell) {
return "<span style='color: aqua'>$cell</span>";
},
]);
//$table->setBeforePrintCell(function ($key, $data) {
// return $key == "email" ? "<span style='color: aqua'>$data</span>" : $data;
//});
$table->afterPrint(function ($meta) {
return "<div>After Print</div>";
});

View File

@ -5,13 +5,11 @@ namespace Itguild\Tables\Filter;
abstract class Filter
{
public string $html = '';
public string|array $data;
public string|array $param;
public string $name;
// public array|string $value;
public function __construct(array $source)
{
$this->data = $source['data'] ?? '';
// $this->value = $data['value'] ?? [];
$this->param = $source['param'] ?? '';
$this->name = $source['name'];
}

View File

@ -9,17 +9,6 @@ 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[$key] as $value){
//// echo "<pre>";
//// print_r($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>";
return "<td><input type='$this->param' name='$this->name'></td>";
}
}

View File

@ -10,7 +10,7 @@ class SelectFilter extends Filter
public function fetch()
{
$this->html = "<td><select name='$this->name'>";
foreach ($this->data as $value) {
foreach ($this->param as $value) {
$this->html .= "<option value='$value'>$value</option>";
}
$this->html .= "</select></td>";

View File

@ -91,33 +91,33 @@ 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($this->beforePrintCell[$column]['filter']);
// 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($this->beforePrintCell[$column]['filter']);
//// }
// }
}
}
}
return "text";
}
protected function getFilterFromCustomColumn(string $column)
{
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 $value) {
// echo "<pre>";
// print_r(current($this->beforePrintCell[$column]['filter']));
return current($this->beforePrintCell[$column]['filter']);
// }
}
}
}
return "text";
}
// }
// return "text";
// }
//
// protected function getFilterFromCustomColumn(string $column)
// {
// 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 $value) {
//// echo "<pre>";
//// print_r(current($this->beforePrintCell[$column]['filter']));
// return current($this->beforePrintCell[$column]['filter']);
//// }
// }
// }
// }
// return "text";
// }
}

View File

@ -147,13 +147,21 @@ class ListJsonTable extends JasonTable
$this->actionsArray = array_merge($this->actionsArray, $this->customActionsArray);
}
private function getCurrentFilter(string $filter): false|string
private function getCurrentFilter(string $column)
{
return match ($filter) {
'input' => InputFilter::class,
'select' => SelectFilter::class,
default => false,
};
// return match ($filter) {
// 'input' => InputFilter::class,
// 'select' => SelectFilter::class,
// default => false,
// };
if (is_array($this->beforePrintCell[$column])) {
if (isset($this->beforePrintCell[$column]['filter'])) {
if (is_array($this->beforePrintCell[$column]['filter'])) {
return $this->beforePrintCell[$column]['filter'];
}
}
}
return false;
}
public function getCustomColumns($id = null): void
@ -269,12 +277,18 @@ class ListJsonTable extends JasonTable
foreach ($columnKeys as $key){
if ($this->issetFilter($key)){
// $this->html .= "<td><input type='" . $this->getFilterFromCustomColumn($key) . "' name='$key'></td>";
$item = $this->getCurrentFilter($this->getTagFromCustomColumn($key));
$objItem = new $item([
'data' => $this->getFilterFromCustomColumn($key),
// $item = $this->getCurrentFilter($this->getTagFromCustomColumn($key));
// $objItem = new $item([
// 'data' => $this->getFilterFromCustomColumn($key),
// 'name' => $key
// ]);
// $this->html .= $objItem->fetch();
$arr = $this->getCurrentFilter($key);
$class = new $arr['class']([
'param' => $arr['param'],
'name' => $key
]);
$this->html .= $objItem->fetch();
$this->html .= $class->fetch();
}
else {
$this->html .= "<td></td>";