formats and style

This commit is contained in:
Kavalar 2024-08-08 17:08:46 +03:00
parent d0f136e484
commit 38d5deb36a
6 changed files with 50 additions and 15 deletions

View File

@ -26,7 +26,7 @@
{"id": 1,"email":"fas1@mail.ru","description":"sdgsdfg","description2":"ffdghdas", "created_at": "17.06.2024", "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": 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": 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": 4,"email":"fas4@mail.ru","description":"fafd <b>dsfgsd</b> vcbgdfs","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": 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": 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": 7,"email":"fas7@mail.ru","description":"fafdfgnfdgdfs","description2":"ffdghdas", "created_at": "23.06.2024", "status": 0},

View File

@ -12,8 +12,12 @@ $table->columns([
"created_at" => [ "created_at" => [
"format" => "date:Y-m-d", "format" => "date:Y-m-d",
], ],
'description2' => [ 'description' => [
"format" => "html:", "format" => "html",
"style" => ["width" => "300px"],
"value" => function ($cell) {
return "<span style='color: sienna'>$cell</span>";
}
], ],
'status' => function ($cell) { 'status' => function ($cell) {
return getStatusLabel()[$cell]; return getStatusLabel()[$cell];
@ -21,9 +25,6 @@ $table->columns([
'email' => function ($cell) { 'email' => function ($cell) {
return "<span style='color: aqua'>$cell</span>"; return "<span style='color: aqua'>$cell</span>";
}, },
'description' => function ($cell) {
return "<span style='color: sienna'>$cell</span>";
}
]); ]);
//$table->setBeforePrintCell(function ($key, $data) { //$table->setBeforePrintCell(function ($key, $data) {
// return $key == "email" ? "<span style='color: aqua'>$data</span>" : $data; // return $key == "email" ? "<span style='color: aqua'>$data</span>" : $data;

View File

@ -25,15 +25,16 @@ class JasonTable
if (is_array($this->beforePrintCell)) { if (is_array($this->beforePrintCell)) {
if (isset($this->beforePrintCell[$key])) { if (isset($this->beforePrintCell[$key])) {
if (is_array($this->beforePrintCell[$key])){ if (is_array($this->beforePrintCell[$key])){
if (!isset($this->beforePrintCell[$key]['format'])){
$this->beforePrintCell[$key]['format'] = "text";
}
$format = explode(":", $this->beforePrintCell[$key]['format']);
$formatClass = FormatMapper::getFormat()[$format[0]];
$cell = $formatClass::fetch($cell, $format[1] ?? "");
if (isset($this->beforePrintCell[$key]['value'])){ if (isset($this->beforePrintCell[$key]['value'])){
$hook = $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 { else {
$hook = $this->beforePrintCell[$key]; $hook = $this->beforePrintCell[$key];
@ -58,4 +59,35 @@ class JasonTable
{ {
$this->beforePrintHook = $closure; $this->beforePrintHook = $closure;
} }
protected function getParamFromCustomColumn(string $column, string $paramName)
{
if (is_array($this->beforePrintCell)) {
if (isset($this->beforePrintCell[$column])) {
if (is_array($this->beforePrintCell[$column])){
if (isset($this->beforePrintCell[$column][$paramName])){
return $this->beforePrintCell[$column][$paramName];
}
}
}
}
return null;
}
protected function getStyleFromCustomColumn(string $column)
{
$styleStr = '';
$style = $this->getParamFromCustomColumn($column, "style");
if (is_array($style)){
foreach ($style as $key => $value){
$styleStr .= $key . ": " . $value . ";";
}
}
else {
$styleStr = $style;
}
return $styleStr;
}
} }

View File

@ -91,7 +91,7 @@ class ListJsonTable extends JasonTable
if($this->beforePrintCell) { if($this->beforePrintCell) {
$cell = $this->getCustomCell($key, $cell); $cell = $this->getCustomCell($key, $cell);
} }
$this->html .= "<td>" . $cell . "</td>"; $this->html .= "<td style='" . $this->getStyleFromCustomColumn($key) . "'>" . $cell . "</td>";
} }
} }
$this->getCustomColumns($row["id"] ?? null); $this->getCustomColumns($row["id"] ?? null);
@ -163,6 +163,8 @@ class ListJsonTable extends JasonTable
return $keys; return $keys;
} }
private function getColumnKeys(): array private function getColumnKeys(): array
{ {
$columnKeys = []; $columnKeys = [];

View File

@ -7,8 +7,8 @@ use Itguild\Tables\formats\BaseFormat;
class HtmlFormat extends BaseFormat class HtmlFormat extends BaseFormat
{ {
static function fetch(?string $data, string $options = "") static function fetch(?string $data, string $options = ""): string
{ {
return; return (string)$data;
} }
} }

View File

@ -7,7 +7,7 @@ class TextFormat extends BaseFormat
public static function fetch($data, string $options = ""): string public static function fetch($data, string $options = ""): string
{ {
return (string)$data; return strip_tags((string)$data);
} }
} }