add hook afterPrint

This commit is contained in:
Kavalar 2024-07-30 12:20:16 +03:00
parent 48e6d645d9
commit b7fea4122c
3 changed files with 41 additions and 13 deletions

View File

@ -9,7 +9,14 @@
"params": {"class": "table table-bordered", "border": "1"} "params": {"class": "table table-bordered", "border": "1"}
}, },
"data": [ "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"}
] ]
} }

View File

@ -1,11 +1,16 @@
<?php <?php
require_once "../vendor/autoload.php"; require_once "../vendor/autoload.php";
use Itguild\Tables\ListJsonTable; 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) {
return $key == "email" ? "<span style='color: aqua'>$data</span>" : $data;
});
$table->afterPrint(function () {
return "<div>After Print</div>";
});
$table->create(); $table->create();
$table->render(); $table->render();

View File

@ -7,6 +7,7 @@ use Itguild\Tables\ActionColumn\DeleteActionColumn;
use Itguild\Tables\ActionColumn\EditActionColumn; use Itguild\Tables\ActionColumn\EditActionColumn;
use Itguild\Tables\ActionColumn\ViewActionColumn; use Itguild\Tables\ActionColumn\ViewActionColumn;
use Itguild\Tables\traits\CreateParams; use Itguild\Tables\traits\CreateParams;
use JetBrains\PhpStorm\NoReturn;
class ListJsonTable class ListJsonTable
{ {
@ -17,34 +18,42 @@ class ListJsonTable
private int $count = 0; private int $count = 0;
private \Closure|false $beforePrintCell; private \Closure|false $beforePrintCell;
private \Closure|false $beforePrint; private \Closure|false $beforePrintHook;
private \Closure|false $afterPrintHook;
private string $baseUrl; private string $baseUrl;
private array $data; private array $data;
private array $actionsArray = []; private array $actionsArray = [];
private array $customActionsArray = []; private array $customActionsArray = [];
public function __construct(string $json) #[NoReturn] public function __construct(string $json)
{ {
$this->beforePrintCell = false; $this->beforePrintCell = false;
$this->beforePrint = function () {
};
$this->json = $json; $this->json = $json;
$this->data = json_decode($this->json, true); $this->data = json_decode($this->json, true);
$this->baseUrl = $this->data['meta']['baseUrl'] ?? ''; $this->baseUrl = $this->data['meta']['baseUrl'] ?? '';
$this->beforePrintHook = function () {
};
$this->afterPrintHook = function () {
};
} }
public function beginTable(): void public function beginTable(): void
{ {
$paramsStr = $this->createParams($this->data['meta']['params']); $paramsStr = $this->createParams($this->data['meta']['params']);
$hook = $this->beforePrint; $hookBefore = $this->beforePrintHook;
$this->html .= $hook(); $this->html .= $hookBefore();
$this->html .= "<table $paramsStr>"; $this->html .= "<table $paramsStr>";
} }
public function beforePrint(\Closure $closure): void 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 public function createThead(): void
@ -83,9 +92,14 @@ class ListJsonTable
} }
} }
$actions = $this->getActions($col["id"]); if (isset($col["id"])){
$actions = $this->getActions($col["id"]);
$this->html .= "<td>$actions</td></tr>"; $this->html .= "<td>$actions</td></tr>";
}
else {
$this->html .= "<td></td></tr>";
}
} }
} }
} }
@ -177,6 +191,8 @@ class ListJsonTable
public function endTable(): void public function endTable(): void
{ {
$this->html .= "</table>"; $this->html .= "</table>";
$hookAfter = $this->afterPrintHook;
$this->html .= $hookAfter();
} }
public function render(): void public function render(): void