Sarke
1
Hi all,
Is it possible to put in a divider row in CGridView? What I would want is something like this:
Row 1 AA BB CC DD
Row 2 AA BB CC DD
----------------- (<-- this)
Row 3 AA BB CC DD
Row 4 AA BB CC DD
Or better yet, insert the table header again at that point.
Thanks.
Chris83
(Cniska)
2
Hello Sarke,
This is at least possible by creating an own data provider.
Sarke
3
I ended up doing a quick extension of the CGridView class.
Yii::import('zii.widgets.grid.CGridView');
class MyGridView extends CGridView
{
public $dividers = array();
public function renderTableBody()
{
$data=$this->dataProvider->getData();
$n=count($data);
$cols=count($this->columns);
echo "<tbody>\n";
if($n>0)
{
for($row=0;$row<$n;++$row)
{
if ($cols > 0 and in_array($row, $this->dividers))
echo '<tr><td colspan="' . $cols . '" class="divider"></td></tr>' . "\n";
$this->renderTableRow($row);
}
}
else
{
echo '<tr><td colspan="'.count($this->columns).'">';
$this->renderEmptyText();
echo "</td></tr>\n";
}
echo "</tbody>\n";
}
}
The widget code:
$this->widget('application.extensions.MyGridView', array(
'dataProvider'=>$data,
'dividers'=>array(3, <img src='http://www.yiiframework.com/forum/public/style_emoticons/default/cool.gif' class='bbc_emoticon' alt='8)' />,
);
And the css for the divider class:
.grid-view table.items tr td.divider
{
padding: 2px;
background-color: black;
}
Sarke
4
Updated code, I rewrote it to allow both vertical and horizontal dividers.
Yii::import('zii.widgets.grid.CGridView');
class MyGridView extends CGridView
{
public $hdiv = array();
public $vdiv = array();
public function renderTableRow($row)
{
if (count($this->columns) > 0 and in_array($row, $this->hdiv))
$extra = ' divider';
else
$extra = '';
if($this->rowCssClassExpression!==null)
{
$data=$this->dataProvider->data[$row];
echo '<tr class="'.$this->evaluateExpression($this->rowCssClassExpression,array('row'=>$row,'data'=>$data)).$extra.'">';
}
else if(is_array($this->rowCssClass) && ($n=count($this->rowCssClass))>0)
echo '<tr class="'.$this->rowCssClass[$row%$n].$extra.'">';
elseif (!empty($extra))
echo '<tr class="divider">';
else
echo '<tr>';
foreach($this->columns as $column)
{
if (in_array($column->header, $this->vdiv))
$column->cssClassExpression = '"divider"';
$column->renderDataCell($row);
}
echo "</tr>\n";
}
}
The widget code:
$this->widget('application.extensions.MyGridView', array(
'dataProvider'=>$data,
'hdiv'=>array(3, <img src='http://www.yiiframework.com/forum/public/style_emoticons/default/cool.gif' class='bbc_emoticon' alt='8)' />, // row numbers
'vdiv'=>array('Qty', 'Tax'), // column headers
);
And the css for the divider classes:
.grid-view table.items tr.divider
{
border-top: 2px solid black;
}
.grid-view table.items tr th.divider,
.grid-view table.items tr td.divider
{
border-right: 2px solid black;
}
meg
(Megblub)
5
Thank you very much, Sarke!
I was trying to do something a bit different and your code helped me a lot.
Meg