This commit is contained in:
Билай Станислав 2024-08-01 15:10:41 +03:00
parent 433cc85f54
commit 8d1dce0222
2 changed files with 35 additions and 27 deletions

View File

@ -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 "<span style='color: aqua'>$ceil</span>";
// }
//]);
$table->columns([
'status' => function ($ceil) {
return getStatusLabel()[$ceil];
},
'email' => function ($ceil) {
return "<span style='color: aqua'>$ceil</span>";
},
'description' => function ($ceil) {
return "<span style='color: sienna'>$ceil</span>";
}
]);
//$table->setBeforePrintCell(function ($key, $data) {
// return $key == "email" ? "<span style='color: aqua'>$data</span>" : $data;
//});

View File

@ -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 .= "<td>" . $ceil . "</td>";
}
@ -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);
}
}
}