From 2c1db8573ca3796fb3855e5bf76e7d119fbb7825 Mon Sep 17 00:00:00 2001 From: stasbilay02 Date: Fri, 2 Aug 2024 11:44:34 +0300 Subject: [PATCH] custom handler --- examples/simple.php | 3 --- src/ListJsonTable.php | 59 +++++++++++++++++++------------------------ 2 files changed, 26 insertions(+), 36 deletions(-) diff --git a/examples/simple.php b/examples/simple.php index 3efe225..66520f1 100644 --- a/examples/simple.php +++ b/examples/simple.php @@ -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) { diff --git a/src/ListJsonTable.php b/src/ListJsonTable.php index 90512ff..10960c7 100644 --- a/src/ListJsonTable.php +++ b/src/ListJsonTable.php @@ -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 .= ""; -// 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 .= ""; @@ -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 .= "" . $ceil . ""; } } - $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; + + } } \ No newline at end of file