Compare commits
4 Commits
Author | SHA1 | Date | |
---|---|---|---|
b7fea4122c | |||
48e6d645d9 | |||
85c4a850b1 | |||
02ded1a613 |
@ -9,7 +9,14 @@
|
||||
"params": {"class": "table table-bordered", "border": "1"}
|
||||
},
|
||||
"data": [
|
||||
{"email":"fas@mail.ru","description":"fafdgdfs","description2":"ffdghdas"},
|
||||
|
||||
{"email":"fas1@mail.ru","description":"sdgsdfg","description2":"ffdghdas"},
|
||||
{"email":"fas2@mail.ru","description":"fafdgdfgsdfdfs","description2":"ffdghdas"},
|
||||
{"email":"fas3@mail.ru","description":"fafdgdssdfgdfs","description2":"ffdghdas"},
|
||||
{"email":"fas4@mail.ru","description":"fafddsfgsdvcbgdfs","description2":"ffdghdas"},
|
||||
{"email":"fas5@mail.ru","description":"fafdgghjgfdfs","description2":"ffdghdas"},
|
||||
{"email":"fas6@mail.ru","description":"fafddfgdhvgdfs","description2":"ffdghdas"},
|
||||
{"email":"fas7@mail.ru","description":"fafdfgnfdgdfs","description2":"ffdghdas"},
|
||||
{"email":"fas8@mail.ru","description":"fafdfghdfgdfs","description2":"ffdghdas"},
|
||||
{"email":"dfdfd@mail.ru","description":"sdffhdfhggsdfg","description2":"ffdgdfgsdfghdas"}
|
||||
]
|
||||
}
|
@ -5,7 +5,12 @@ require_once "../vendor/autoload.php";
|
||||
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->afterPrint(function () {
|
||||
return "<div>After Print</div>";
|
||||
});
|
||||
$table->create();
|
||||
$table->render();
|
27
examples/view.json
Normal file
27
examples/view.json
Normal 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
11
examples/view.php
Normal 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();
|
@ -7,6 +7,7 @@ use Itguild\Tables\ActionColumn\DeleteActionColumn;
|
||||
use Itguild\Tables\ActionColumn\EditActionColumn;
|
||||
use Itguild\Tables\ActionColumn\ViewActionColumn;
|
||||
use Itguild\Tables\traits\CreateParams;
|
||||
use JetBrains\PhpStorm\NoReturn;
|
||||
|
||||
class ListJsonTable
|
||||
{
|
||||
@ -17,35 +18,42 @@ class ListJsonTable
|
||||
|
||||
private int $count = 0;
|
||||
private \Closure|false $beforePrintCell;
|
||||
private \Closure|false $beforePrint;
|
||||
private \Closure|false $beforePrintHook;
|
||||
private \Closure|false $afterPrintHook;
|
||||
private string $baseUrl;
|
||||
private array $data;
|
||||
|
||||
private array $actionsArray = [];
|
||||
private array $customActionsArray = [];
|
||||
|
||||
public function __construct(string $json)
|
||||
#[NoReturn] public function __construct(string $json)
|
||||
{
|
||||
$this->beforePrintCell = false;
|
||||
$this->beforePrint = function () {
|
||||
};
|
||||
$this->json = $json;
|
||||
$this->data = json_decode($this->json, true);
|
||||
$this->baseUrl = $this->data['meta']['baseUrl'] ?? '';
|
||||
$this->setActions();
|
||||
$this->beforePrintHook = function () {
|
||||
};
|
||||
$this->afterPrintHook = function () {
|
||||
};
|
||||
}
|
||||
|
||||
public function beginTable(): void
|
||||
{
|
||||
$paramsStr = $this->createParams($this->data['meta']['params']);
|
||||
$hook = $this->beforePrint;
|
||||
$this->html .= $hook();
|
||||
$hookBefore = $this->beforePrintHook;
|
||||
$this->html .= $hookBefore();
|
||||
$this->html .= "<table $paramsStr>";
|
||||
}
|
||||
|
||||
public function beforePrint(\Closure $closure): void
|
||||
{
|
||||
$this->beforePrint = $closure;
|
||||
$this->beforePrintHook = $closure;
|
||||
}
|
||||
|
||||
public function afterPrint(\Closure $closure): void
|
||||
{
|
||||
$this->afterPrintHook = $closure;
|
||||
}
|
||||
|
||||
public function createThead(): void
|
||||
@ -84,14 +92,19 @@ class ListJsonTable
|
||||
}
|
||||
}
|
||||
|
||||
if (isset($col["id"])){
|
||||
$actions = $this->getActions($col["id"]);
|
||||
|
||||
$this->html .= "<td>$actions</td></tr>";
|
||||
}
|
||||
else {
|
||||
$this->html .= "<td></td></tr>";
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public function addAction(ActionColumn $actionColumn): void
|
||||
public function addAction(string $actionColumn): void
|
||||
{
|
||||
$this->customActionsArray[] = $actionColumn;
|
||||
}
|
||||
@ -153,8 +166,9 @@ class ListJsonTable
|
||||
return $actions;
|
||||
}
|
||||
|
||||
public function create()
|
||||
public function create(): void
|
||||
{
|
||||
$this->setActions();
|
||||
$this->beginTable();
|
||||
$this->createThead();
|
||||
$this->createTbody();
|
||||
@ -177,6 +191,8 @@ class ListJsonTable
|
||||
public function endTable(): void
|
||||
{
|
||||
$this->html .= "</table>";
|
||||
$hookAfter = $this->afterPrintHook;
|
||||
$this->html .= $hookAfter();
|
||||
}
|
||||
|
||||
public function render(): void
|
||||
|
@ -23,7 +23,7 @@ class ViewJsonTable
|
||||
{
|
||||
$this->json = $json;
|
||||
$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
|
||||
{
|
||||
$paramsStr = $this->createParams($this->data['meta']['params']);
|
||||
|
||||
//Хук перед выводом ячейки
|
||||
if (isset($this->beforePrintTable)){
|
||||
$hook = $this->beforePrintTable;
|
||||
$this->html = $hook();
|
||||
}
|
||||
|
||||
$this->html .= "<table $paramsStr>";
|
||||
}
|
||||
public function createColum(): string
|
||||
public function createRows(): string
|
||||
{
|
||||
foreach ($this->data['meta']['columns'] as $key => $column){
|
||||
if ($this->issetColumn($key)){
|
||||
if ($this->beforePrintCell){
|
||||
foreach ($this->data['meta']['rows'] as $key => $row){
|
||||
if ($this->issetRow($key)){
|
||||
if (isset($this->beforePrintCell)){
|
||||
$hook = $this->beforePrintCell;
|
||||
$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;
|
||||
}
|
||||
|
||||
private function issetColumn($column)
|
||||
private function issetRow($column): bool
|
||||
{
|
||||
|
||||
if (isset($this->data['meta']['columns'])){
|
||||
foreach ($this->data['meta']['columns'] as $key => $currentColumn){
|
||||
if (isset($this->data['meta']['rows'])){
|
||||
foreach ($this->data['meta']['rows'] as $key => $currentColumn){
|
||||
if ($key === $column){
|
||||
return true;
|
||||
}
|
||||
@ -67,18 +72,20 @@ class ViewJsonTable
|
||||
return false;
|
||||
}
|
||||
|
||||
public function endTable()
|
||||
public function endTable(): void
|
||||
{
|
||||
$this->html .= "</table>";
|
||||
|
||||
if(isset($this->afterPrintTable)){
|
||||
$hookAfter = $this->afterPrintTable;
|
||||
$this->html .= $hookAfter();
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
public function create()
|
||||
public function create(): void
|
||||
{
|
||||
$this->beginTable();
|
||||
$this->createColum();
|
||||
$this->createRows();
|
||||
$this->endTable();
|
||||
}
|
||||
|
||||
@ -93,11 +100,9 @@ class ViewJsonTable
|
||||
$this->afterPrintTable = $closure;
|
||||
}
|
||||
|
||||
public function render()
|
||||
public function render(): void
|
||||
{
|
||||
|
||||
echo $this->html;
|
||||
|
||||
}
|
||||
|
||||
public function setBeforePrintCell(\Closure $closure): void
|
||||
|
Reference in New Issue
Block a user