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