8 Commits
0.1 ... 0.1.6

Author SHA1 Message Date
48e6d645d9 update view json table 2024-07-23 15:17:16 +03:00
85c4a850b1 set action 2024-07-16 13:26:29 +03:00
02ded1a613 some fix 2024-07-16 13:20:48 +03:00
c648bd37b6 edit class column 2024-07-16 13:12:40 +03:00
cf347f1dc2 custom ation column 2024-07-15 17:19:30 +03:00
4bdd883d67 fix to psr4 2024-07-15 16:25:03 +03:00
f9fc6b9f17 tag 2024-07-15 15:54:11 +03:00
338555d62c fix link 2024-07-15 15:49:12 +03:00
12 changed files with 85 additions and 31 deletions

27
examples/view.json Normal file
View 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
View 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();

View File

@ -8,8 +8,11 @@ abstract class ActionColumn
protected string $prefix;
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->id = $id;
}

View File

@ -4,7 +4,7 @@ namespace Itguild\Tables\ActionColumn;
class DeleteActionColumn extends ActionColumn
{
protected string $prefix = "/form-delete/";
protected string $prefix = "/delete/";
public function fetch(): string
{

View File

@ -5,7 +5,7 @@ namespace Itguild\Tables\ActionColumn;
class EditActionColumn extends ActionColumn
{
protected string $prefix = "/form-edit/";
protected string $prefix = "/edit/";
public function fetch(): string
{

View File

@ -4,7 +4,7 @@ namespace Itguild\Tables\ActionColumn;
class ViewActionColumn extends ActionColumn
{
protected string $prefix = "/form-item/";
protected string $prefix = "/view/";
public function fetch(): string
{

View File

@ -2,6 +2,7 @@
namespace Itguild\Tables;
use Itguild\Tables\ActionColumn\ActionColumn;
use Itguild\Tables\ActionColumn\DeleteActionColumn;
use Itguild\Tables\ActionColumn\EditActionColumn;
use Itguild\Tables\ActionColumn\ViewActionColumn;
@ -21,15 +22,16 @@ class ListJsonTable
private array $data;
private array $actionsArray = [];
private array $customActionsArray = [];
public function __construct(string $json)
{
$this->beforePrintCell = false;
$this->beforePrint = function (){};
$this->beforePrint = function () {
};
$this->json = $json;
$this->data = json_decode($this->json, true);
$this->baseUrl = $this->data['meta']['baseUrl'] ?? '';
$this->setActions();
}
public function beginTable(): void
@ -69,7 +71,7 @@ class ListJsonTable
foreach ($this->data['data'] as $col) {
$this->html .= "<tr>";
$this->count += 1;
$this->html .= '<td><a href=' . $this->baseUrl . "/form-item/" . $col["id"] . '>' . $this->count . '</a></td>';
$this->html .= '<td><a href=' . $this->baseUrl . "/" . $col["id"] . '>' . $this->count . '</a></td>';
foreach ($col as $key => $row) {
if ($this->issetColumn($key) and $this->is_fillable($key)) {
if ($this->beforePrintCell) {
@ -88,6 +90,11 @@ class ListJsonTable
}
}
public function addAction(string $actionColumn): void
{
$this->customActionsArray[] = $actionColumn;
}
private function setActions(): void
{
if (isset($this->data['meta']['actions'])) {
@ -105,9 +112,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'])) {
foreach ($this->data['meta']['columns'] as $key => $currentColumn) {
@ -122,12 +130,11 @@ class ListJsonTable
private function is_fillable($column): bool
{
if (isset($this->data['meta']['fillable'])){
if (isset($this->data['meta']['fillable'])) {
if (in_array($column, $this->data['meta']['fillable'])) {
return true;
}
}
else{
} else {
return true;
}
@ -145,8 +152,9 @@ class ListJsonTable
return $actions;
}
public function create()
public function create(): void
{
$this->setActions();
$this->beginTable();
$this->createThead();
$this->createTbody();

View File

@ -23,7 +23,7 @@ class ViewJsonTable
{
$this->json = $json;
$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
{
$paramsStr = $this->createParams($this->data['meta']['params']);
$hook = $this->beforePrintTable;
$this->html = $hook();
//Хук перед выводом ячейки
if (isset($this->beforePrintTable)){
$hook = $this->beforePrintTable;
$this->html = $hook();
}
$this->html .= "<table $paramsStr>";
}
public function createColum(): string
public function createRows(): string
{
foreach ($this->data['meta']['columns'] as $key => $column){
if ($this->issetColumn($key)){
if ($this->beforePrintCell){
foreach ($this->data['meta']['rows'] as $key => $row){
if ($this->issetRow($key)){
if (isset($this->beforePrintCell)){
$hook = $this->beforePrintCell;
$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;
}
private function issetColumn($column)
private function issetRow($column): bool
{
if (isset($this->data['meta']['columns'])){
foreach ($this->data['meta']['columns'] as $key => $currentColumn){
if (isset($this->data['meta']['rows'])){
foreach ($this->data['meta']['rows'] as $key => $currentColumn){
if ($key === $column){
return true;
}
@ -67,18 +72,20 @@ class ViewJsonTable
return false;
}
public function endTable()
public function endTable(): void
{
$this->html .= "</table>";
$hookAfter = $this->afterPrintTable;
$this->html .= $hookAfter();
if(isset($this->afterPrintTable)){
$hookAfter = $this->afterPrintTable;
$this->html .= $hookAfter();
}
}
public function create()
public function create(): void
{
$this->beginTable();
$this->createColum();
$this->createRows();
$this->endTable();
}
@ -93,11 +100,9 @@ class ViewJsonTable
$this->afterPrintTable = $closure;
}
public function render()
public function render(): void
{
echo $this->html;
}
public function setBeforePrintCell(\Closure $closure): void