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

View File

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

View File

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

View File

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

View File

@ -35,7 +35,7 @@ class ViewJsonTable extends JasonTable
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 ($this->beforePrintCell and $this->dataJson[$key] !== null) { 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>"; $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 public function rows(array $data): void
// { {
// foreach ($data as $key => $value) { foreach ($data as $key => $value) {
// $this->beforePrintCell[$key] = $value; $this->beforePrintCell[$key] = $value;
// } }
// } }
} }