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

View File

@ -2,9 +2,11 @@
"meta": { "meta": {
"title": "forma 1", "title": "forma 1",
"columns": { "columns": {
"id": "ID",
"email": "Email", "email": "Email",
"description": "Описание 1", "description": "Описание 1",
"description2": "Описание 2" "description2": "Описание 2",
"status": "Статус"
}, },
"actions": ["view"], "actions": ["view"],
"pagination": true, "pagination": true,
@ -17,18 +19,18 @@
"params": {"class": "table table-bordered", "border": "1"} "params": {"class": "table table-bordered", "border": "1"}
}, },
"filters": [ "filters": [
"id", "email" "email", "description"
], ],
"data": [ "data": [
{"id": 1,"email":"fas1@mail.ru","description":"sdgsdfg","description2":"ffdghdas"}, {"id": 1,"email":"fas1@mail.ru","description":"sdgsdfg","description2":"ffdghdas", "status": 1},
{"id": 2,"email":"fas2@mail.ru","description":"fafdgdfgsdfdfs","description2":"ffdghdas"}, {"id": 2,"email":"fas2@mail.ru","description":"fafdgdfgsdfdfs","description2":"ffdghdas", "status": 1},
{"id": 3,"email":"fas3@mail.ru","description":"fafdgdssdfgdfs","description2":"ffdghdas"}, {"id": 3,"email":"fas3@mail.ru","description":"fafdgdssdfgdfs","description2":"ffdghdas", "status": 2},
{"id": 4,"email":"fas4@mail.ru","description":"fafddsfgsdvcbgdfs","description2":"ffdghdas"}, {"id": 4,"email":"fas4@mail.ru","description":"fafddsfgsdvcbgdfs","description2":"ffdghdas", "status": 1},
{"id": 5,"email":"fas5@mail.ru","description":"fafdgghjgfdfs","description2":"ffdghdas"}, {"id": 5,"email":"fas5@mail.ru","description":"fafdgghjgfdfs","description2":"ffdghdas", "status": 1},
{"id": 6,"email":"fas6@mail.ru","description":"fafddfgdhvgdfs","description2":"ffdghdas"}, {"id": 6,"email":"fas6@mail.ru","description":"fafddfgdhvgdfs","description2":"ffdghdas", "status": 1},
{"id": 7,"email":"fas7@mail.ru","description":"fafdfgnfdgdfs","description2":"ffdghdas"}, {"id": 7,"email":"fas7@mail.ru","description":"fafdfgnfdgdfs","description2":"ffdghdas", "status": 0},
{"id": 8,"email":"fas8@mail.ru","description":"fafdfghdfgdfs","description2":"ffdghdas"}, {"id": 8,"email":"fas8@mail.ru","description":"fafdfghdfgdfs","description2":"ffdghdas", "status": 1},
{"id": 12,"email":"dfdfd@mail.ru","description":"sdffhdfhggsdfg","description2":"ffdgdfgsdfghdas"}, {"id": 12,"email":"dfdfd@mail.ru","description":"sdffhdfhggsdfg","description2":"ffdgdfgsdfghdas", "status": 99},
{"id": 13,"email":"dfsdfsddfd@mail.ru","description":"sdffhdfsdfshggsdfg","description2":"ffdgdsdffgsdfghdas"} {"id": 13,"email":"dfsdfsddfd@mail.ru","description":"sdffhdfsdfshggsdfg","description2":"ffdgdsdffgsdfghdas", "status": 1}
] ]
} }

View File

