Skip to content

Commit

Permalink
Row color: JqGrid + Bootstrap
Browse files Browse the repository at this point in the history
  • Loading branch information
ThaDafinser committed Nov 5, 2013
1 parent 66dda30 commit d864558
Show file tree
Hide file tree
Showing 8 changed files with 212 additions and 13 deletions.
38 changes: 38 additions & 0 deletions src/ZfcDatagrid/Datagrid.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -115,6 +116,12 @@ class Datagrid implements ServiceLocatorAwareInterface
*/
protected $columns = array();

/**
*
* @var Style\AbstractStyle[]
*/
protected $rowStyles = array();

/**
*
* @var Column\Action\AbstractAction
Expand Down Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions src/ZfcDatagrid/Examples/Controller/PersonController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion src/ZfcDatagrid/Examples/Data/PhpArray.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
2 changes: 2 additions & 0 deletions src/ZfcDatagrid/Renderer/AbstractRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
38 changes: 34 additions & 4 deletions src/ZfcDatagrid/Renderer/BootstrapTable/View/Helper/TableRow.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use ZfcDatagrid\Column\Style;
use ZfcDatagrid\Column\Type;
use ZfcDatagrid\Column\Action\AbstractAction;
use ZfcDatagrid\Filter;

/**
* View Helper
Expand All @@ -18,10 +19,12 @@ private function getTr($row, $open = true)
if ($open !== true) {
return '</tr>';
} else {
if (isset($row['idConcated']))

if (isset($row['idConcated'])) {
return '<tr id="' . $row['idConcated'] . '">';
else
} else {
return '<tr>';
}
}
}

Expand All @@ -39,7 +42,7 @@ private function getTd($dataValue, $attributes = array())
return '<td ' . $attr . '>' . $dataValue . '</td>';
}

public function __invoke($row, $columns, AbstractAction $rowClickAction = null)
public function __invoke($row, $columns, AbstractAction $rowClickAction = null, $rowStyles = array())
{
$return = $this->getTr($row);

Expand Down Expand Up @@ -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();
Expand Down
1 change: 0 additions & 1 deletion src/ZfcDatagrid/Renderer/JqGrid/View/Helper/Columns.php
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,6 @@ private function getFormatter($column)

default:
throw new \Exception('Not defined yet: "' . get_class($style) . '"');

break;
}

Expand Down
2 changes: 1 addition & 1 deletion view/zfc-datagrid/renderer/bootstrapTable/layout.phtml
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ if($this->rowClickAction !== null){
<!-- Start table BODY -->
<tbody>
<?php foreach($this->data as $row): ?>
<?php echo $this->bootstrapTableRow($row, $this->columns, $this->rowClickAction); ?>
<?php echo $this->bootstrapTableRow($row, $this->columns, $this->rowClickAction, $this->rowStyles); ?>
<?php endforeach; ?>
</tbody>
</table>
Expand Down
138 changes: 132 additions & 6 deletions view/zfc-datagrid/renderer/jqGrid/layout.phtml
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
?>

<?php echo $this->partial($this->templateToolbar);?>
Expand All @@ -49,6 +150,15 @@ if($this->rowClickAction){
<input type="hidden" id="<?php echo $this->gridId.'_'.$parameterNames['columnsGroupByLocal']; ?>" name="<?php echo $this->gridId.'_'.$parameterNames['columnsGroupByLocal']; ?>" value="" />

<script>
//Row background-color + column background-color
function grid_<?php echo $this->gridId; ?>_loadComplete(data){
if(data.data && data.data.rows){
$.each(data.data.rows, function(key, row){
<?php echo $styleString; ?>
});
}
}

var grid_<?php echo $this->gridId; ?>_columnsRowClickDisabled = <?php echo json_encode($this->columnsRowClickDisabled) ?>;

var grid_<?php echo $this->gridId; ?> = $('#<?php echo $this->gridId; ?>').jqGrid({
Expand Down Expand Up @@ -101,8 +211,9 @@ var grid_<?php echo $this->gridId; ?> = $('#<?php echo $this->gridId; ?>').jqGri

colModel: <?php echo $this->jqgridColumns($this->columns); ?>,

datatype : 'jsonstring',
datastr: {data: <?php echo json_encode($this->data); ?>},
datatype : 'local',
data: {data: <?php echo json_encode($this->data); ?>},

jsonReader : {
repeatitems : false,
id : 'idConcated',
Expand Down Expand Up @@ -132,7 +243,7 @@ var grid_<?php echo $this->gridId; ?> = $('#<?php echo $this->gridId; ?>').jqGri
return data.data.rows;
}
}
}
},

<?php if($rowClickLink != ''): ?>
onSelectRow: function(rowId, status, e){
Expand All @@ -142,6 +253,15 @@ var grid_<?php echo $this->gridId; ?> = $('#<?php echo $this->gridId; ?>').jqGri
},
<?php endif; ?>

<?php if($styleString != ''): ?>
loadComplete: function (data) {
if(data !== undefined){
//on inti we load the first page, but locale type do not know about json data
grid_<?php echo $this->gridId; ?>_loadComplete(data);
}
},
<?php endif; ?>

beforeSelectRow: function (rowId, e) {
var colModel = grid_<?php echo $this->gridId; ?>.jqGrid('getGridParam','colModel');
var name = colModel[$.jgrid.getCellIndex(e.target)];
Expand All @@ -158,8 +278,14 @@ var grid_<?php echo $this->gridId; ?> = $('#<?php echo $this->gridId; ?>').jqGri
grid_<?php echo $this->gridId; ?>.jqGrid('filterToolbar');
<?php endif; ?>

//This is needed to load the data with ajax!
/*
* The first page is loaded directly without ajax, that's why it's here complicated...
* @todo find something "smarter"
*/
grid_<?php echo $this->gridId; ?>.jqGrid('setGridParam', {
datatype : 'json'
});
datatype : 'json',
}).trigger('reload');
grid_<?php echo $this->gridId; ?>.jqGrid()[0].addJSONData(grid_<?php echo $this->gridId; ?>.jqGrid('getGridParam', 'data'));
grid_<?php echo $this->gridId; ?>.trigger('reload');
grid_<?php echo $this->gridId; ?>_loadComplete(grid_<?php echo $this->gridId; ?>.jqGrid('getGridParam', 'data'));
</script>

0 comments on commit d864558

Please sign in to comment.