view custom ceil

This commit is contained in:
Билай Станислав 2024-08-02 13:26:32 +03:00
parent 6d5f6a8665
commit 67a3f5770e
3 changed files with 37 additions and 14 deletions

View File

@ -7,5 +7,13 @@ use Itguild\Tables\ViewJsonTable;
$json = file_get_contents('view.json'); $json = file_get_contents('view.json');
$table = new ViewJsonTable($json); $table = new ViewJsonTable($json);
$table->rows([
'username' => function ($ceil) {
return "<span style='color: aqua'>$ceil</span>";
},
'email' => function ($ceil) {
return "<span style='color: firebrick'>$ceil</span>";
}
]);
$table->create(); $table->create();
$table->render(); $table->render();

View File

@ -118,7 +118,7 @@ class ListJsonTable
} }
foreach ($row as $key => $ceil) { foreach ($row as $key => $ceil) {
if ($this->issetColumn($key) and $this->is_fillable($key)) { if ($this->issetColumn($key) and $this->is_fillable($key)) {
if($this->beforePrintCell) { if($this->beforePrintCell and $ceil) {
$ceil = $this->getCustomCeil($key, $ceil); $ceil = $this->getCustomCeil($key, $ceil);
} }
$this->html .= "<td>" . $ceil . "</td>"; $this->html .= "<td>" . $ceil . "</td>";

View File

@ -13,7 +13,7 @@ class ViewJsonTable
private string $html = ""; private string $html = "";
private string $json; private string $json;
private \Closure|false $beforePrintCell; private \Closure|array|false $beforePrintCell;
private \Closure|false $beforePrintTable; private \Closure|false $beforePrintTable;
private \Closure|false $afterPrintTable; private \Closure|false $afterPrintTable;
@ -26,8 +26,6 @@ class ViewJsonTable
$this->dataJson = $this->data['data']; $this->dataJson = $this->data['data'];
} }
public function beginTable(): void public function beginTable(): void
{ {
$paramsStr = $this->createParams($this->data['meta']['params']); $paramsStr = $this->createParams($this->data['meta']['params']);
@ -44,20 +42,14 @@ class ViewJsonTable
{ {
foreach ($this->data['meta']['rows'] as $key => $row){ foreach ($this->data['meta']['rows'] as $key => $row){
if ($this->issetRow($key)){ if ($this->issetRow($key)){
if (isset($this->beforePrintCell)){ if ($this->beforePrintCell and $this->dataJson[$key]) {
$hook = $this->beforePrintCell; $this->dataJson[$key] = $this->getCustomCeil($key, $this->dataJson[$key]);
$this->dataJson[$key] = $hook($key, $this->dataJson[$key]);
} }
$this->html .= "<tr><th>" . $row . "</th><td>" . $this->dataJson[$key] . "</td></tr>";
$this->html .= "<tr><th>" . $row . ": </th><td>" . $this->dataJson[$key] . "</td></tr>";
} }
} }
return $this->html; return $this->html;
} }
private function issetRow($column): bool private function issetRow($column): bool
{ {
@ -89,7 +81,6 @@ class ViewJsonTable
$this->endTable(); $this->endTable();
} }
public function beforeTable(\Closure $closure): void public function beforeTable(\Closure $closure): void
{ {
$this->beforePrintTable = $closure; $this->beforePrintTable = $closure;
@ -109,4 +100,28 @@ class ViewJsonTable
{ {
$this->beforePrintCell = $closure; $this->beforePrintCell = $closure;
} }
public function rows(array $data): void
{
foreach ($data as $key => $value) {
$this->beforePrintCell[$key] = $value;
}
}
public function getCustomCeil(string $key, string $ceil)
{
if (is_array($this->beforePrintCell)) {
foreach ($this->beforePrintCell as $row => $closure) {
if ($key == $row) {
$hook = $closure;
$ceil = $hook($ceil);
}
}
} else {
$hook = $this->beforePrintCell;
$ceil = $hook($key, $ceil);
}
return $ceil;
}
} }