@ -6,17 +6,39 @@ use Itguild\Tables\ListJsonTable;
$json = file_get_contents('simple.json'); $json = file_get_contents('simple.json');
$table = new ListJsonTable($json); $table = new ListJsonTable($json);
$table->setBeforePrintCell(function ($key, $data) { //$table->column("status", function ($ceil){
return $key == "email" ? "<span style='color: aqua'>$data</span>" : $data; // return getStatusLabel()[$ceil];
}); //});
//$table->columns([
// 'status' => function ($ceil) {
// return getStatusLabel()[$ceil];
// },
// 'email' => function ($ceil) {
// return "<span style='color: aqua'>$ceil</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>";
}); });
$table->addColumn("Колонка 33", "k33", function ($id){ $table->addColumn("Колонка 33", "k33", function ($id) {
return "my ID: " . $id; return "my ID: " . $id;
}); });
$table->addColumn("Колонка 34", "k34", function ($id){ $table->addColumn("Колонка 34", "k34", function ($id) {
return "some34"; return "some34";
}); });
$table->create(); $table->create();
$table->render(); $table->render();
function getStatusLabel(): array
{
return [
0 => "На модерации",
1 => "Активный",
2 => "Модератор",
99 => "Удален",
];
}

View File

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

View File

@ -26,7 +26,7 @@ class ListJsonTable
private bool $pagination = true; private bool $pagination = true;
private bool $showActionColumn = true; private bool $showActionColumn = true;
private bool $filter = true; private bool|array $filters = false;
private array $actionsArray = []; private array $actionsArray = [];
private array $customActionsArray = []; private array $customActionsArray = [];
@ -40,7 +40,7 @@ class ListJsonTable
$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->filter = $this->data['meta']['filter'] ?? true; $this->filters = $this->data['filters'] ?? false;
$this->beforePrintHook = function () { $this->beforePrintHook = function () {
}; };
$this->afterPrintHook = function () { $this->afterPrintHook = function () {
@ -67,21 +67,40 @@ class ListJsonTable
public function createThead(): void public function createThead(): void
{ {
$columnKeys = [];
if (!isset($this->data['meta']['columns'])) { if (!isset($this->data['meta']['columns'])) {
return; return;
} }
$this->html .= "<thead><tr>"; $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) { foreach ($this->data['meta']['columns'] as $key => $column) {
if ($this->is_fillable($key)) { if ($this->is_fillable($key)) {
$this->html .= "<th>" . $column . "</th>"; $this->html .= "<th>" . $column . "</th>";
$columnKeys[] = $key;
} }
} }
$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></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 public function createTbody(): void
@ -107,14 +126,15 @@ class ListJsonTable
$this->html .= "<tr>"; $this->html .= "<tr>";
$this->count += 1; $this->count += 1;
$id = $row["id"] ?? $this->count; $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) { foreach ($row as $key => $ceil) {
if ($this->issetColumn($key) and $this->is_fillable($key)) { if ($this->issetColumn($key) and $this->is_fillable($key)) {
if ($this->beforePrintCell) { if ($this->beforePrintCell) {
$hook = $this->beforePrintCell; $hook = $this->beforePrintCell;
$ceil = $hook($key, $ceil); $ceil = $hook($key, $ceil);
} }
$this->html .= "<td>" . $ceil . "</td>"; $this->html .= "<td>" . $ceil . "</td>";
} }
} }
@ -168,17 +188,27 @@ class ListJsonTable
public function getCustomColumns($id = null): void public function getCustomColumns($id = null): void
{ {
foreach ($this->customColumnsArray as $item) { foreach ($this->customColumnsArray as $item) {
$this->html .= "<td>".$item['handler']($id)."</td>"; $this->html .= "<td>" . $item['handler']($id) . "</td>";
} }
} }
public function getCustomHeadColumn(): void public function getCustomHeadColumn(): void
{ {
foreach ($this->customColumnsArray as $item) { 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 private function issetColumn($column): bool
{ {
if (isset($this->data['meta']['columns'])) { if (isset($this->data['meta']['columns'])) {
@ -194,13 +224,18 @@ class ListJsonTable
private function issetFilter($filter): bool private function issetFilter($filter): bool
{ {
if (isset($this->data['meta']['filter'])) { if (isset($this->data['filters'])) {
foreach ($this->data['meta']['filter'] as $key => $currentFilter) { foreach ($this->data['filters'] as $key => $currentFilter) {
if ($key === $filter) { if (is_array($currentFilter)) {
return true; return false;
} elseif (is_string($currentFilter)) {
if ($currentFilter === $filter) {
return true;
}
} }
} }
} }
return false; return false;
} }