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

View File

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

View File

@ -9,17 +9,6 @@ class InputFilter extends Filter
public function fetch() public function fetch()
{ {
// if(is_array($this->data)){ return "<td><input type='$this->param' name='$this->name'></td>";
// $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>";
} }
} }

View File

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

View File

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