From 8d1dce02227ba3de028788c981ca09e2b46c8224 Mon Sep 17 00:00:00 2001 From: stasbilay02 Date: Thu, 1 Aug 2024 15:10:41 +0300 Subject: [PATCH] columns --- examples/simple.php | 20 ++++++++++++-------- src/ListJsonTable.php | 42 +++++++++++++++++++++++------------------- 2 files changed, 35 insertions(+), 27 deletions(-) diff --git a/examples/simple.php b/examples/simple.php index 88e15d7..3efe225 100644 --- a/examples/simple.php +++ b/examples/simple.php @@ -9,14 +9,18 @@ $table = new ListJsonTable($json); //$table->column("status", function ($ceil){ // return getStatusLabel()[$ceil]; //}); -//$table->columns([ -// 'status' => function ($ceil) { -// return getStatusLabel()[$ceil]; -// }, -// 'email' => function ($ceil) { -// return "$ceil"; -// } -//]); + +$table->columns([ + 'status' => function ($ceil) { + return getStatusLabel()[$ceil]; + }, + 'email' => function ($ceil) { + return "$ceil"; + }, + 'description' => function ($ceil) { + return "$ceil"; + } +]); //$table->setBeforePrintCell(function ($key, $data) { // return $key == "email" ? "$data" : $data; //}); diff --git a/src/ListJsonTable.php b/src/ListJsonTable.php index b054122..90512ff 100644 --- a/src/ListJsonTable.php +++ b/src/ListJsonTable.php @@ -18,7 +18,8 @@ class ListJsonTable private string $json; private int $count = 0; - private \Closure|false $beforePrintCell; +// private \Closure|false $beforePrintCell; + private array $beforePrintCell; private \Closure|false $beforePrintHook; private \Closure|false $afterPrintHook; private string $baseUrl; @@ -34,7 +35,8 @@ class ListJsonTable #[NoReturn] public function __construct(string $json) { - $this->beforePrintCell = false; +// $this->beforePrintCell = false; + $this->beforePrintCell = []; $this->json = $json; $this->data = json_decode($this->json, true); $this->baseUrl = $this->data['meta']['baseUrl'] ?? ''; @@ -131,9 +133,15 @@ class ListJsonTable } foreach ($row as $key => $ceil) { if ($this->issetColumn($key) and $this->is_fillable($key)) { - if ($this->beforePrintCell) { - $hook = $this->beforePrintCell; - $ceil = $hook($key, $ceil); + foreach ($this->beforePrintCell as $column => $closure) { +// if ($this->beforePrintCell) { +// $hook = $this->beforePrintCell; +// $ceil = $hook($key, $ceil); +// } + if ($key == $column) { + $hook = $closure; + $ceil = $hook($ceil); + } } $this->html .= "" . $ceil . ""; } @@ -199,7 +207,7 @@ class ListJsonTable } } - protected function getCustomColumnKeys() + protected function getCustomColumnKeys(): array { $keys = []; foreach ($this->customColumnsArray as $item) { @@ -252,17 +260,6 @@ class ListJsonTable return false; } -// private function getColumns(int $id): string -// { -// $columns = []; -// foreach ($this->actionsArray as $item) { -// $objItem = new $item($this->baseUrl, $id); -// $columns .= $objItem->fetch(); -// } -// -// return $columns; -// } - private function getActions(int $id): string { $actions = ""; @@ -326,8 +323,15 @@ class ListJsonTable echo $this->html; } - public function setBeforePrintCell(\Closure $closure): void + public function setBeforePrintCell(string $col, \Closure $closure): void { - $this->beforePrintCell = $closure; + $this->beforePrintCell[$col] = $closure; + } + + public function columns(array $data): void + { + foreach ($data as $key => $value) { + $this->setBeforePrintCell($key, $value); + } } } \ No newline at end of file