Compare commits
9 Commits
Author | SHA1 | Date | |
---|---|---|---|
647fe34d11 | |||
1714afcf3f | |||
d6d703aaa6 | |||
b7fea4122c | |||
48e6d645d9 | |||
85c4a850b1 | |||
02ded1a613 | |||
c648bd37b6 | |||
cf347f1dc2 |
18
composer.lock
generated
Normal file
18
composer.lock
generated
Normal file
@ -0,0 +1,18 @@
|
|||||||
|
{
|
||||||
|
"_readme": [
|
||||||
|
"This file locks the dependencies of your project to a known state",
|
||||||
|
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
|
||||||
|
"This file is @generated automatically"
|
||||||
|
],
|
||||||
|
"content-hash": "76e2cbfc39ced89c5f5aa46d0b1474c7",
|
||||||
|
"packages": [],
|
||||||
|
"packages-dev": [],
|
||||||
|
"aliases": [],
|
||||||
|
"minimum-stability": "dev",
|
||||||
|
"stability-flags": [],
|
||||||
|
"prefer-stable": false,
|
||||||
|
"prefer-lowest": false,
|
||||||
|
"platform": [],
|
||||||
|
"platform-dev": [],
|
||||||
|
"plugin-api-version": "2.6.0"
|
||||||
|
}
|
@ -6,10 +6,22 @@
|
|||||||
"description": "Описание 1",
|
"description": "Описание 1",
|
||||||
"description2": "Описание 2"
|
"description2": "Описание 2"
|
||||||
},
|
},
|
||||||
|
"pagination": true,
|
||||||
|
"perPage": "5",
|
||||||
|
"currentPage": "1",
|
||||||
|
"total": 10,
|
||||||
"params": {"class": "table table-bordered", "border": "1"}
|
"params": {"class": "table table-bordered", "border": "1"}
|
||||||
},
|
},
|
||||||
"data": [
|
"data": [
|
||||||
{"email":"fas@mail.ru","description":"fafdgdfs","description2":"ffdghdas"},
|
{"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"}
|
||||||
]
|
]
|
||||||
}
|
}
|
@ -5,7 +5,12 @@ require_once "../vendor/autoload.php";
|
|||||||
use Itguild\Tables\ListJsonTable;
|
use Itguild\Tables\ListJsonTable;
|
||||||
|
|
||||||
$json = file_get_contents('simple.json');
|
$json = file_get_contents('simple.json');
|
||||||
|
|
||||||
$table = new ListJsonTable($json);
|
$table = new ListJsonTable($json);
|
||||||
|
$table->setBeforePrintCell(function ($key, $data) {
|
||||||
|
return $key == "email" ? "<span style='color: aqua'>$data</span>" : $data;
|
||||||
|
});
|
||||||
|
$table->afterPrint(function ($meta) {
|
||||||
|
return "<div>After Print</div>";
|
||||||
|
});
|
||||||
$table->create();
|
$table->create();
|
||||||
$table->render();
|
$table->render();
|
27
examples/view.json
Normal file
27
examples/view.json
Normal file
@ -0,0 +1,27 @@
|
|||||||
|
{
|
||||||
|
"meta": {
|
||||||
|
"rows": {
|
||||||
|
"username": "Логин",
|
||||||
|
"email": "Email",
|
||||||
|
"created_at": "Создан",
|
||||||
|
"updated_at": "Обновлен"
|
||||||
|
},
|
||||||
|
"perPage": 10,
|
||||||
|
"currentPage": 1,
|
||||||
|
"baseUrl": "\/admin\/user",
|
||||||
|
"actions": [],
|
||||||
|
"params": {
|
||||||
|
"class": "table table-bordered",
|
||||||
|
"border": "2"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"data": {
|
||||||
|
"id": 1,
|
||||||
|
"username": "rrr",
|
||||||
|
"email": "rrr@mail.ru",
|
||||||
|
"password_hash": "sdgh46eyrtghsdret",
|
||||||
|
"role": 1,
|
||||||
|
"created_at": "15.07.2024",
|
||||||
|
"updated_at": null
|
||||||
|
}
|
||||||
|
}
|
11
examples/view.php
Normal file
11
examples/view.php
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
require_once "../vendor/autoload.php";
|
||||||
|
|
||||||
|
use Itguild\Tables\ViewJsonTable;
|
||||||
|
|
||||||
|
$json = file_get_contents('view.json');
|
||||||
|
|
||||||
|
$table = new ViewJsonTable($json);
|
||||||
|
$table->create();
|
||||||
|
$table->render();
|
@ -8,8 +8,11 @@ abstract class ActionColumn
|
|||||||
protected string $prefix;
|
protected string $prefix;
|
||||||
|
|
||||||
protected int $id;
|
protected int $id;
|
||||||
public function __construct(string $baseUrl, int $id)
|
public function __construct(string $baseUrl, int $id, string $prefix = '')
|
||||||
{
|
{
|
||||||
|
if (!empty($prefix)){
|
||||||
|
$this->prefix = $prefix;
|
||||||
|
}
|
||||||
$this->baseUrl = $baseUrl;
|
$this->baseUrl = $baseUrl;
|
||||||
$this->id = $id;
|
$this->id = $id;
|
||||||
}
|
}
|
||||||
|
@ -4,7 +4,7 @@ namespace Itguild\Tables\ActionColumn;
|
|||||||
|
|
||||||
class DeleteActionColumn extends ActionColumn
|
class DeleteActionColumn extends ActionColumn
|
||||||
{
|
{
|
||||||
protected string $prefix = "/form-delete/";
|
protected string $prefix = "/delete/";
|
||||||
|
|
||||||
public function fetch(): string
|
public function fetch(): string
|
||||||
{
|
{
|
||||||
|
@ -5,7 +5,7 @@ namespace Itguild\Tables\ActionColumn;
|
|||||||
class EditActionColumn extends ActionColumn
|
class EditActionColumn extends ActionColumn
|
||||||
{
|
{
|
||||||
|
|
||||||
protected string $prefix = "/form-edit/";
|
protected string $prefix = "/edit/";
|
||||||
|
|
||||||
public function fetch(): string
|
public function fetch(): string
|
||||||
{
|
{
|
||||||
|
@ -4,7 +4,7 @@ namespace Itguild\Tables\ActionColumn;
|
|||||||
|
|
||||||
class ViewActionColumn extends ActionColumn
|
class ViewActionColumn extends ActionColumn
|
||||||
{
|
{
|
||||||
protected string $prefix = "/form-item/";
|
protected string $prefix = "/view/";
|
||||||
|
|
||||||
public function fetch(): string
|
public function fetch(): string
|
||||||
{
|
{
|
||||||
|
@ -2,10 +2,12 @@
|
|||||||
|
|
||||||
namespace Itguild\Tables;
|
namespace Itguild\Tables;
|
||||||
|
|
||||||
|
use Itguild\Tables\ActionColumn\ActionColumn;
|
||||||
use Itguild\Tables\ActionColumn\DeleteActionColumn;
|
use Itguild\Tables\ActionColumn\DeleteActionColumn;
|
||||||
use Itguild\Tables\ActionColumn\EditActionColumn;
|
use Itguild\Tables\ActionColumn\EditActionColumn;
|
||||||
use Itguild\Tables\ActionColumn\ViewActionColumn;
|
use Itguild\Tables\ActionColumn\ViewActionColumn;
|
||||||
use Itguild\Tables\traits\CreateParams;
|
use Itguild\Tables\traits\CreateParams;
|
||||||
|
use JetBrains\PhpStorm\NoReturn;
|
||||||
|
|
||||||
class ListJsonTable
|
class ListJsonTable
|
||||||
{
|
{
|
||||||
@ -16,34 +18,45 @@ class ListJsonTable
|
|||||||
|
|
||||||
private int $count = 0;
|
private int $count = 0;
|
||||||
private \Closure|false $beforePrintCell;
|
private \Closure|false $beforePrintCell;
|
||||||
private \Closure|false $beforePrint;
|
private \Closure|false $beforePrintHook;
|
||||||
|
private \Closure|false $afterPrintHook;
|
||||||
private string $baseUrl;
|
private string $baseUrl;
|
||||||
private array $data;
|
private array $data;
|
||||||
|
|
||||||
private array $actionsArray = [];
|
private bool $pagination = true;
|
||||||
|
|
||||||
public function __construct(string $json)
|
private array $actionsArray = [];
|
||||||
|
private array $customActionsArray = [];
|
||||||
|
|
||||||
|
#[NoReturn] public function __construct(string $json)
|
||||||
{
|
{
|
||||||
$this->beforePrintCell = false;
|
$this->beforePrintCell = false;
|
||||||
$this->beforePrint = function () {
|
|
||||||
};
|
|
||||||
$this->json = $json;
|
$this->json = $json;
|
||||||
$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->setActions();
|
$this->pagination = $this->data['meta']['pagination'] ?? true;
|
||||||
|
$this->beforePrintHook = function () {
|
||||||
|
};
|
||||||
|
$this->afterPrintHook = function () {
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
public function beginTable(): void
|
public function beginTable(): void
|
||||||
{
|
{
|
||||||
$paramsStr = $this->createParams($this->data['meta']['params']);
|
$paramsStr = $this->createParams($this->data['meta']['params']);
|
||||||
$hook = $this->beforePrint;
|
$hookBefore = $this->beforePrintHook;
|
||||||
$this->html .= $hook();
|
$this->html .= $hookBefore($this->data['meta']);
|
||||||
$this->html .= "<table $paramsStr>";
|
$this->html .= "<table $paramsStr>";
|
||||||
}
|
}
|
||||||
|
|
||||||
public function beforePrint(\Closure $closure): void
|
public function beforePrint(\Closure $closure): void
|
||||||
{
|
{
|
||||||
$this->beforePrint = $closure;
|
$this->beforePrintHook = $closure;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function afterPrint(\Closure $closure): void
|
||||||
|
{
|
||||||
|
$this->afterPrintHook = $closure;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function createThead(): void
|
public function createThead(): void
|
||||||
@ -70,7 +83,8 @@ class ListJsonTable
|
|||||||
foreach ($this->data['data'] as $col) {
|
foreach ($this->data['data'] as $col) {
|
||||||
$this->html .= "<tr>";
|
$this->html .= "<tr>";
|
||||||
$this->count += 1;
|
$this->count += 1;
|
||||||
$this->html .= '<td><a href=' . $this->baseUrl . "/" . $col["id"] . '>' . $this->count . '</a></td>';
|
$id = $col["id"] ?? $this->count;
|
||||||
|
$this->html .= '<td><a href=' . $this->baseUrl . "/" . $id . '>' . $id . '</a></td>';
|
||||||
foreach ($col as $key => $row) {
|
foreach ($col as $key => $row) {
|
||||||
if ($this->issetColumn($key) and $this->is_fillable($key)) {
|
if ($this->issetColumn($key) and $this->is_fillable($key)) {
|
||||||
if ($this->beforePrintCell) {
|
if ($this->beforePrintCell) {
|
||||||
@ -82,12 +96,22 @@ class ListJsonTable
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (isset($col["id"])){
|
||||||
$actions = $this->getActions($col["id"]);
|
$actions = $this->getActions($col["id"]);
|
||||||
|
|
||||||
$this->html .= "<td>$actions</td></tr>";
|
$this->html .= "<td>$actions</td></tr>";
|
||||||
}
|
}
|
||||||
|
else {
|
||||||
|
$this->html .= "<td></td></tr>";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public function addAction(string $actionColumn): void
|
||||||
|
{
|
||||||
|
$this->customActionsArray[] = $actionColumn;
|
||||||
|
}
|
||||||
|
|
||||||
private function setActions(): void
|
private function setActions(): void
|
||||||
{
|
{
|
||||||
@ -106,9 +130,10 @@ class ListJsonTable
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
$this->actionsArray = array_merge($this->actionsArray, $this->customActionsArray);
|
||||||
}
|
}
|
||||||
|
|
||||||
private function issetColumn($column)
|
private function issetColumn($column): bool
|
||||||
{
|
{
|
||||||
if (isset($this->data['meta']['columns'])) {
|
if (isset($this->data['meta']['columns'])) {
|
||||||
foreach ($this->data['meta']['columns'] as $key => $currentColumn) {
|
foreach ($this->data['meta']['columns'] as $key => $currentColumn) {
|
||||||
@ -145,8 +170,9 @@ class ListJsonTable
|
|||||||
return $actions;
|
return $actions;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function create()
|
public function create(): void
|
||||||
{
|
{
|
||||||
|
$this->setActions();
|
||||||
$this->beginTable();
|
$this->beginTable();
|
||||||
$this->createThead();
|
$this->createThead();
|
||||||
$this->createTbody();
|
$this->createTbody();
|
||||||
@ -169,6 +195,19 @@ class ListJsonTable
|
|||||||
public function endTable(): void
|
public function endTable(): void
|
||||||
{
|
{
|
||||||
$this->html .= "</table>";
|
$this->html .= "</table>";
|
||||||
|
$hookAfter = $this->afterPrintHook;
|
||||||
|
$this->html .= $hookAfter($this->data['meta']);
|
||||||
|
|
||||||
|
if ($this->pagination){
|
||||||
|
$pagination = new Pagination(
|
||||||
|
$this->data['meta']['total'],
|
||||||
|
$this->data['meta']['perPage'],
|
||||||
|
$this->data['meta']['currentPage'],
|
||||||
|
$this->baseUrl
|
||||||
|
);
|
||||||
|
$pagination->create();
|
||||||
|
$this->html .= $pagination->fetch();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public function render(): void
|
public function render(): void
|
||||||
|
@ -15,16 +15,17 @@ class Pagination
|
|||||||
private string $baseUrl;
|
private string $baseUrl;
|
||||||
|
|
||||||
public function __construct($countItem, $perPage, $currentPage, $baseUrl)
|
public function __construct($countItem, $perPage, $currentPage, $baseUrl)
|
||||||
|
// public function __construct(int $countItem, array $params, string $baseUrl)
|
||||||
{
|
{
|
||||||
$this->countItem = $countItem;
|
$this->countItem = $countItem;
|
||||||
$this->perPage = $perPage;
|
$this->perPage = $perPage;
|
||||||
$this->currentPage = $currentPage;
|
$this->currentPage = $currentPage;
|
||||||
$this->baseUrl = $baseUrl;
|
$this->baseUrl = $baseUrl;
|
||||||
|
|
||||||
$this->countPages = ceil($this->countItem / $perPage);
|
$this->countPages = ceil($this->countItem / $this->perPage);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function create()
|
public function create(): void
|
||||||
{
|
{
|
||||||
$prev = $this->currentPage - 1 >= 1 ? $this->currentPage - 1 : null;
|
$prev = $this->currentPage - 1 >= 1 ? $this->currentPage - 1 : null;
|
||||||
$next = $this->currentPage + 1 <= $this->countPages ? $this->currentPage + 1 : null;
|
$next = $this->currentPage + 1 <= $this->countPages ? $this->currentPage + 1 : null;
|
||||||
@ -42,12 +43,17 @@ class Pagination
|
|||||||
echo $this->html;
|
echo $this->html;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function fetch()
|
public function fetch(): string
|
||||||
{
|
{
|
||||||
return $this->html;
|
return $this->html;
|
||||||
}
|
}
|
||||||
|
|
||||||
private function getTemplate()
|
public function hookBefore(): void
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
private function getTemplate(): string
|
||||||
{
|
{
|
||||||
return '<nav aria-label="Page navigation example">
|
return '<nav aria-label="Page navigation example">
|
||||||
<ul class="pagination">
|
<ul class="pagination">
|
||||||
|
@ -23,7 +23,7 @@ class ViewJsonTable
|
|||||||
{
|
{
|
||||||
$this->json = $json;
|
$this->json = $json;
|
||||||
$this->data = json_decode($this->json, true);
|
$this->data = json_decode($this->json, true);
|
||||||
$this->dataJson = json_decode($this->data['data']['data'], true);
|
$this->dataJson = $this->data['data'];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -31,20 +31,25 @@ class ViewJsonTable
|
|||||||
public function beginTable(): void
|
public function beginTable(): void
|
||||||
{
|
{
|
||||||
$paramsStr = $this->createParams($this->data['meta']['params']);
|
$paramsStr = $this->createParams($this->data['meta']['params']);
|
||||||
|
|
||||||
|
//Хук перед выводом ячейки
|
||||||
|
if (isset($this->beforePrintTable)){
|
||||||
$hook = $this->beforePrintTable;
|
$hook = $this->beforePrintTable;
|
||||||
$this->html = $hook();
|
$this->html = $hook();
|
||||||
|
}
|
||||||
|
|
||||||
$this->html .= "<table $paramsStr>";
|
$this->html .= "<table $paramsStr>";
|
||||||
}
|
}
|
||||||
public function createColum(): string
|
public function createRows(): string
|
||||||
{
|
{
|
||||||
foreach ($this->data['meta']['columns'] as $key => $column){
|
foreach ($this->data['meta']['rows'] as $key => $row){
|
||||||
if ($this->issetColumn($key)){
|
if ($this->issetRow($key)){
|
||||||
if ($this->beforePrintCell){
|
if (isset($this->beforePrintCell)){
|
||||||
$hook = $this->beforePrintCell;
|
$hook = $this->beforePrintCell;
|
||||||
$this->dataJson[$key] = $hook($key, $this->dataJson[$key]);
|
$this->dataJson[$key] = $hook($key, $this->dataJson[$key]);
|
||||||
}
|
}
|
||||||
|
|
||||||
$this->html .= "<tr><th>" . $column . ": </th><td>" . $this->dataJson[$key] . "</td></tr>";
|
$this->html .= "<tr><th>" . $row . ": </th><td>" . $this->dataJson[$key] . "</td></tr>";
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -53,11 +58,11 @@ class ViewJsonTable
|
|||||||
return $this->html;
|
return $this->html;
|
||||||
}
|
}
|
||||||
|
|
||||||
private function issetColumn($column)
|
private function issetRow($column): bool
|
||||||
{
|
{
|
||||||
|
|
||||||
if (isset($this->data['meta']['columns'])){
|
if (isset($this->data['meta']['rows'])){
|
||||||
foreach ($this->data['meta']['columns'] as $key => $currentColumn){
|
foreach ($this->data['meta']['rows'] as $key => $currentColumn){
|
||||||
if ($key === $column){
|
if ($key === $column){
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
@ -67,18 +72,20 @@ class ViewJsonTable
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function endTable()
|
public function endTable(): void
|
||||||
{
|
{
|
||||||
$this->html .= "</table>";
|
$this->html .= "</table>";
|
||||||
|
|
||||||
|
if(isset($this->afterPrintTable)){
|
||||||
$hookAfter = $this->afterPrintTable;
|
$hookAfter = $this->afterPrintTable;
|
||||||
$this->html .= $hookAfter();
|
$this->html .= $hookAfter();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public function create()
|
public function create(): void
|
||||||
{
|
{
|
||||||
$this->beginTable();
|
$this->beginTable();
|
||||||
$this->createColum();
|
$this->createRows();
|
||||||
$this->endTable();
|
$this->endTable();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -93,11 +100,9 @@ class ViewJsonTable
|
|||||||
$this->afterPrintTable = $closure;
|
$this->afterPrintTable = $closure;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function render()
|
public function render(): void
|
||||||
{
|
{
|
||||||
|
|
||||||
echo $this->html;
|
echo $this->html;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public function setBeforePrintCell(\Closure $closure): void
|
public function setBeforePrintCell(\Closure $closure): void
|
||||||
|
Reference in New Issue
Block a user