column format
This commit is contained in:
parent
d98ddded07
commit
02fa193652
@ -4,6 +4,7 @@
|
|||||||
"columns": {
|
"columns": {
|
||||||
"id": "ID",
|
"id": "ID",
|
||||||
"email": "Email",
|
"email": "Email",
|
||||||
|
"created_at": "Дата создания",
|
||||||
"description": "Описание 1",
|
"description": "Описание 1",
|
||||||
"description2": "Описание 2",
|
"description2": "Описание 2",
|
||||||
"status": "Статус"
|
"status": "Статус"
|
||||||
@ -22,15 +23,15 @@
|
|||||||
"email", "description"
|
"email", "description"
|
||||||
],
|
],
|
||||||
"data": [
|
"data": [
|
||||||
{"id": 1,"email":"fas1@mail.ru","description":"sdgsdfg","description2":"ffdghdas", "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", "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", "status": 2},
|
{"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", "status": 1},
|
{"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", "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", "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", "status": 0},
|
{"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", "status": 1},
|
{"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", "status": 99},
|
{"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", "status": 1}
|
{"id": 13,"email":"dfsdfsddfd@mail.ru","description":"sdffhdfsdfshggsdfg","description2":"ffdgdsdffgsdfghdas", "created_at": "25.06.2024", "status": 1}
|
||||||
]
|
]
|
||||||
}
|
}
|
@ -9,6 +9,9 @@ $table = new ListJsonTable($json);
|
|||||||
|
|
||||||
//$table->columns([
|
//$table->columns([
|
||||||
$table->columns([
|
$table->columns([
|
||||||
|
"created_at" => [
|
||||||
|
"format" => "date:Y-m-d",
|
||||||
|
],
|
||||||
'status' => function ($cell) {
|
'status' => function ($cell) {
|
||||||
return getStatusLabel()[$cell];
|
return getStatusLabel()[$cell];
|
||||||
},
|
},
|
||||||
|
19
src/FormatMapper.php
Normal file
19
src/FormatMapper.php
Normal 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,
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -23,11 +23,23 @@ class JasonTable
|
|||||||
public function getCustomCell(string $key, string|null $cell)
|
public function getCustomCell(string $key, string|null $cell)
|
||||||
{
|
{
|
||||||
if (is_array($this->beforePrintCell)) {
|
if (is_array($this->beforePrintCell)) {
|
||||||
foreach ($this->beforePrintCell as $currentKey => $closure) {
|
if (isset($this->beforePrintCell[$key])) {
|
||||||
if ($key == $currentKey) {
|
if (is_array($this->beforePrintCell[$key])){
|
||||||
$hook = $closure;
|
if (isset($this->beforePrintCell[$key]['value'])){
|
||||||
|
$hook = $this->beforePrintCell[$key]['value'];
|
||||||
$cell = $hook($cell);
|
$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 {
|
} else {
|
||||||
$hook = $this->beforePrintCell;
|
$hook = $this->beforePrintCell;
|
||||||
|
@ -62,7 +62,7 @@ class ListJsonTable extends JasonTable
|
|||||||
$this->html .= "<th>" . "ID" . "</th>";
|
$this->html .= "<th>" . "ID" . "</th>";
|
||||||
$columnKeys[] = "id";
|
$columnKeys[] = "id";
|
||||||
}
|
}
|
||||||
$columnKeys = $this->getColumnKeys($columnKeys);
|
$columnKeys = $this->getColumnKeys();
|
||||||
$columnKeys = array_merge($columnKeys, $this->getCustomColumnKeys());
|
$columnKeys = array_merge($columnKeys, $this->getCustomColumnKeys());
|
||||||
$this->getCustomHeadColumn();
|
$this->getCustomHeadColumn();
|
||||||
if ($this->showActionColumn) {
|
if ($this->showActionColumn) {
|
||||||
@ -78,6 +78,8 @@ class ListJsonTable extends JasonTable
|
|||||||
if ($this->data['data']) {
|
if ($this->data['data']) {
|
||||||
$this->count = $this->data["meta"]["perPage"] * ($this->data['meta']["currentPage"] - 1);
|
$this->count = $this->data["meta"]["perPage"] * ($this->data['meta']["currentPage"] - 1);
|
||||||
foreach ($this->data['data'] as $row) {
|
foreach ($this->data['data'] as $row) {
|
||||||
|
//сортируем колнки согласно массиву колонок из json
|
||||||
|
$row = array_merge(array_flip($this->getFillableColumns()), $row);
|
||||||
$this->html .= "<tr>";
|
$this->html .= "<tr>";
|
||||||
$this->count += 1;
|
$this->count += 1;
|
||||||
$id = $row["id"] ?? $this->count;
|
$id = $row["id"] ?? $this->count;
|
||||||
@ -161,8 +163,9 @@ class ListJsonTable extends JasonTable
|
|||||||
return $keys;
|
return $keys;
|
||||||
}
|
}
|
||||||
|
|
||||||
private function getColumnKeys(array $columnKeys): array
|
private function getColumnKeys(): array
|
||||||
{
|
{
|
||||||
|
$columnKeys = [];
|
||||||
foreach ($this->data['meta']['columns'] as $key => $column) {
|
foreach ($this->data['meta']['columns'] as $key => $column) {
|
||||||
if ($this->is_fillable($key)) {
|
if ($this->is_fillable($key)) {
|
||||||
$this->html .= "<th>" . $column . "</th>";
|
$this->html .= "<th>" . $column . "</th>";
|
||||||
@ -172,6 +175,17 @@ class ListJsonTable extends JasonTable
|
|||||||
return $columnKeys;
|
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
|
private function issetColumn($column): bool
|
||||||
{
|
{
|
||||||
if (isset($this->data['meta']['columns'])) {
|
if (isset($this->data['meta']['columns'])) {
|
||||||
|
10
src/formats/BaseFormat.php
Normal file
10
src/formats/BaseFormat.php
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Itguild\Tables\formats;
|
||||||
|
|
||||||
|
abstract class BaseFormat
|
||||||
|
{
|
||||||
|
|
||||||
|
abstract static function fetch(string|null $data, string $options = "");
|
||||||
|
|
||||||
|
}
|
17
src/formats/DateFormat.php
Normal file
17
src/formats/DateFormat.php
Normal 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;
|
||||||
|
}
|
||||||
|
}
|
13
src/formats/TextFormat.php
Normal file
13
src/formats/TextFormat.php
Normal 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;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user