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

@ -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;
}
}
}