From 9a25d98c9a3bfcb2db6a71262a2efdab5bcf7900 Mon Sep 17 00:00:00 2001 From: mokaddem Date: Tue, 23 Feb 2021 11:42:26 +0100 Subject: [PATCH] chg: [helpers:bootstrap] Improvements for table --- src/View/Helper/BootstrapHelper.php | 64 +++++++++++++++++++---------- 1 file changed, 42 insertions(+), 22 deletions(-) diff --git a/src/View/Helper/BootstrapHelper.php b/src/View/Helper/BootstrapHelper.php index 5ce4400..d41fb89 100644 --- a/src/View/Helper/BootstrapHelper.php +++ b/src/View/Helper/BootstrapHelper.php @@ -501,32 +501,52 @@ class BoostrapTable extends BootstrapGeneric { ], ]); foreach ($this->items as $i => $row) { - $body .= $this->openNode('tr',[ - 'class' => [ - !empty($row['_rowVariant']) ? "table-{$row['_rowVariant']}" : '' - ] - ]); - if (array_keys($row) !== range(0, count($row) - 1)) { // associative array - foreach ($this->fields as $i => $field) { - if (is_array($field)) { - $key = $field['key']; - } else { - $key = $field; - } - $cellValue = $row[$key]; - $body .= $this->genNode('td', [], h($cellValue)); - } - } else { - foreach ($row as $cellValue) { - $body .= $this->genNode('td', [], h($cellValue)); - } - } - $body .= $this->closeNode('tr');; + $body .= $this->genRow($row); } - $body .= $this->closeNode('tbody');; + $body .= $this->closeNode('tbody'); return $body; } + private function genRow($row) + { + $html = $this->openNode('tr',[ + 'class' => [ + !empty($row['_rowVariant']) ? "table-{$row['_rowVariant']}" : '' + ] + ]); + if (array_keys($row) !== range(0, count($row) - 1)) { // associative array + foreach ($this->fields as $i => $field) { + if (is_array($field)) { + $key = $field['key']; + } else { + $key = $field; + } + $cellValue = $row[$key]; + $html .= $this->genCell($cellValue, $field, $row); + } + } else { // indexed array + foreach ($row as $cellValue) { + $html .= $this->genCell($cellValue, $field, $row); + } + } + $html .= $this->closeNode('tr'); + return $html; + } + + private function genCell($value, $field=[], $row=[]) + { + if (isset($field['formatter'])) { + $cellContent = $field['formatter']($value, $row); + } else { + $cellContent = h($value); + } + return $this->genNode('td', [ + 'class' => [ + !empty($row['_cellVariant']) ? "bg-{$row['_cellVariant']}" : '' + ] + ], $cellContent); + } + private function genCaption() { return $this->genNode('caption', [], h($this->caption));