column format

This commit is contained in:
Kavalar 2024-08-08 14:21:47 +03:00
parent d98ddded07
commit 02fa193652
8 changed files with 104 additions and 15 deletions

View File

@ -4,6 +4,7 @@
"columns": {
"id": "ID",
"email": "Email",
"created_at": "Дата создания",
"description": "Описание 1",
"description2": "Описание 2",
"status": "Статус"
@ -22,15 +23,15 @@
"email", "description"
],
"data": [
{"id": 1,"email":"fas1@mail.ru","description":"sdgsdfg","description2":"ffdghdas", "status": 1},
{"id": 2,"email":"fas2@mail.ru","description":"fafdgdfgsdfdfs","description2":"ffdghdas", "status": 1},
{"id": 3,"email":"fas3@mail.ru","description":"fafdgdssdfgdfs","description2":"ffdghdas", "status": 2},
{"id": 4,"email":"fas4@mail.ru","description":"fafddsfgsdvcbgdfs","description2":"ffdghdas", "status": 1},
{"id": 5,"email":"fas5@mail.ru","description":"fafdgghjgfdfs","description2":"ffdghdas", "status": 1},
{"id": 6,"email":"fas6@mail.ru","description":"fafddfgdhvgdfs","description2":"ffdghdas", "status": 1},
{"id": 7,"email":"fas7@mail.ru","description":"fafdfgnfdgdfs","description2":"ffdghdas", "status": 0},
{"id": 8,"email":"fas8@mail.ru","description":"fafdfghdfgdfs","description2":"ffdghdas", "status": 1},
{"id": 12,"email":"dfdfd@mail.ru","description":"sdffhdfhggsdfg","description2":"ffdgdfgsdfghdas", "status": 99},
{"id": 13,"email":"dfsdfsddfd@mail.ru","description":"sdffhdfsdfshggsdfg","description2":"ffdgdsdffgsdfghdas", "status": 1}
{"id": 1,"email":"fas1@mail.ru","description":"sdgsdfg","description2":"ffdghdas", "created_at": "17.06.2024", "status": 1},
{"id": 2,"email":"fas2@mail.ru","description":"fafdgdfgsdfdfs","description2":"ffdghdas", "created_at": "18.06.2024", "status": 1},
{"id": 3,"email":"fas3@mail.ru","description":"fafdgdssdfgdfs","description2":"ffdghdas", "created_at": "19.06.2024", "status": 2},
{"id": 4,"email":"fas4@mail.ru","description":"fafddsfgsdvcbgdfs","description2":"ffdghdas", "created_at": "20.06.2024", "status": 1},
{"id": 5,"email":"fas5@mail.ru","description":"fafdgghjgfdfs","description2":"ffdghdas", "created_at": "21.06.2024", "status": 1},
{"id": 6,"email":"fas6@mail.ru","description":"fafddfgdhvgdfs","description2":"ffdghdas", "created_at": "22.06.2024", "status": 1},
{"id": 7,"email":"fas7@mail.ru","description":"fafdfgnfdgdfs","description2":"ffdghdas", "created_at": "23.06.2024", "status": 0},
{"id": 8,"email":"fas8@mail.ru","description":"fafdfghdfgdfs","description2":"ffdghdas", "created_at": "24.06.2024", "status": 1},
{"id": 12,"email":"dfdfd@mail.ru","description":"sdffhdfhggsdfg","description2":"ffdgdfgsdfghdas", "created_at": "25.06.2024", "status": 99},
{"id": 13,"email":"dfsdfsddfd@mail.ru","description":"sdffhdfsdfshggsdfg","description2":"ffdgdsdffgsdfghdas", "created_at": "25.06.2024", "status": 1}
]
}

View File

