This commit is contained in:
2024-08-01 13:32:31 +03:00
parent 39931b257b
commit 433cc85f54
4 changed files with 89 additions and 30 deletions

View File

@ -5,7 +5,7 @@ namespace Itguild\Tables;
class Filter
{
// private array $columnsForFilter;
private string $column;
private string $column;
private string $html = "";
private string $baseUrl;

View File

@ -26,7 +26,7 @@ class ListJsonTable
private bool $pagination = true;
private bool $showActionColumn = true;
private bool $filter = true;
private bool|array $filters = false;
private array $actionsArray = [];
private array $customActionsArray = [];
@ -40,7 +40,7 @@ class ListJsonTable
$this->baseUrl = $this->data['meta']['baseUrl'] ?? '';
$this->pagination = $this->data['meta']['pagination'] ?? true;
$this->showActionColumn = $this->data['meta']['showActionColumn'] ?? true;
$this->filter = $this->data['meta']['filter'] ?? true;
$this->filters = $this->data['filters'] ?? false;
$this->beforePrintHook = function () {
};
$this->afterPrintHook = function () {
@ -67,21 +67,40 @@ class ListJsonTable
public function createThead(): void
{
$columnKeys = [];
if (!isset($this->data['meta']['columns'])) {
return;
}
$this->html .= "<thead><tr>";
$this->html .= "<th>" . "ID" . "</th>";
if (!$this->issetColumn("id")) {
$this->html .= "<th>" . "ID" . "</th>";
$columnKeys[] = "id";
}
foreach ($this->data['meta']['columns'] as $key => $column) {
if ($this->is_fillable($key)) {
$this->html .= "<th>" . $column . "</th>";
$columnKeys[] = $key;
}
}
$columnKeys = array_merge($columnKeys, $this->getCustomColumnKeys());
$this->getCustomHeadColumn();
if ($this->showActionColumn) {
$this->html .= "<th>Действия</th></th></tr></thead>";
}
if ($this->filters) {
$this->html .= "<tr><form action='$this->baseUrl/search'>";
foreach ($columnKeys as $key){
if ($this->issetFilter($key)){
$this->html .= "<td><input type='text' name='$key'></td>";
}
else {
$this->html .= "<td></td>";
}
}
$this->html .= "</form></tr>";
}
}
public function createTbody(): void
@ -107,14 +126,15 @@ class ListJsonTable
$this->html .= "<tr>";
$this->count += 1;
$id = $row["id"] ?? $this->count;
$this->html .= '<td><a href=' . $this->baseUrl . "/" . $id . '>' . $id . '</a></td>';
if (!$this->issetColumn("id")) {
$this->html .= '<td><a href=' . $this->baseUrl . "/" . $id . '>' . $id . '</a></td>';
}
foreach ($row as $key => $ceil) {
if ($this->issetColumn($key) and $this->is_fillable($key)) {
if ($this->beforePrintCell) {
$hook = $this->beforePrintCell;
$ceil = $hook($key, $ceil);
}
$this->html .= "<td>" . $ceil . "</td>";
}
}
@ -168,17 +188,27 @@ class ListJsonTable
public function getCustomColumns($id = null): void
{
foreach ($this->customColumnsArray as $item) {
$this->html .= "<td>".$item['handler']($id)."</td>";
$this->html .= "<td>" . $item['handler']($id) . "</td>";
}
}
public function getCustomHeadColumn(): void
{
foreach ($this->customColumnsArray as $item) {
$this->html .= "<th>".$item['label']."</th>";
$this->html .= "<th>" . $item['label'] . "</th>";
}
}
protected function getCustomColumnKeys()
{
$keys = [];
foreach ($this->customColumnsArray as $item) {
$keys[] = $item['key'];
}
return $keys;
}
private function issetColumn($column): bool
{
if (isset($this->data['meta']['columns'])) {
@ -194,13 +224,18 @@ class ListJsonTable
private function issetFilter($filter): bool
{
if (isset($this->data['meta']['filter'])) {
foreach ($this->data['meta']['filter'] as $key => $currentFilter) {
if ($key === $filter) {
return true;
if (isset($this->data['filters'])) {
foreach ($this->data['filters'] as $key => $currentFilter) {
if (is_array($currentFilter)) {
return false;
} elseif (is_string($currentFilter)) {
if ($currentFilter === $filter) {
return true;
}
}
}
}
return false;
}