Compare commits
10 Commits
Author | SHA1 | Date | |
---|---|---|---|
f11d155606 | |||
557fa1e22b | |||
33d7069708 | |||
8063ec0735 | |||
d7e9e06925 | |||
b7927bf3c8 | |||
5effacf12f | |||
5f46431d45 | |||
02a3e52b7d | |||
d747203d99 |
@ -68,6 +68,9 @@ $table->addColumn("Колонка 33", "k33", function ($id) {
|
|||||||
$table->addColumn("Колонка 34", "k34", function ($id) {
|
$table->addColumn("Колонка 34", "k34", function ($id) {
|
||||||
return "some34";
|
return "some34";
|
||||||
});
|
});
|
||||||
|
$table->addAction(function($row, $url){
|
||||||
|
return "<a href='mailto:". $row['email'] ."'>Написать</a>";
|
||||||
|
});
|
||||||
$table->create();
|
$table->create();
|
||||||
$table->render();
|
$table->render();
|
||||||
|
|
||||||
|
@ -7,6 +7,10 @@ use Itguild\Tables\ViewJsonTable;
|
|||||||
$json = file_get_contents('view.json');
|
$json = file_get_contents('view.json');
|
||||||
|
|
||||||
$table = new ViewJsonTable($json);
|
$table = new ViewJsonTable($json);
|
||||||
|
|
||||||
|
$table->addRow("Имя", function (){
|
||||||
|
return "Kirill";
|
||||||
|
}, ['after' => 'username']);
|
||||||
//$table->rows([
|
//$table->rows([
|
||||||
$table->rows([
|
$table->rows([
|
||||||
'username' => function ($cell) {
|
'username' => function ($cell) {
|
||||||
|
@ -5,12 +5,12 @@ namespace Itguild\Tables\Filter;
|
|||||||
abstract class Filter
|
abstract class Filter
|
||||||
{
|
{
|
||||||
public string $html = '';
|
public string $html = '';
|
||||||
public string|array $param;
|
public string|array $params;
|
||||||
public string $name;
|
public string $name;
|
||||||
public string $value;
|
public string $value;
|
||||||
public function __construct(array $source)
|
public function __construct(array $source)
|
||||||
{
|
{
|
||||||
$this->param = $source['param'] ?? '';
|
$this->params = $source['params'] ?? '';
|
||||||
$this->name = $source['name'];
|
$this->name = $source['name'];
|
||||||
$this->value = $source['value'] ?? '';
|
$this->value = $source['value'] ?? '';
|
||||||
}
|
}
|
||||||
|
@ -9,6 +9,7 @@ use Itguild\Tables\ActionColumn\ViewActionColumn;
|
|||||||
use Itguild\Tables\Filter\InputTextFilter;
|
use Itguild\Tables\Filter\InputTextFilter;
|
||||||
use Itguild\Tables\traits\CreateParams;
|
use Itguild\Tables\traits\CreateParams;
|
||||||
use JetBrains\PhpStorm\NoReturn;
|
use JetBrains\PhpStorm\NoReturn;
|
||||||
|
use kernel\helpers\Debug;
|
||||||
|
|
||||||
class ListJsonTable extends JasonTable
|
class ListJsonTable extends JasonTable
|
||||||
{
|
{
|
||||||
@ -18,6 +19,7 @@ class ListJsonTable extends JasonTable
|
|||||||
|
|
||||||
private int $count = 0;
|
private int $count = 0;
|
||||||
private string $baseUrl;
|
private string $baseUrl;
|
||||||
|
private string $searchPrefix;
|
||||||
private array $data;
|
private array $data;
|
||||||
|
|
||||||
private bool $pagination = true;
|
private bool $pagination = true;
|
||||||
@ -35,6 +37,7 @@ class ListJsonTable extends JasonTable
|
|||||||
$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->searchPrefix = $this->data['meta']['searchPrefix'] ?? '/search';
|
||||||
$this->pagination = $this->data['meta']['pagination'] ?? true;
|
$this->pagination = $this->data['meta']['pagination'] ?? true;
|
||||||
$this->showActionColumn = $this->data['meta']['showActionColumn'] ?? true;
|
$this->showActionColumn = $this->data['meta']['showActionColumn'] ?? true;
|
||||||
$this->showFiltersRow = $this->data['meta']['showFiltersRow'] ?? true;
|
$this->showFiltersRow = $this->data['meta']['showFiltersRow'] ?? true;
|
||||||
@ -71,10 +74,10 @@ class ListJsonTable extends JasonTable
|
|||||||
if ($this->showActionColumn) {
|
if ($this->showActionColumn) {
|
||||||
$this->html .= "<th>Действия</th></th></tr>";
|
$this->html .= "<th>Действия</th></th></tr>";
|
||||||
}
|
}
|
||||||
if($this->showFiltersRow){
|
if ($this->showFiltersRow) {
|
||||||
$this->getFilters($columnKeys);
|
$this->getFilters($columnKeys);
|
||||||
$this->html .= "</thead>";
|
|
||||||
}
|
}
|
||||||
|
$this->html .= "</thead>";
|
||||||
}
|
}
|
||||||
|
|
||||||
public function createTbody(): void
|
public function createTbody(): void
|
||||||
@ -92,8 +95,8 @@ class ListJsonTable extends JasonTable
|
|||||||
}
|
}
|
||||||
foreach ($row as $key => $cell) {
|
foreach ($row as $key => $cell) {
|
||||||
if ($this->issetColumn($key) and $this->is_fillable($key)) {
|
if ($this->issetColumn($key) and $this->is_fillable($key)) {
|
||||||
if($this->beforePrintCell) {
|
if ($this->beforePrintCell) {
|
||||||
if ($key === "id" and $cell === 0){
|
if ($key === "id" and $cell === 0) {
|
||||||
$cell = $this->count;
|
$cell = $this->count;
|
||||||
}
|
}
|
||||||
$cell = $this->getCustomCell($key, $cell);
|
$cell = $this->getCustomCell($key, $cell);
|
||||||
@ -104,7 +107,7 @@ class ListJsonTable extends JasonTable
|
|||||||
$this->getCustomColumns($row["id"] ?? null);
|
$this->getCustomColumns($row["id"] ?? null);
|
||||||
if ($this->showActionColumn) {
|
if ($this->showActionColumn) {
|
||||||
if (isset($row["id"])) {
|
if (isset($row["id"])) {
|
||||||
$actions = $this->getActions($row["id"]);
|
$actions = $this->getActions($row);
|
||||||
|
|
||||||
$this->html .= "<td>$actions</td></tr>";
|
$this->html .= "<td>$actions</td></tr>";
|
||||||
} else {
|
} else {
|
||||||
@ -115,7 +118,7 @@ class ListJsonTable extends JasonTable
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public function addAction(string $actionColumn): void
|
public function addAction(string|\Closure $actionColumn): void
|
||||||
{
|
{
|
||||||
$this->customActionsArray[] = $actionColumn;
|
$this->customActionsArray[] = $actionColumn;
|
||||||
}
|
}
|
||||||
@ -183,7 +186,6 @@ class ListJsonTable extends JasonTable
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
private function getColumnKeys(): array
|
private function getColumnKeys(): array
|
||||||
{
|
{
|
||||||
$columnKeys = [];
|
$columnKeys = [];
|
||||||
@ -254,12 +256,16 @@ class ListJsonTable extends JasonTable
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
private function getActions(int $id): string
|
private function getActions(array $row): string
|
||||||
{
|
{
|
||||||
$actions = "";
|
$actions = "";
|
||||||
foreach ($this->actionsArray as $item) {
|
foreach ($this->actionsArray as $item) {
|
||||||
$objItem = new $item($this->baseUrl, $id);
|
if (is_string($item)) {
|
||||||
|
$objItem = new $item($this->baseUrl, $row['id']);
|
||||||
$actions .= $objItem->fetch();
|
$actions .= $objItem->fetch();
|
||||||
|
} else {
|
||||||
|
$actions .= $item($row, $this->baseUrl);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return $actions;
|
return $actions;
|
||||||
@ -267,28 +273,32 @@ class ListJsonTable extends JasonTable
|
|||||||
|
|
||||||
private function getFilters(array $columnKeys): void
|
private function getFilters(array $columnKeys): void
|
||||||
{
|
{
|
||||||
$this->html .= "<tr><form action='$this->baseUrl/search'>";
|
$this->html .= "<tr><form action='$this->baseUrl$this->searchPrefix'>";
|
||||||
foreach ($columnKeys as $key){
|
$flag = false;
|
||||||
if ($this->issetFilter($key)){
|
foreach ($columnKeys as $key) {
|
||||||
|
if ($this->issetFilter($key)) {
|
||||||
|
$flag = true;
|
||||||
$filter = $this->getCurrentFilter($key);
|
$filter = $this->getCurrentFilter($key);
|
||||||
$params = [
|
$params = [
|
||||||
'param' => $filter['param'] ?? '',
|
'params' => $filter['params'] ?? '',
|
||||||
'name' => $key,
|
'name' => $key,
|
||||||
'value' => $filter['value'] ?? '',
|
'value' => $filter['value'] ?? '',
|
||||||
];
|
];
|
||||||
if ($filter){
|
if ($filter) {
|
||||||
$class = new $filter['class']($params);
|
$class = new $filter['class']($params);
|
||||||
$this->html .= $class->fetch();
|
$this->html .= $class->fetch();
|
||||||
}
|
} else {
|
||||||
else {
|
|
||||||
$class = new InputTextFilter($params);
|
$class = new InputTextFilter($params);
|
||||||
$this->html .= $class->fetch();
|
$this->html .= $class->fetch();
|
||||||
}
|
}
|
||||||
}
|
} else {
|
||||||
else {
|
|
||||||
$this->html .= "<td></td>";
|
$this->html .= "<td></td>";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if ($flag) {
|
||||||
|
$this->showActionColumn = true;
|
||||||
|
$this->html .= "<td><input class='btn btn-primary' type='submit' style='width: 150px' value='Применить'></td>";
|
||||||
|
}
|
||||||
$this->html .= "</form></tr>";
|
$this->html .= "</form></tr>";
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -325,7 +335,7 @@ class ListJsonTable extends JasonTable
|
|||||||
$hookAfter = $this->afterPrintHook;
|
$hookAfter = $this->afterPrintHook;
|
||||||
$this->html .= $hookAfter($this->data['meta']);
|
$this->html .= $hookAfter($this->data['meta']);
|
||||||
|
|
||||||
if ($this->pagination) {
|
if ($this->pagination && $this->data['data']) {
|
||||||
$options = [
|
$options = [
|
||||||
'countItem' => $this->data['meta']['total'],
|
'countItem' => $this->data['meta']['total'],
|
||||||
'perPage' => $this->data['meta']['perPage'] ?? 10,
|
'perPage' => $this->data['meta']['perPage'] ?? 10,
|
||||||
|
@ -11,6 +11,9 @@ class ViewJsonTable extends JasonTable
|
|||||||
private array $data;
|
private array $data;
|
||||||
private string $json;
|
private string $json;
|
||||||
private array $dataJson;
|
private array $dataJson;
|
||||||
|
|
||||||
|
private array $customRowsArray = [];
|
||||||
|
|
||||||
public function __construct($json)
|
public function __construct($json)
|
||||||
{
|
{
|
||||||
$this->beforePrintCell = [];
|
$this->beforePrintCell = [];
|
||||||
@ -28,44 +31,40 @@ class ViewJsonTable extends JasonTable
|
|||||||
$paramsStr = $this->createParams($this->data['meta']['params']);
|
$paramsStr = $this->createParams($this->data['meta']['params']);
|
||||||
|
|
||||||
//Хук перед выводом ячейки
|
//Хук перед выводом ячейки
|
||||||
if (isset($this->beforePrintHook)){
|
if (isset($this->beforePrintHook)) {
|
||||||
$hook = $this->beforePrintHook;
|
$hook = $this->beforePrintHook;
|
||||||
$this->html .= $hook();
|
$this->html .= $hook();
|
||||||
}
|
}
|
||||||
|
|
||||||
$this->html .= "<table $paramsStr>";
|
$this->html .= "<table $paramsStr>";
|
||||||
}
|
}
|
||||||
|
|
||||||
public function createRows(): string
|
public function createRows(): string
|
||||||
{
|
{
|
||||||
foreach ($this->data['meta']['rows'] as $key => $row){
|
foreach ($this->data['meta']['rows'] as $key => $row) {
|
||||||
if ($this->issetRow($key)){
|
if ($this->issetRow($key)) {
|
||||||
|
$rowsBefore = $this->getCustomRows($key, "before");
|
||||||
|
$rowsAfter = $this->getCustomRows($key, "after");
|
||||||
if ($this->beforePrintCell) {
|
if ($this->beforePrintCell) {
|
||||||
$this->dataJson[$key] = $this->getCustomCell($key, $this->dataJson[$key]);
|
$this->dataJson[$key] = $this->getCustomCell($key, $this->dataJson[$key]);
|
||||||
}
|
}
|
||||||
|
foreach ($rowsBefore as $item){
|
||||||
|
$this->html .= "<tr><th>" . $item['label'] . "</th><td>" . $this->getCustomRowHandler($item) . "</td></tr>";
|
||||||
|
}
|
||||||
$this->html .= "<tr><th>" . $row . "</th><td>" . $this->dataJson[$key] . "</td></tr>";
|
$this->html .= "<tr><th>" . $row . "</th><td>" . $this->dataJson[$key] . "</td></tr>";
|
||||||
|
foreach ($rowsAfter as $item){
|
||||||
|
$this->html .= "<tr><th>" . $item['label'] . "</th><td>" . $this->getCustomRowHandler($item) . "</td></tr>";
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return $this->html;
|
return $this->html;
|
||||||
}
|
}
|
||||||
private function issetRow($column): bool
|
|
||||||
{
|
|
||||||
|
|
||||||
if (isset($this->data['meta']['rows'])){
|
|
||||||
foreach ($this->data['meta']['rows'] as $key => $currentColumn){
|
|
||||||
if ($key === $column){
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
public function endTable(): void
|
public function endTable(): void
|
||||||
{
|
{
|
||||||
$this->html .= "</table>";
|
$this->html .= "</table>";
|
||||||
|
|
||||||
if(isset($this->afterPrintHook)){
|
if (isset($this->afterPrintHook)) {
|
||||||
$hookAfter = $this->afterPrintHook;
|
$hookAfter = $this->afterPrintHook;
|
||||||
$this->html .= $hookAfter();
|
$this->html .= $hookAfter();
|
||||||
}
|
}
|
||||||
@ -85,4 +84,46 @@ class ViewJsonTable extends JasonTable
|
|||||||
$this->beforePrintCell[$key] = $value;
|
$this->beforePrintCell[$key] = $value;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function addRow(string $label, \Closure $handler, array $position = []): void
|
||||||
|
{
|
||||||
|
$this->customRowsArray[] = ['label' => $label, 'handler' => $handler, 'position' => $position];
|
||||||
|
}
|
||||||
|
|
||||||
|
private function issetRow($column): bool
|
||||||
|
{
|
||||||
|
|
||||||
|
if (isset($this->data['meta']['rows'])) {
|
||||||
|
foreach ($this->data['meta']['rows'] as $key => $currentColumn) {
|
||||||
|
if ($key === $column) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
private function getCustomRows(string $key, string $position): array
|
||||||
|
{
|
||||||
|
$customRows = [];
|
||||||
|
foreach ($this->customRowsArray as $item) {
|
||||||
|
if (!empty($item['position'])) {
|
||||||
|
if (isset($item['position'][$position]) && $item['position'][$position] === $key){
|
||||||
|
$customRows[] = $item;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return $customRows;
|
||||||
|
}
|
||||||
|
|
||||||
|
private function getCustomRowHandler(array $row): mixed
|
||||||
|
{
|
||||||
|
if (isset($row['handler'])){
|
||||||
|
return $row['handler']();
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
}
|
}
|
Reference in New Issue
Block a user