4 Commits
1.0.6 ... 1.1.0

Author SHA1 Message Date
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
3 changed files with 66 additions and 18 deletions

View File

@ -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) {

View File

@ -290,6 +290,9 @@ class ListJsonTable extends JasonTable
$this->html .= "<td></td>"; $this->html .= "<td></td>";
} }
} }
if ($this->showActionColumn) {
$this->html .= "<td><input class='btn btn-primary' type='submit' style='width: 150px' value='Применить'></td>";
}
$this->html .= "</form></tr>"; $this->html .= "</form></tr>";
} }
@ -326,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,

View File

@ -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 = [];
@ -35,31 +38,27 @@ class ViewJsonTable extends JasonTable
$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
{ {
@ -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;
}
} }