custom column
This commit is contained in:
parent
8bc8a75a53
commit
320e1070ba
@ -6,23 +6,28 @@
|
||||
"description": "Описание 1",
|
||||
"description2": "Описание 2"
|
||||
},
|
||||
"actions": ["view"],
|
||||
"pagination": true,
|
||||
"perPage": "5",
|
||||
"currentPage": "1",
|
||||
"showActionColumn": true,
|
||||
"total": 10,
|
||||
"prefix": "/page",
|
||||
"paginationPrefix": "/page",
|
||||
"params": {"class": "table table-bordered", "border": "1"}
|
||||
},
|
||||
"filters": [
|
||||
"id", "email"
|
||||
],
|
||||
"data": [
|
||||
{"email":"fas1@mail.ru","description":"sdgsdfg","description2":"ffdghdas"},
|
||||
{"email":"fas2@mail.ru","description":"fafdgdfgsdfdfs","description2":"ffdghdas"},
|
||||
{"email":"fas3@mail.ru","description":"fafdgdssdfgdfs","description2":"ffdghdas"},
|
||||
{"email":"fas4@mail.ru","description":"fafddsfgsdvcbgdfs","description2":"ffdghdas"},
|
||||
{"email":"fas5@mail.ru","description":"fafdgghjgfdfs","description2":"ffdghdas"},
|
||||
{"email":"fas6@mail.ru","description":"fafddfgdhvgdfs","description2":"ffdghdas"},
|
||||
{"email":"fas7@mail.ru","description":"fafdfgnfdgdfs","description2":"ffdghdas"},
|
||||
{"email":"fas8@mail.ru","description":"fafdfghdfgdfs","description2":"ffdghdas"},
|
||||
{"email":"dfdfd@mail.ru","description":"sdffhdfhggsdfg","description2":"ffdgdfgsdfghdas"},
|
||||
{"email":"dfsdfsddfd@mail.ru","description":"sdffhdfsdfshggsdfg","description2":"ffdgdsdffgsdfghdas"}
|
||||
{"id": 1,"email":"fas1@mail.ru","description":"sdgsdfg","description2":"ffdghdas"},
|
||||
{"id": 2,"email":"fas2@mail.ru","description":"fafdgdfgsdfdfs","description2":"ffdghdas"},
|
||||
{"id": 3,"email":"fas3@mail.ru","description":"fafdgdssdfgdfs","description2":"ffdghdas"},
|
||||
{"id": 4,"email":"fas4@mail.ru","description":"fafddsfgsdvcbgdfs","description2":"ffdghdas"},
|
||||
{"id": 5,"email":"fas5@mail.ru","description":"fafdgghjgfdfs","description2":"ffdghdas"},
|
||||
{"id": 6,"email":"fas6@mail.ru","description":"fafddfgdhvgdfs","description2":"ffdghdas"},
|
||||
{"id": 7,"email":"fas7@mail.ru","description":"fafdfgnfdgdfs","description2":"ffdghdas"},
|
||||
{"id": 8,"email":"fas8@mail.ru","description":"fafdfghdfgdfs","description2":"ffdghdas"},
|
||||
{"id": 12,"email":"dfdfd@mail.ru","description":"sdffhdfhggsdfg","description2":"ffdgdfgsdfghdas"},
|
||||
{"id": 13,"email":"dfsdfsddfd@mail.ru","description":"sdffhdfsdfshggsdfg","description2":"ffdgdsdffgsdfghdas"}
|
||||
]
|
||||
}
|
@ -12,5 +12,11 @@ $table->setBeforePrintCell(function ($key, $data) {
|
||||
$table->afterPrint(function ($meta) {
|
||||
return "<div>After Print</div>";
|
||||
});
|
||||
$table->addColumn("Колонка 33", "k33", function ($id){
|
||||
return "my ID: " . $id;
|
||||
});
|
||||
$table->addColumn("Колонка 34", "k34", function ($id){
|
||||
return "some34";
|
||||
});
|
||||
$table->create();
|
||||
$table->render();
|
@ -25,6 +25,7 @@ class ListJsonTable
|
||||
private array $data;
|
||||
|
||||
private bool $pagination = true;
|
||||
private bool $showActionColumn = true;
|
||||
|
||||
private array $actionsArray = [];
|
||||
private array $customActionsArray = [];
|
||||
@ -37,6 +38,7 @@ class ListJsonTable
|
||||
$this->data = json_decode($this->json, true);
|
||||
$this->baseUrl = $this->data['meta']['baseUrl'] ?? '';
|
||||
$this->pagination = $this->data['meta']['pagination'] ?? true;
|
||||
$this->showActionColumn = $this->data['meta']['showActionColumn'] ?? true;
|
||||
$this->beforePrintHook = function () {
|
||||
};
|
||||
$this->afterPrintHook = function () {
|
||||
@ -74,30 +76,34 @@ class ListJsonTable
|
||||
$this->html .= "<th>" . $column . "</th>";
|
||||
}
|
||||
}
|
||||
$this->getCustomHeadColumn();
|
||||
if ($this->showActionColumn) {
|
||||
$this->html .= "<th>Действия</th></th></tr></thead>";
|
||||
}
|
||||
}
|
||||
|
||||
public function createTbody(): void
|
||||
{
|
||||
if ($this->data['data']) {
|
||||
|
||||
$this->count = $this->data["meta"]["perPage"] * ($this->data['meta']["currentPage"] - 1);
|
||||
foreach ($this->data['data'] as $col) {
|
||||
foreach ($this->data['data'] as $row) {
|
||||
$this->html .= "<tr>";
|
||||
$this->count += 1;
|
||||
$id = $col["id"] ?? $this->count;
|
||||
$id = $row["id"] ?? $this->count;
|
||||
$this->html .= '<td><a href=' . $this->baseUrl . "/" . $id . '>' . $id . '</a></td>';
|
||||
foreach ($col as $key => $row) {
|
||||
foreach ($row as $key => $ceil) {
|
||||
if ($this->issetColumn($key) and $this->is_fillable($key)) {
|
||||
if ($this->beforePrintCell) {
|
||||
$hook = $this->beforePrintCell;
|
||||
$row = $hook($key, $row);
|
||||
$ceil = $hook($key, $ceil);
|
||||
}
|
||||
|
||||
$this->html .= "<td>" . $row . "</td>";
|
||||
$this->html .= "<td>" . $ceil . "</td>";
|
||||
}
|
||||
}
|
||||
|
||||
$this->getCustomColumns($row["id"] ?? null);
|
||||
// if (isset($col['id'])) {
|
||||
// $newColumn = $this->getColumns($col['id']);
|
||||
//
|
||||
@ -106,24 +112,28 @@ class ListJsonTable
|
||||
// else {
|
||||
// $this->html .= "<td></td></tr>";
|
||||
// }
|
||||
|
||||
if (isset($col["id"])){
|
||||
$actions = $this->getActions($col["id"]);
|
||||
if ($this->showActionColumn) {
|
||||
if (isset($row["id"])) {
|
||||
$actions = $this->getActions($row["id"]);
|
||||
|
||||
$this->html .= "<td>$actions</td></tr>";
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
$this->html .= "<td></td></tr>";
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public function addAction(string $actionColumn): void
|
||||
{
|
||||
$this->customActionsArray[] = $actionColumn;
|
||||
}
|
||||
|
||||
public function addColumn(string $label, string $key, \Closure $closure): void
|
||||
{
|
||||
$this->customColumnsArray[] = ['label' => $label, "key" => $key, "handler" => $closure];
|
||||
}
|
||||
|
||||
|
||||
private function setActions(): void
|
||||
@ -146,6 +156,20 @@ class ListJsonTable
|
||||
$this->actionsArray = array_merge($this->actionsArray, $this->customActionsArray);
|
||||
}
|
||||
|
||||
public function getCustomColumns($id = null): void
|
||||
{
|
||||
foreach ($this->customColumnsArray as $item) {
|
||||
$this->html .= "<td>".$item['handler']($id)."</td>";
|
||||
}
|
||||
}
|
||||
|
||||
public function getCustomHeadColumn(): void
|
||||
{
|
||||
foreach ($this->customColumnsArray as $item) {
|
||||
$this->html .= "<th>".$item['label']."</th>";
|
||||
}
|
||||
}
|
||||
|
||||
private function issetColumn($column): bool
|
||||
{
|
||||
if (isset($this->data['meta']['columns'])) {
|
||||
@ -208,7 +232,6 @@ class ListJsonTable
|
||||
|
||||
public function tableAction($json): void
|
||||
{
|
||||
|
||||
$tableJson = json_decode($json, true);
|
||||
|
||||
foreach ($tableJson as $key => $value) {
|
||||
@ -228,13 +251,13 @@ class ListJsonTable
|
||||
$hookAfter = $this->afterPrintHook;
|
||||
$this->html .= $hookAfter($this->data['meta']);
|
||||
|
||||
if ($this->pagination){
|
||||
if ($this->pagination) {
|
||||
$options = [
|
||||
'countItem' => $this->data['meta']['total'],
|
||||
'perPage' => $this->data['meta']['perPage'],
|
||||
'currentPage' => $this->data['meta']['currentPage'],
|
||||
'baseUrl' => $this->baseUrl,
|
||||
'prefix' => $this->data['meta']['prefix'],
|
||||
'prefix' => $this->data['meta']['paginationPrefix'],
|
||||
];
|
||||
$pagination = new Pagination($options);
|
||||
$pagination->create();
|
||||
|
@ -22,7 +22,7 @@ class Pagination
|
||||
/**
|
||||
* @throws Exception
|
||||
*/
|
||||
public function __construct(array $options,)
|
||||
public function __construct(array $options)
|
||||
{
|
||||
if (!$options['countItem']) {
|
||||
throw new Exception(message: "countItem is not valid");
|
||||
@ -30,7 +30,7 @@ class Pagination
|
||||
$this->countItem = $options['countItem'];
|
||||
$this->perPage = $options['perPage'] ?? 10;
|
||||
$this->currentPage = $options['currentPage'] ?? 1;
|
||||
$this->baseUrl = $options['baseUrl'];
|
||||
$this->baseUrl = $options['baseUrl'] ?? '';
|
||||
$this->baseUrl .= $options['prefix'] ?? '/page';
|
||||
|
||||
$this->countPages = ceil($this->countItem / $this->perPage);
|
||||
|
Loading…
Reference in New Issue
Block a user