add row at json view table
This commit is contained in:
parent
5f46431d45
commit
5effacf12f
@ -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) {
|
||||
|
@ -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>";
|
||||
}
|
||||
}
|
||||
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;
|
||||
foreach ($rowsAfter as $item){
|
||||
$this->html .= "<tr><th>" . $item['label'] . "</th><td>" . $this->getCustomRowHandler($item) . "</td></tr>";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
return $this->html;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user