add value to filter

This commit is contained in:
Билай Станислав 2024-08-28 15:09:34 +03:00
parent 4a15305505
commit f20f408f12
5 changed files with 17 additions and 7 deletions

View File

@ -22,7 +22,8 @@ $table->columns([
"style" => ["width" => "300px"], "style" => ["width" => "300px"],
"filter" => [ "filter" => [
'class' => InputFilter::class, 'class' => InputFilter::class,
'param' => 'text' 'param' => 'text',
'value' => 'value'
], ],
"value" => function ($cell) { "value" => function ($cell) {
return "<span style='color: sienna'>$cell</span>"; return "<span style='color: sienna'>$cell</span>";
@ -32,14 +33,16 @@ $table->columns([
"format" => "html", "format" => "html",
"filter" => [ "filter" => [
'class' => SelectFilter::class, 'class' => SelectFilter::class,
'param' => ['black', 'red', 'green', 'blue', 'yellow'] 'param' => ['black', 'red', 'green', 'blue', 'yellow'],
'value' => 'red'
], ],
], ],
'status' => [ 'status' => [
"format" => "integer", "format" => "integer",
"filter" => [ "filter" => [
'class' => SelectFilter::class, 'class' => SelectFilter::class,
'param' => getStatusLabel() 'param' => getStatusLabel(),
'value' => 'Активный'
], ],
"value" => function ($cell) { "value" => function ($cell) {
return getStatusLabel()[$cell]; return getStatusLabel()[$cell];

View File

@ -7,10 +7,12 @@ abstract class Filter
public string $html = ''; public string $html = '';
public string|array $param; public string|array $param;
public string $name; public string $name;
public string $value;
public function __construct(array $source) public function __construct(array $source)
{ {
$this->param = $source['param'] ?? ''; $this->param = $source['param'] ?? '';
$this->name = $source['name']; $this->name = $source['name'];
$this->value = $source['value'] ?? '';
} }
abstract public function fetch(); abstract public function fetch();

View File

@ -9,6 +9,6 @@ class InputFilter extends Filter
public function fetch() public function fetch()
{ {
return "<td><input type='$this->param' name='$this->name'></td>"; return "<td><input type='$this->param' name='$this->name' value ='$this->value'></td>";
} }
} }

View File

@ -11,9 +11,13 @@ class SelectFilter extends Filter
{ {
$this->html = "<td><select name='$this->name'>"; $this->html = "<td><select name='$this->name'>";
foreach ($this->param as $value) { foreach ($this->param as $value) {
if ($value === $this->value) {
$this->html .= "<option value='$value' selected>$value</option>";
} else {
$this->html .= "<option value='$value'>$value</option>"; $this->html .= "<option value='$value'>$value</option>";
} }
$this->html .= "</select></td>"; }
$this->html .= "value='$this->value'</select></td>";
return $this->html; return $this->html;
} }
} }

View File

@ -286,7 +286,8 @@ class ListJsonTable extends JasonTable
$filter = $this->getCurrentFilter($key); $filter = $this->getCurrentFilter($key);
$class = new $filter['class']([ $class = new $filter['class']([
'param' => $filter['param'], 'param' => $filter['param'],
'name' => $key 'name' => $key,
'value' => $filter['value']
]); ]);
$this->html .= $class->fetch(); $this->html .= $class->fetch();
} }