This commit is contained in:
Билай Станислав 2024-08-06 12:10:26 +03:00
parent f53346610e
commit 71b2fdfb6b
5 changed files with 65 additions and 69 deletions

View File

@ -8,15 +8,15 @@ $json = file_get_contents('simple.json');
$table = new ListJsonTable($json);
//$table->columns([
$table->setBeforePrintCell([
'status' => function ($ceil) {
return getStatusLabel()[$ceil];
$table->columns([
'status' => function ($cell) {
return getStatusLabel()[$cell];
},
'email' => function ($ceil) {
return "<span style='color: aqua'>$ceil</span>";
'email' => function ($cell) {
return "<span style='color: aqua'>$cell</span>";
},
'description' => function ($ceil) {
return "<span style='color: sienna'>$ceil</span>";
'description' => function ($cell) {
return "<span style='color: sienna'>$cell</span>";
}
]);
//$table->setBeforePrintCell(function ($key, $data) {

View File

@ -8,12 +8,12 @@ $json = file_get_contents('view.json');
$table = new ViewJsonTable($json);
//$table->rows([
$table->setBeforePrintCell([
'username' => function ($ceil) {
return "<span style='color: aqua'>$ceil</span>";
$table->rows([
'username' => function ($cell) {
return "<span style='color: aqua'>$cell</span>";
},
'email' => function ($ceil) {
return "<span style='color: firebrick'>$ceil</span>";
'email' => function ($cell) {
return "<span style='color: firebrick'>$cell</span>";
}
]);
$table->create();

View File

@ -2,7 +2,7 @@
namespace Itguild\Tables;
abstract class JasonTable
class JasonTable
{
protected string $html = "";
protected \Closure|array|false $beforePrintCell;
@ -15,37 +15,26 @@ abstract class JasonTable
echo $this->html;
}
// public function setBeforePrintCell(\Closure $closure): void
// {
// $this->beforePrintCell = $closure;
// }
public function setBeforePrintCell(\Closure|array $data): void
public function setBeforePrintCell(\Closure $closure): void
{
if(is_array($data)) {
foreach ($data as $key => $value) {
$this->beforePrintCell[$key] = $value;
}
} else {
$this->beforePrintCell = $data;
}
$this->beforePrintCell = $closure;
}
public function getCustomCeil(string $key, string $ceil)
public function getCustomCell(string $key, string $cell)
{
if (is_array($this->beforePrintCell)) {
foreach ($this->beforePrintCell as $_key => $closure) {
if ($key == $_key) {
foreach ($this->beforePrintCell as $currentKey => $closure) {
if ($key == $currentKey) {
$hook = $closure;
$ceil = $hook($ceil);
$cell = $hook($cell);
}
}
} else {
$hook = $this->beforePrintCell;
$ceil = $hook($key, $ceil);
$cell = $hook($key, $cell);
}
return $ceil;
return $cell;
}
public function afterPrint(\Closure $closure): void

View File

@ -2,9 +2,7 @@
namespace Itguild\Tables;
use Closure;
use Exception;
use Itguild\Tables\ActionColumn\ActionColumn;
use Itguild\Tables\ActionColumn\DeleteActionColumn;
use Itguild\Tables\ActionColumn\EditActionColumn;
use Itguild\Tables\ActionColumn\ViewActionColumn;
@ -32,7 +30,6 @@ class ListJsonTable extends JasonTable
#[NoReturn] public function __construct(string $json)
{
$this->beforePrintCell = false;
// $this->beforePrintCellArr = [];
$this->json = $json;
$this->data = json_decode($this->json, true);
$this->baseUrl = $this->data['meta']['baseUrl'] ?? '';
@ -65,29 +62,14 @@ class ListJsonTable extends JasonTable
$this->html .= "<th>" . "ID" . "</th>";
$columnKeys[] = "id";
}
foreach ($this->data['meta']['columns'] as $key => $column) {
if ($this->is_fillable($key)) {
$this->html .= "<th>" . $column . "</th>";
$columnKeys[] = $key;
}
}
$columnKeys = $this->getColumnKeys($columnKeys);
$columnKeys = array_merge($columnKeys, $this->getCustomColumnKeys());
$this->getCustomHeadColumn();
if ($this->showActionColumn) {
$this->html .= "<th>Действия</th></th></tr></thead>";
}
if ($this->filters) {
$this->html .= "<tr><form action='$this->baseUrl/search'>";
foreach ($columnKeys as $key){
if ($this->issetFilter($key)){
$this->html .= "<td><input type='text' name='$key'></td>";
}
else {
$this->html .= "<td></td>";
}
}
$this->html .= "</form></tr>";
$this->getFilters($columnKeys);
}
}
@ -102,12 +84,12 @@ class ListJsonTable extends JasonTable
if (!$this->issetColumn("id")) {
$this->html .= '<td><a href=' . $this->baseUrl . "/" . $id . '>' . $id . '</a></td>';
}
foreach ($row as $key => $ceil) {
foreach ($row as $key => $cell) {
if ($this->issetColumn($key) and $this->is_fillable($key)) {
if($this->beforePrintCell and $ceil !== null) {
$ceil = $this->getCustomCeil($key, $ceil);
if($this->beforePrintCell and $cell !== null) {
$cell = $this->getCustomCell($key, $cell);
}
$this->html .= "<td>" . $ceil . "</td>";
$this->html .= "<td>" . $cell . "</td>";
}
}
$this->getCustomColumns($row["id"] ?? null);
@ -179,6 +161,17 @@ class ListJsonTable extends JasonTable
return $keys;
}
private function getColumnKeys(array $columnKeys): array
{
foreach ($this->data['meta']['columns'] as $key => $column) {
if ($this->is_fillable($key)) {
$this->html .= "<th>" . $column . "</th>";
$columnKeys[] = $key;
}
}
return $columnKeys;
}
private function issetColumn($column): bool
{
if (isset($this->data['meta']['columns'])) {
@ -233,6 +226,20 @@ class ListJsonTable extends JasonTable
return $actions;
}
private function getFilters(array $columnKeys): void
{
$this->html .= "<tr><form action='$this->baseUrl/search'>";
foreach ($columnKeys as $key){
if ($this->issetFilter($key)){
$this->html .= "<td><input type='text' name='$key'></td>";
}
else {
$this->html .= "<td></td>";
}
}
$this->html .= "</form></tr>";
}
/**
* @throws Exception
*/
@ -280,10 +287,10 @@ class ListJsonTable extends JasonTable
}
}
// public function columns(array $data): void
// {
// foreach ($data as $key => $value) {
// $this->beforePrintCell[$key] = $value;
// }
// }
public function columns(array $data): void
{
foreach ($data as $key => $value) {
$this->beforePrintCell[$key] = $value;
}
}
}

View File

@ -35,7 +35,7 @@ class ViewJsonTable extends JasonTable
foreach ($this->data['meta']['rows'] as $key => $row){
if ($this->issetRow($key)){
if ($this->beforePrintCell and $this->dataJson[$key] !== null) {
$this->dataJson[$key] = $this->getCustomCeil($key, $this->dataJson[$key]);
$this->dataJson[$key] = $this->getCustomCell($key, $this->dataJson[$key]);
}
$this->html .= "<tr><th>" . $row . "</th><td>" . $this->dataJson[$key] . "</td></tr>";
}
@ -74,10 +74,10 @@ class ViewJsonTable extends JasonTable
}
// public function rows(array $data): void
// {
// foreach ($data as $key => $value) {
// $this->beforePrintCell[$key] = $value;
// }
// }
public function rows(array $data): void
{
foreach ($data as $key => $value) {
$this->beforePrintCell[$key] = $value;
}
}
}