12 Commits
1.0.3 ... 1.1.5

Author SHA1 Message Date
f11d155606 fix filter 2024-12-27 14:55:06 +03:00
557fa1e22b fix filter 2024-12-27 14:50:28 +03:00
33d7069708 param => params fix 2024-12-27 12:28:49 +03:00
8063ec0735 filter fix 2024-12-23 16:30:18 +03:00
d7e9e06925 fix filter submit button 2024-12-23 15:24:15 +03:00
b7927bf3c8 add action column to filter with submit button 2024-12-19 16:53:57 +03:00
5effacf12f add row at json view table 2024-11-28 11:57:17 +03:00
5f46431d45 fix filter row 2024-10-11 16:31:11 +03:00
02a3e52b7d fix pagination 2024-09-24 15:42:55 +03:00
d747203d99 custom action 2024-09-02 22:34:25 +03:00
ad18d94489 table filter fix 2024-08-29 13:13:51 +03:00
453917ea1a table filter fix 2024-08-29 13:08:43 +03:00
6 changed files with 108 additions and 42 deletions

View File

@ -15,11 +15,11 @@
"currentPage": "1",
"showActionColumn": true,
"showFiltersRow": true,
"filter": true,
"total": 10,
"paginationPrefix": "/page",
"params": {"class": "table table-bordered", "border": "1"}
},
"filters": ["email", "status"],
"data": [
{"id": 1,"email":"fas1@mail.ru","description":"sdgsdfg","description2":"ffdghdas", "created_at": "17.06.2024", "status": "1"},
{"id": 2,"email":"fas2@mail.ru","description":"fafdgdfgsdfdfs","description2":"ffdghdas", "created_at": "18.06.2024", "status": "1"},

View File

@ -68,6 +68,9 @@ $table->addColumn("Колонка 33", "k33", function ($id) {
$table->addColumn("Колонка 34", "k34", function ($id) {
return "some34";
});
$table->addAction(function($row, $url){
return "<a href='mailto:". $row['email'] ."'>Написать</a>";
});
$table->create();
$table->render();

View File

@ -7,6 +7,10 @@ use Itguild\Tables\ViewJsonTable;
$json = file_get_contents('view.json');
$table = new ViewJsonTable($json);
$table->addRow("Имя", function (){
return "Kirill";
}, ['after' => 'username']);
//$table->rows([
$table->rows([
'username' => function ($cell) {

View File

@ -5,12 +5,12 @@ namespace Itguild\Tables\Filter;
abstract class Filter
{
public string $html = '';
public string|array $param;
public string|array $params;
public string $name;
public string $value;
public function __construct(array $source)
{
$this->param = $source['param'] ?? '';
$this->params = $source['params'] ?? '';
$this->name = $source['name'];
$this->value = $source['value'] ?? '';
}

View File

@ -6,8 +6,10 @@ use Exception;
use Itguild\Tables\ActionColumn\DeleteActionColumn;
use Itguild\Tables\ActionColumn\EditActionColumn;
use Itguild\Tables\ActionColumn\ViewActionColumn;
use Itguild\Tables\Filter\InputTextFilter;
use Itguild\Tables\traits\CreateParams;
use JetBrains\PhpStorm\NoReturn;
use kernel\helpers\Debug;
class ListJsonTable extends JasonTable
{
@ -17,6 +19,7 @@ class ListJsonTable extends JasonTable
private int $count = 0;
private string $baseUrl;
private string $searchPrefix;
private array $data;
private bool $pagination = true;
@ -34,6 +37,7 @@ class ListJsonTable extends JasonTable
$this->json = $json;
$this->data = json_decode($this->json, true);
$this->baseUrl = $this->data['meta']['baseUrl'] ?? '';
$this->searchPrefix = $this->data['meta']['searchPrefix'] ?? '/search';
$this->pagination = $this->data['meta']['pagination'] ?? true;
$this->showActionColumn = $this->data['meta']['showActionColumn'] ?? true;
$this->showFiltersRow = $this->data['meta']['showFiltersRow'] ?? true;
@ -70,10 +74,10 @@ class ListJsonTable extends JasonTable
if ($this->showActionColumn) {
$this->html .= "<th>Действия</th></th></tr>";
}
if($this->showFiltersRow){
if ($this->showFiltersRow) {
$this->getFilters($columnKeys);
$this->html .= "</thead>";
}
$this->html .= "</thead>";
}
public function createTbody(): void
@ -91,8 +95,8 @@ class ListJsonTable extends JasonTable
}
foreach ($row as $key => $cell) {
if ($this->issetColumn($key) and $this->is_fillable($key)) {
if($this->beforePrintCell) {
if ($key === "id" and $cell === 0){
if ($this->beforePrintCell) {
if ($key === "id" and $cell === 0) {
$cell = $this->count;
}
$cell = $this->getCustomCell($key, $cell);
@ -103,7 +107,7 @@ class ListJsonTable extends JasonTable
$this->getCustomColumns($row["id"] ?? null);
if ($this->showActionColumn) {
if (isset($row["id"])) {
$actions = $this->getActions($row["id"]);
$actions = $this->getActions($row);
$this->html .= "<td>$actions</td></tr>";
} else {
@ -114,7 +118,7 @@ class ListJsonTable extends JasonTable
}
}
public function addAction(string $actionColumn): void
public function addAction(string|\Closure $actionColumn): void
{
$this->customActionsArray[] = $actionColumn;
}
@ -147,9 +151,9 @@ class ListJsonTable extends JasonTable
private function getCurrentFilter(string $column)
{
if (is_array($this->beforePrintCell[$column])) {
if (isset($this->beforePrintCell[$column]) and is_array($this->beforePrintCell[$column])) {
if (isset($this->beforePrintCell[$column]['filter'])) {
if (is_array($this->beforePrintCell[$column]['filter'])) {
if (isset($this->beforePrintCell[$column]['filter']) and is_array($this->beforePrintCell[$column]['filter'])) {
return $this->beforePrintCell[$column]['filter'];
}
}
@ -182,7 +186,6 @@ class ListJsonTable extends JasonTable
}
private function getColumnKeys(): array
{
$columnKeys = [];
@ -253,12 +256,16 @@ class ListJsonTable extends JasonTable
return false;
}
private function getActions(int $id): string
private function getActions(array $row): string
{
$actions = "";
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();
} else {
$actions .= $item($row, $this->baseUrl);
}
}
return $actions;
@ -266,21 +273,32 @@ class ListJsonTable extends JasonTable
private function getFilters(array $columnKeys): void
{
$this->html .= "<tr><form action='$this->baseUrl/search'>";
foreach ($columnKeys as $key){
if ($this->issetFilter($key)){
$this->html .= "<tr><form action='$this->baseUrl$this->searchPrefix'>";
$flag = false;
foreach ($columnKeys as $key) {
if ($this->issetFilter($key)) {
$flag = true;
$filter = $this->getCurrentFilter($key);
$class = new $filter['class']([
'param' => $filter['param'] ?? '',
$params = [
'params' => $filter['params'] ?? '',
'name' => $key,
'value' => $filter['value'] ?? '',
]);
];
if ($filter) {
$class = new $filter['class']($params);
$this->html .= $class->fetch();
} else {
$class = new InputTextFilter($params);
$this->html .= $class->fetch();
}
else {
} else {
$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>";
}
@ -317,7 +335,7 @@ class ListJsonTable extends JasonTable
$hookAfter = $this->afterPrintHook;
$this->html .= $hookAfter($this->data['meta']);
if ($this->pagination) {
if ($this->pagination && $this->data['data']) {
$options = [
'countItem' => $this->data['meta']['total'],
'perPage' => $this->data['meta']['perPage'] ?? 10,

View File

@ -11,6 +11,9 @@ class ViewJsonTable extends JasonTable
private array $data;
private string $json;
private array $dataJson;
private array $customRowsArray = [];
public function __construct($json)
{
$this->beforePrintCell = [];
@ -28,44 +31,40 @@ class ViewJsonTable extends JasonTable
$paramsStr = $this->createParams($this->data['meta']['params']);
//Хук перед выводом ячейки
if (isset($this->beforePrintHook)){
if (isset($this->beforePrintHook)) {
$hook = $this->beforePrintHook;
$this->html .= $hook();
}
$this->html .= "<table $paramsStr>";
}
public function createRows(): string
{
foreach ($this->data['meta']['rows'] as $key => $row){
if ($this->issetRow($key)){
foreach ($this->data['meta']['rows'] as $key => $row) {
if ($this->issetRow($key)) {
$rowsBefore = $this->getCustomRows($key, "before");
$rowsAfter = $this->getCustomRows($key, "after");
if ($this->beforePrintCell) {
$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>";
foreach ($rowsAfter as $item){
$this->html .= "<tr><th>" . $item['label'] . "</th><td>" . $this->getCustomRowHandler($item) . "</td></tr>";
}
}
}
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
{
$this->html .= "</table>";
if(isset($this->afterPrintHook)){
if (isset($this->afterPrintHook)) {
$hookAfter = $this->afterPrintHook;
$this->html .= $hookAfter();
}
@ -85,4 +84,46 @@ class ViewJsonTable extends JasonTable
$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;
}
}