From d864558d31cb8f494b2aa6bdb1080c5d67cb0550 Mon Sep 17 00:00:00 2001 From: Martin Keckeis Date: Tue, 5 Nov 2013 09:00:39 +0100 Subject: [PATCH] Row color: JqGrid + Bootstrap --- src/ZfcDatagrid/Datagrid.php | 38 +++++ .../Examples/Controller/PersonController.php | 4 + src/ZfcDatagrid/Examples/Data/PhpArray.php | 2 +- src/ZfcDatagrid/Renderer/AbstractRenderer.php | 2 + .../BootstrapTable/View/Helper/TableRow.php | 38 ++++- .../Renderer/JqGrid/View/Helper/Columns.php | 1 - .../renderer/bootstrapTable/layout.phtml | 2 +- .../zfc-datagrid/renderer/jqGrid/layout.phtml | 138 +++++++++++++++++- 8 files changed, 212 insertions(+), 13 deletions(-) diff --git a/src/ZfcDatagrid/Datagrid.php b/src/ZfcDatagrid/Datagrid.php index cdc7b6a4..60edc923 100644 --- a/src/ZfcDatagrid/Datagrid.php +++ b/src/ZfcDatagrid/Datagrid.php @@ -19,6 +19,7 @@ use Zend\View\Model\JsonModel; use Zend\Stdlib\ResponseInterface; use Doctrine\Common\Collections\Collection; +use ZfcDatagrid\Column\Style; class Datagrid implements ServiceLocatorAwareInterface { @@ -115,6 +116,12 @@ class Datagrid implements ServiceLocatorAwareInterface */ protected $columns = array(); + /** + * + * @var Style\AbstractStyle[] + */ + protected $rowStyles = array(); + /** * * @var Column\Action\AbstractAction @@ -675,6 +682,37 @@ public function getColumnByUniqueId($id) return null; } + /** + * + * @param unknown $style + */ + public function addRowStyle(Style\AbstractStyle $style) + { + $this->rowStyles[] = $style; + } + + /** + * + * @return Style\AbstractStyle[] + */ + public function getRowStyles() + { + return $this->rowStyles; + } + + /** + * + * @return boolean + */ + public function hasRowStyles() + { + if (count($this->rowStyles) > 0) { + return true; + } + + return false; + } + /** * * @param boolean $mode diff --git a/src/ZfcDatagrid/Examples/Controller/PersonController.php b/src/ZfcDatagrid/Examples/Controller/PersonController.php index 64873fae..ae508afa 100644 --- a/src/ZfcDatagrid/Examples/Controller/PersonController.php +++ b/src/ZfcDatagrid/Examples/Controller/PersonController.php @@ -71,6 +71,10 @@ private function getGrid(){ $col->setSortDefault(2, 'DESC'); $dataGrid->addColumn($col); + $style = new Style\BackgroundColor(Style\AbstractColor::$GREEN); + $style->setByValue($col, 'Martin'); + $dataGrid->addRowStyle($style); + $col = new Column\Select('gender'); $col->setLabel('Gender'); $col->setWidth(10); diff --git a/src/ZfcDatagrid/Examples/Data/PhpArray.php b/src/ZfcDatagrid/Examples/Data/PhpArray.php index 9c558381..103cf361 100644 --- a/src/ZfcDatagrid/Examples/Data/PhpArray.php +++ b/src/ZfcDatagrid/Examples/Data/PhpArray.php @@ -72,7 +72,7 @@ public function getPersons () 'changeDate' => '2013-04-19 09:30:41' ); $row4 = array( - 'id' => 5, + 'id' => 4, 'displayName' => 'Martin Keckeis', 'familyName' => 'Keckeis', 'givenName' => 'Martin', diff --git a/src/ZfcDatagrid/Renderer/AbstractRenderer.php b/src/ZfcDatagrid/Renderer/AbstractRenderer.php index fe3c1c1d..bf61a6fb 100644 --- a/src/ZfcDatagrid/Renderer/AbstractRenderer.php +++ b/src/ZfcDatagrid/Renderer/AbstractRenderer.php @@ -536,6 +536,8 @@ public function prepareViewModel(Datagrid $grid) } $viewModel->setVariable('columnsHidden', $columnsHidden); + $viewModel->setVariable('rowStyles', $grid->getRowStyles()); + $viewModel->setVariable('paginator', $this->getPaginator()); $viewModel->setVariable('data', $this->getData()); $viewModel->setVariable('filters', $this->getFilters()); diff --git a/src/ZfcDatagrid/Renderer/BootstrapTable/View/Helper/TableRow.php b/src/ZfcDatagrid/Renderer/BootstrapTable/View/Helper/TableRow.php index 9891a6cf..012b03b2 100644 --- a/src/ZfcDatagrid/Renderer/BootstrapTable/View/Helper/TableRow.php +++ b/src/ZfcDatagrid/Renderer/BootstrapTable/View/Helper/TableRow.php @@ -6,6 +6,7 @@ use ZfcDatagrid\Column\Style; use ZfcDatagrid\Column\Type; use ZfcDatagrid\Column\Action\AbstractAction; +use ZfcDatagrid\Filter; /** * View Helper @@ -18,10 +19,12 @@ private function getTr($row, $open = true) if ($open !== true) { return ''; } else { - if (isset($row['idConcated'])) + + if (isset($row['idConcated'])) { return ''; - else + } else { return ''; + } } } @@ -39,7 +42,7 @@ private function getTd($dataValue, $attributes = array()) return '' . $dataValue . ''; } - public function __invoke($row, $columns, AbstractAction $rowClickAction = null) + public function __invoke($row, $columns, AbstractAction $rowClickAction = null, $rowStyles = array()) { $return = $this->getTr($row); @@ -105,13 +108,40 @@ public function __invoke($row, $columns, AbstractAction $rowClickAction = null) default: throw new \Exception('Not defined yet: "' . get_class($style) . '"'); - break; } } } } + foreach ($rowStyles as $style) { + /* @var $style \ZfcDatagrid\Column\Style\AbstractStyle */ + if ($style->isApply($row) === true) { + switch (get_class($style)) { + + case 'ZfcDatagrid\Column\Style\Bold': + $styles[] = 'font-weight: bold'; + break; + + case 'ZfcDatagrid\Column\Style\Italic': + $styles[] = 'font-style: italic'; + break; + + case 'ZfcDatagrid\Column\Style\Color': + $styles[] = 'color: #' . $style->getRgbHexString(); + break; + + case 'ZfcDatagrid\Column\Style\BackgroundColor': + $styles[] = 'background-color: #' . $style->getRgbHexString(); + break; + + default: + throw new \Exception('Not defined yet: "' . get_class($style) . '"'); + break; + } + } + } + if ($column instanceof Column\Action) { /* @var $column \ZfcDatagrid\Column\Action */ $actions = array(); diff --git a/src/ZfcDatagrid/Renderer/JqGrid/View/Helper/Columns.php b/src/ZfcDatagrid/Renderer/JqGrid/View/Helper/Columns.php index cad7c1f3..08848e12 100644 --- a/src/ZfcDatagrid/Renderer/JqGrid/View/Helper/Columns.php +++ b/src/ZfcDatagrid/Renderer/JqGrid/View/Helper/Columns.php @@ -222,7 +222,6 @@ private function getFormatter($column) default: throw new \Exception('Not defined yet: "' . get_class($style) . '"'); - break; } diff --git a/view/zfc-datagrid/renderer/bootstrapTable/layout.phtml b/view/zfc-datagrid/renderer/bootstrapTable/layout.phtml index 531902dc..e5a4716d 100644 --- a/view/zfc-datagrid/renderer/bootstrapTable/layout.phtml +++ b/view/zfc-datagrid/renderer/bootstrapTable/layout.phtml @@ -129,7 +129,7 @@ if($this->rowClickAction !== null){ data as $row): ?> - bootstrapTableRow($row, $this->columns, $this->rowClickAction); ?> + bootstrapTableRow($row, $this->columns, $this->rowClickAction, $this->rowStyles); ?> diff --git a/view/zfc-datagrid/renderer/jqGrid/layout.phtml b/view/zfc-datagrid/renderer/jqGrid/layout.phtml index 2da8b316..34955828 100644 --- a/view/zfc-datagrid/renderer/jqGrid/layout.phtml +++ b/view/zfc-datagrid/renderer/jqGrid/layout.phtml @@ -37,6 +37,107 @@ if($this->rowClickAction){ throw new \Exception('Currently the row click action cannot bet used with Column parameters, except the rowId...'); } } + +/* + * Column - background-color + */ +$styleString = ''; +foreach($this->columns as $column){ + foreach ($column->getStyles() as $style) { + + switch (get_class($style)) { + + case 'ZfcDatagrid\Column\Style\BackgroundColor': + $css = '$(\'#\' + row.idConcated).find(\'td[aria-describedby=' . $this->gridId . '_' . $column->getUniqueId() . ']\').css(\'background-color\', \'#'.$style->getRgbHexString().'\');'; + if ($style->isForAll() === false) { + foreach ($style->getByValues() as $rule) { + $colString = $rule['column']->getUniqueId(); + $operator = ''; + switch ($rule['operator']) { + + case Filter::EQUAL: + $operator = '=='; + break; + + case Filter::NOT_EQUAL: + $operator = '!='; + break; + + default: + throw new \Exception('currently not implemented filter type: "' . $rule['operator'] . '"'); + break; + } + + $styleString .= 'if(row.'.$colString.' ' . $operator . ' \''. $rule['value'] . '\'){'; + $styleString .= $css; + $styleString .= '}'; + } + } else{ + $styleString .= $css; + } + break; + } + } +} + +/* + * Row coloring + */ +foreach($this->rowStyles as $style){ + /* @var $style \ZfcDatagrid\Column\Style\AbstractStyle */ + + switch (get_class($style)) { + + case 'ZfcDatagrid\Column\Style\Bold': + $css = '\'font-weight\', \'bold\''; + break; + + case 'ZfcDatagrid\Column\Style\Italic': + $css = '\'font-style\', \'italic\''; + break; + + case 'ZfcDatagrid\Column\Style\Color': + $css = '\'color\', \'#' . $style->getRgbHexString().'\''; + break; + + case 'ZfcDatagrid\Column\Style\BackgroundColor': + $css = '\'background-color\', \'#' . $style->getRgbHexString().'\''; + break; + + default: + throw new \Exception('Not defined yet: "' . get_class($style) . '"'); + break; + } + $css = '$(\'#\' + row.idConcated).find(\'td\').css('.$css.');'; + + + if ($style->isForAll() === false) { + foreach ($style->getByValues() as $rule) { + $colString = $rule['column']->getUniqueId(); + $operator = ''; + switch ($rule['operator']) { + + case Filter::EQUAL: + $operator = '=='; + break; + + case Filter::NOT_EQUAL: + $operator = '!='; + break; + + default: + throw new \Exception('currently not implemented filter type: "' . $rule['operator'] . '"'); + break; + } + + $styleString .= 'if(row.'.$colString.' ' . $operator . ' \''. $rule['value'] . '\'){'; + $styleString .= $css; + $styleString .= '}'; + } + } else{ + $styleString .= $css; + } +} ?> partial($this->templateToolbar);?> @@ -49,6 +150,15 @@ if($this->rowClickAction){