@ -9,6 +9,9 @@ $table = new ListJsonTable($json);
//$table->columns([
$table->columns([
"created_at" => [
"format" => "date:Y-m-d",
],
'status' => function ($cell) {
return getStatusLabel()[$cell];
},

19
src/FormatMapper.php Normal file
View File

@ -0,0 +1,19 @@
<?php
namespace Itguild\Tables;
use Itguild\Tables\formats\DateFormat;
use Itguild\Tables\formats\TextFormat;
class FormatMapper
{
public static function getFormat(): array
{
return [
'text' => TextFormat::class,
'date' => DateFormat::class,
];
}
}

View File

@ -23,11 +23,23 @@ class JasonTable
public function getCustomCell(string $key, string|null $cell)
{
if (is_array($this->beforePrintCell)) {
foreach ($this->beforePrintCell as $currentKey => $closure) {
if ($key == $currentKey) {
$hook = $closure;
if (isset($this->beforePrintCell[$key])) {
if (is_array($this->beforePrintCell[$key])){
if (isset($this->beforePrintCell[$key]['value'])){
$hook = $this->beforePrintCell[$key]['value'];
$cell = $hook($cell);
}
elseif (isset($this->beforePrintCell[$key]['format'])){
$format = explode(":", $this->beforePrintCell[$key]['format']);
$formatClass = FormatMapper::getFormat()[$format[0]];
$cell = $formatClass::fetch($cell, $format[1] ?? null);
}
}
else {
$hook = $this->beforePrintCell[$key];
$cell = $hook($cell);
}
}
} else {
$hook = $this->beforePrintCell;

View File

@ -62,7 +62,7 @@ class ListJsonTable extends JasonTable
$this->html .= "<th>" . "ID" . "</th>";
$columnKeys[] = "id";
}
$columnKeys = $this->getColumnKeys($columnKeys);
$columnKeys = $this->getColumnKeys();
$columnKeys = array_merge($columnKeys, $this->getCustomColumnKeys());
$this->getCustomHeadColumn();
if ($this->showActionColumn) {
@ -78,6 +78,8 @@ class ListJsonTable extends JasonTable
if ($this->data['data']) {
$this->count = $this->data["meta"]["perPage"] * ($this->data['meta']["currentPage"] - 1);
foreach ($this->data['data'] as $row) {
//сортируем колнки согласно массиву колонок из json
$row = array_merge(array_flip($this->getFillableColumns()), $row);
$this->html .= "<tr>";
$this->count += 1;
$id = $row["id"] ?? $this->count;
@ -161,8 +163,9 @@ class ListJsonTable extends JasonTable
return $keys;
}
private function getColumnKeys(array $columnKeys): array
private function getColumnKeys(): array
{
$columnKeys = [];
foreach ($this->data['meta']['columns'] as $key => $column) {
if ($this->is_fillable($key)) {
$this->html .= "<th>" . $column . "</th>";
@ -172,6 +175,17 @@ class ListJsonTable extends JasonTable
return $columnKeys;
}
private function getFillableColumns(): array
{
$columnKeys = [];
foreach ($this->data['meta']['columns'] as $key => $column) {
if ($this->is_fillable($key)) {
$columnKeys[] = $key;
}
}
return $columnKeys;
}
private function issetColumn($column): bool
{
if (isset($this->data['meta']['columns'])) {

View File

@ -0,0 +1,10 @@
<?php
namespace Itguild\Tables\formats;
abstract class BaseFormat
{
abstract static function fetch(string|null $data, string $options = "");
}

View File

@ -0,0 +1,17 @@
<?php
namespace Itguild\Tables\formats;
use DateTimeImmutable;
class DateFormat extends BaseFormat
{
/**
* @throws \Exception
*/
static function fetch(?string $data, string $options = ""): ?string
{
return (new DateTimeImmutable($data))->format($options) ?? null;
}
}

View File

@ -0,0 +1,13 @@
<?php
namespace Itguild\Tables\formats;
class TextFormat extends BaseFormat
{
public static function fetch($data, string $options = ""): string
{
return (string)$data;
}
}