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": {
"title": "forma 1",
"columns": {
"id": "ID",
"email": "Email",
"description": "Описание 1",
"description2": "Описание 2"
"description2": "Описание 2",
"status": "Статус"
},
"actions": ["view"],
"pagination": true,
@ -17,18 +19,18 @@
"params": {"class": "table table-bordered", "border": "1"}
},
"filters": [
"id", "email"
"email", "description"
],
"data": [
{"id": 1,"email":"fas1@mail.ru","description":"sdgsdfg","description2":"ffdghdas"},
{"id": 2,"email":"fas2@mail.ru","description":"fafdgdfgsdfdfs","description2":"ffdghdas"},
{"id": 3,"email":"fas3@mail.ru","description":"fafdgdssdfgdfs","description2":"ffdghdas"},
{"id": 4,"email":"fas4@mail.ru","description":"fafddsfgsdvcbgdfs","description2":"ffdghdas"},
{"id": 5,"email":"fas5@mail.ru","description":"fafdgghjgfdfs","description2":"ffdghdas"},
{"id": 6,"email":"fas6@mail.ru","description":"fafddfgdhvgdfs","description2":"ffdghdas"},
{"id": 7,"email":"fas7@mail.ru","description":"fafdfgnfdgdfs","description2":"ffdghdas"},
{"id": 8,"email":"fas8@mail.ru","description":"fafdfghdfgdfs","description2":"ffdghdas"},
{"id": 12,"email":"dfdfd@mail.ru","description":"sdffhdfhggsdfg","description2":"ffdgdfgsdfghdas"},
{"id": 13,"email":"dfsdfsddfd@mail.ru","description":"sdffhdfsdfshggsdfg","description2":"ffdgdsdffgsdfghdas"}
{"id": 1,"email":"fas1@mail.ru","description":"sdgsdfg","description2":"ffdghdas", "status": 1},
{"id": 2,"email":"fas2@mail.ru","description":"fafdgdfgsdfdfs","description2":"ffdghdas", "status": 1},
{"id": 3,"email":"fas3@mail.ru","description":"fafdgdssdfgdfs","description2":"ffdghdas", "status": 2},
{"id": 4,"email":"fas4@mail.ru","description":"fafddsfgsdvcbgdfs","description2":"ffdghdas", "status": 1},
{"id": 5,"email":"fas5@mail.ru","description":"fafdgghjgfdfs","description2":"ffdghdas", "status": 1},
{"id": 6,"email":"fas6@mail.ru","description":"fafddfgdhvgdfs","description2":"ffdghdas", "status": 1},
{"id": 7,"email":"fas7@mail.ru","description":"fafdfgnfdgdfs","description2":"ffdghdas", "status": 0},
{"id": 8,"email":"fas8@mail.ru","description":"fafdfghdfgdfs","description2":"ffdghdas", "status": 1},
{"id": 12,"email":"dfdfd@mail.ru","description":"sdffhdfhggsdfg","description2":"ffdgdfgsdfghdas", "status": 99},
{"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');
$table = new ListJsonTable($json);
$table->setBeforePrintCell(function ($key, $data) {
return $key == "email" ? "<span style='color: aqua'>$data</span>" : $data;
});
//$table->column("status", function ($ceil){
// 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) {
return "<div>After Print</div>";
});
$table->addColumn("Колонка 33", "k33", function ($id){
$table->addColumn("Колонка 33", "k33", function ($id) {
return "my ID: " . $id;
});
$table->addColumn("Колонка 34", "k34", function ($id){
$table->addColumn("Колонка 34", "k34", function ($id) {
return "some34";
});
$table->create();
$table->render();
function getStatusLabel(): array
{
return [
0 => "На модерации",
1 => "Активный",
2 => "Модератор",
99 => "Удален",
];
}

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;
}