custom handler

This commit is contained in:
Билай Станислав 2024-08-02 11:44:34 +03:00
parent 8d1dce0222
commit 2c1db8573c
2 changed files with 26 additions and 36 deletions

View File

@ -6,9 +6,6 @@ use Itguild\Tables\ListJsonTable;
$json = file_get_contents('simple.json');
$table = new ListJsonTable($json);
//$table->column("status", function ($ceil){
// return getStatusLabel()[$ceil];
//});
$table->columns([
'status' => function ($ceil) {

View File

@ -2,6 +2,7 @@
namespace Itguild\Tables;
use Closure;
use Exception;
use Itguild\Tables\ActionColumn\ActionColumn;
use Itguild\Tables\ActionColumn\DeleteActionColumn;
@ -18,8 +19,7 @@ class ListJsonTable
private string $json;
private int $count = 0;
// private \Closure|false $beforePrintCell;
private array $beforePrintCell;
private \Closure|array|false $beforePrintCell;
private \Closure|false $beforePrintHook;
private \Closure|false $afterPrintHook;
private string $baseUrl;
@ -35,8 +35,8 @@ class ListJsonTable
#[NoReturn] public function __construct(string $json)
{
// $this->beforePrintCell = false;
$this->beforePrintCell = [];
$this->beforePrintCell = false;
// $this->beforePrintCellArr = [];
$this->json = $json;
$this->data = json_decode($this->json, true);
$this->baseUrl = $this->data['meta']['baseUrl'] ?? '';
@ -108,21 +108,6 @@ class ListJsonTable
public function createTbody(): void
{
if ($this->data['data']) {
// if($this->filter)
// {
// foreach ($this->data['meta']['columns'] as $col) {
// $this->html .= "<tr><td>";
// foreach ($this->data['meta']['filters'] as $filter) {
// if ($this->issetFilter($filter))
// {
// $filters = new Filter($filter, )
// }
// }
//
//
// }
// }
$this->count = $this->data["meta"]["perPage"] * ($this->data['meta']["currentPage"] - 1);
foreach ($this->data['data'] as $row) {
$this->html .= "<tr>";
@ -133,22 +118,13 @@ class ListJsonTable
}
foreach ($row as $key => $ceil) {
if ($this->issetColumn($key) and $this->is_fillable($key)) {
foreach ($this->beforePrintCell as $column => $closure) {
// if ($this->beforePrintCell) {
// $hook = $this->beforePrintCell;
// $ceil = $hook($key, $ceil);
// }
if ($key == $column) {
$hook = $closure;
$ceil = $hook($ceil);
}
if($this->beforePrintCell) {
$ceil = $this->setCustomHandler($this->beforePrintCell, $key, $ceil);
}
$this->html .= "<td>" . $ceil . "</td>";
}
}
$this->getCustomColumns($row["id"] ?? null);
if ($this->showActionColumn) {
if (isset($row["id"])) {
$actions = $this->getActions($row["id"]);
@ -323,15 +299,32 @@ class ListJsonTable
echo $this->html;
}
public function setBeforePrintCell(string $col, \Closure $closure): void
public function setBeforePrintCell(\Closure $closure): void
{
$this->beforePrintCell[$col] = $closure;
$this->beforePrintCell = $closure;
}
public function columns(array $data): void
{
foreach ($data as $key => $value) {
$this->setBeforePrintCell($key, $value);
$this->beforePrintCell[$key] = $value;
}
}
public function setCustomHandler(Closure|array $handler, string $key, string $ceil)
{
if (is_array($handler)) {
foreach ($this->beforePrintCell as $column => $closure) {
if ($key == $column) {
$hook = $closure;
$ceil = $hook($ceil);
}
}
} else {
$hook = $this->beforePrintCell;
$ceil = $hook($key, $ceil);
}
return $ceil;
}
}