update view json table

This commit is contained in:
Kavalar 2024-07-23 15:17:16 +03:00
parent 85c4a850b1
commit 48e6d645d9
3 changed files with 62 additions and 19 deletions

27
examples/view.json Normal file
View File

@ -0,0 +1,27 @@
{
"meta": {
"rows": {
"username": "Логин",
"email": "Email",
"created_at": "Создан",
"updated_at": "Обновлен"
},
"perPage": 10,
"currentPage": 1,
"baseUrl": "\/admin\/user",
"actions": [],
"params": {
"class": "table table-bordered",
"border": "2"
}
},
"data": {
"id": 1,
"username": "rrr",
"email": "rrr@mail.ru",
"password_hash": "sdgh46eyrtghsdret",
"role": 1,
"created_at": "15.07.2024",
"updated_at": null
}
}

11
examples/view.php Normal file
View File

@ -0,0 +1,11 @@
<?php
require_once "../vendor/autoload.php";
use Itguild\Tables\ViewJsonTable;
$json = file_get_contents('view.json');
$table = new ViewJsonTable($json);
$table->create();
$table->render();

View File

@ -23,7 +23,7 @@ class ViewJsonTable
{ {
$this->json = $json; $this->json = $json;
$this->data = json_decode($this->json, true); $this->data = json_decode($this->json, true);
$this->dataJson = json_decode($this->data['data']['data'], true); $this->dataJson = $this->data['data'];
} }
@ -31,20 +31,25 @@ class ViewJsonTable
public function beginTable(): void public function beginTable(): void
{ {
$paramsStr = $this->createParams($this->data['meta']['params']); $paramsStr = $this->createParams($this->data['meta']['params']);
$hook = $this->beforePrintTable;
$this->html = $hook(); //Хук перед выводом ячейки
if (isset($this->beforePrintTable)){
$hook = $this->beforePrintTable;
$this->html = $hook();
}
$this->html .= "<table $paramsStr>"; $this->html .= "<table $paramsStr>";
} }
public function createColum(): string public function createRows(): string
{ {
foreach ($this->data['meta']['columns'] as $key => $column){ foreach ($this->data['meta']['rows'] as $key => $row){
if ($this->issetColumn($key)){ if ($this->issetRow($key)){
if ($this->beforePrintCell){ if (isset($this->beforePrintCell)){
$hook = $this->beforePrintCell; $hook = $this->beforePrintCell;
$this->dataJson[$key] = $hook($key, $this->dataJson[$key]); $this->dataJson[$key] = $hook($key, $this->dataJson[$key]);
} }
$this->html .= "<tr><th>" . $column . ": </th><td>" . $this->dataJson[$key] . "</td></tr>"; $this->html .= "<tr><th>" . $row . ": </th><td>" . $this->dataJson[$key] . "</td></tr>";
} }
} }
@ -53,11 +58,11 @@ class ViewJsonTable
return $this->html; return $this->html;
} }
private function issetColumn($column) private function issetRow($column): bool
{ {
if (isset($this->data['meta']['columns'])){ if (isset($this->data['meta']['rows'])){
foreach ($this->data['meta']['columns'] as $key => $currentColumn){ foreach ($this->data['meta']['rows'] as $key => $currentColumn){
if ($key === $column){ if ($key === $column){
return true; return true;
} }
@ -67,18 +72,20 @@ class ViewJsonTable
return false; return false;
} }
public function endTable() public function endTable(): void
{ {
$this->html .= "</table>"; $this->html .= "</table>";
$hookAfter = $this->afterPrintTable;
$this->html .= $hookAfter();
if(isset($this->afterPrintTable)){
$hookAfter = $this->afterPrintTable;
$this->html .= $hookAfter();
}
} }
public function create() public function create(): void
{ {
$this->beginTable(); $this->beginTable();
$this->createColum(); $this->createRows();
$this->endTable(); $this->endTable();
} }
@ -93,11 +100,9 @@ class ViewJsonTable
$this->afterPrintTable = $closure; $this->afterPrintTable = $closure;
} }
public function render() public function render(): void
{ {
echo $this->html; echo $this->html;
} }
public function setBeforePrintCell(\Closure $closure): void public function setBeforePrintCell(\Closure $closure): void