Compare commits

...

3 Commits

Author SHA1 Message Date
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
2 changed files with 24 additions and 17 deletions

View File

@ -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();

View File

@ -71,7 +71,7 @@ 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>";
} }
@ -92,8 +92,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 +104,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 +115,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 +183,6 @@ class ListJsonTable extends JasonTable
} }
private function getColumnKeys(): array private function getColumnKeys(): array
{ {
$columnKeys = []; $columnKeys = [];
@ -254,12 +253,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;
@ -268,27 +271,28 @@ 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/search'>";
foreach ($columnKeys as $key){ foreach ($columnKeys as $key) {
if ($this->issetFilter($key)){ if ($this->issetFilter($key)) {
$filter = $this->getCurrentFilter($key); $filter = $this->getCurrentFilter($key);
$params = [ $params = [
'param' => $filter['param'] ?? '', 'param' => $filter['param'] ?? '',
'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 ($this->showActionColumn) {
$this->html .= "<td></td>";
}
$this->html .= "</form></tr>"; $this->html .= "</form></tr>";
} }
@ -325,7 +329,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,