GroupGridView extends CGridView with following features:
- group equal values in merged cells
- add extra rows for grouped values
Links ¶
Requirements ¶
Developed and tested on Yii 1.1.9.
Installation ¶
Put groupgridview folder from zip to your protected/extensions.
Usage ¶
See code samples in demo.
Main parameters are:
- mergeColumns - array of columns in which equal cells to be merged
mergeType - how merge is displayed:
simple
: column values are merged independently (default)nested
: column values are merged if at least one value of nested columns changes (makes sense when several columns in $mergeColumns option, see #11462)firstrow
: column values are merged independently, but value is shown in first row of group and below cells just cleared (instead ofrowspan
)
- extraRowColumns - array of columns for which every change of value will trigger extra row
- extraRowPos - position of extra row relative to group:
above
(default) |below
- extraRowExpression - string PHP expression or function that returns value displayed in extrarow. Can use
$data
and$totals
inside. - extraRowTotals - function that is used to calc subtotals. Example:
'extraRowTotals' => function($data, $row, &$totals) {
if(!isset($totals['sum_age'])) $totals['sum_age'] = 0;
$totals['sum_age'] += $data['age'];
}
Variable $totals
is available in extraRowExpression.
Please consider that subtotals are incorrect when used together with pagination!
Usage with Twitter Bootstrap ¶
You can use it with bootstrap gridview via ext.groupgridview.BootGroupGridView widget.
Since 1.2 you need Yii-bootstrap 1.0 and above.
Note ¶
Extension uses only data that are on current grid page. You should take care about sorting to get correct results.
Fantastic
Wonderful extension, I'll make sure to use it on my projects. Many thanks!
super
nice one bruvaa :) May be next step would be ajax grouping with drag_drop on a GroupByBox in the header :) 10x for the extension.
nice
good one!
woww.. wonderfull
this extension has made me smile all day...
thank you vitalets, you make a nice extension..
(NB.. There is just a little bit problem with CButtonColumn class)
you are welcome)
hi all, glad you like it :)
peterjkambey, could you show what is the problem with CButtonColumn ?
CButtonColumn
I got error CButtonColumn.name is not defined.
Simple solution:
create class
class MyButtonColumn extends CButtonColumn { ..... public $name = 'Operations'; //Add this .... }
And in GridView columns:
array( 'class' => 'MyButtonColumn', ),
CLinkColumn Error?
Fantastic extension. Thank you.
@Pawel Kania -- your fix worked for the CButtonColumn. Thanks!
The same type of error occurs on a CLinkColumn -- 'name' not defined. I think we'll run into the same error on every column type that isn't standard. Perhaps this grouping component should have a default 'name' so it doesn't error out on the custom column types?
Fixed
'name' issue fixed in 1.1.
thanks for your feedback!
thank you for fixing, just one question
Hi,
thank you for fixing ... it work now, but just a little question, how to merge column on field in other table based on BELONGS_TO relation?... i try to put
'mergeColumns' => array('master.input_date'),
but it doesn't work. is it not possible?..
relations
hi peterjkambey,
for relations I think you should use value property:
'mergeColumns' => array('order_date'), 'columns' => array( ... array( 'name' => 'order_date', 'value' => '$data->master->input_date', ) )
hi vitalets
hi,
wonderful, thx for fast reply.
for column, it already work of course. I mean, I want to put it as merging Column (purpose of this extension)..:-D
details for relations
when you defined name and value for column, you can put it's name to mergeColumns array.
expanded a little my previous comment)
GroupGridView.... PERFECT!!!!
Bootstrap + GroupGridView..
It's just perfect!!!...
Great extension
I felt from the beginning that this extension was very interesting. And it is indeed. I've just used it (v1.1 on Yii 1.1.8) for a real case and it works great. Thank you for sharing your work.
You should correct in your demos for extra rows the css that disables selection for merged rows:
~~~
[css]
.grid-view td.extrarow
~~~
instead of
~~~
[css]
.grid-view td.merge
~~~
nice extension
nice, thanks
css corrected in demo
@abennouna
thanks! css corrected in demo.
name in CLinkColumn
Really nice application, fixed some of the issues I have been having.
Two things:
I still get the error "CLinkColumn.name is not defined", while using the 1.1 version.
array( 'name'=>'title', 'header'=>'Title', 'labelExpression'=>'$data->session->training->title', 'urlExpression'=>'Yii::app()->createUrl("/training/view", array("id"=>$data->session->training->id))', 'class'=>'CLinkColumn', ),
Secondly, I am also using a class DataColumn. The purpose of it is that it evaluates htmloptions. The downside is that a simple string option has to be enclosed in a string. Your code adds the "merge" htmlOption, is there any way I can make both work together out of the box? Or should I change my DataColumn code?
Edit: Changed my DataColumn class.
<?php /** * DataColumn class file. * Extends {@link CDataColumn} */ class DataColumn extends CDataColumn { /** * @var boolean whether the htmlOptions values should be evaluated. */ public $evaluateHtmlOptions = false; public $evaluatedOptions = array(); /** * Renders a data cell. * @param integer $row the row number (zero-based) * Overrides the method 'renderDataCell()' of the abstract class CGridColumn */ public function renderDataCell($row) { $data=$this->grid->dataProvider->data[$row]; if($this->evaluateHtmlOptions) { foreach($this->evaluatedOptions as $key=>$value) { $options[$key] = $this->evaluateExpression($value,array('row'=>$row,'data'=>$data)); } foreach($this->htmlOptions as $key=>$value) { $options[$key] = $value; } } else $options=$this->htmlOptions; if($this->cssClassExpression!==null) { $class=$this->evaluateExpression($this->cssClassExpression,array('row'=>$row,'data'=>$data)); if(isset($options['class'])) $options['class'].=' '.$class; else $options['class']=$class; } echo CHtml::openTag('td',$options); $this->renderDataCellContent($row,$data); echo '</td>'; } }
RE: name in CLinkColumn
hi Patrigan
[css] .merged { /* style for merged cells */ }
Hope this helps if I understand you right.
ecolumns
For some odd reason, my groupgridview now gives an error when I try to hide a column with ecolumns that should otherwise be grouped. Is there any possible solution to this?
Error 500 column or attribute not found
I also noticed an issue with Extra Row Column expressions and filter, if I display:none a column, it will still generate a filter field and the extra row will be one td too long due to this, any way how to handle this?
With greetings.
Demo site down
The demo site seems to be down :(
RE: Demo site down
yes, thank you, I also got notification from hosting.
Checking..
RE: Demo site down
demo site is online!
Re: snippets
hi fr0d0z,
please see demo, there are plenty of example snippets there
Great!
great extension!!
can it be exported to excel/pdf?
Hi,
Can I export the groupgridview to excel/pdf?
Can I paginate the gridview so that the merge column is displayed in one page?
Thank you for your help.
Cheers,
Daniel
Re: can it be exported to excel/pdf?
Hi,
Can I export the groupgridview to excel/pdf?
no. I think it's should be another extension that converts any CGridView 2 pdf(no matter with grouped cells or not).
Can I paginate the gridview so that the merge column is displayed in one page?
currently no. It would be great improvement of extension. But it seems not easy: now it is grouping data already fetched from database, and in you request extension should generate sql to fetch all rows for particular group (if I understand you right). You can have a look on source on github and may be find a pretty solution.
Hierarchical merge
Hi,
I was wondering is that possible to merge hierarchically? For example
merge to be
not
because I will mixed up the hierarchical process.
Thank you.
Re: Hierarchical merge
hi, is't it this ?
or please fix formatting in your third sample..
Re: Hierarchical merge
see the updated third example.
My point is that "bbb" is split up for "A123" and "A456". not merge since the first "bbb" is belong to "A123" and second one is belong to "A456".
What I need is that "bbb" that is merged only if it has same "A123" see example 2.
Thank you for quick support. Anyway, this is great extension and really useful.
Re: Hierarchical merge
I assume it should already work exactly this way..
Could you re-check the order of columns in
mergeColumns
?'mergeColumns' => array('col A', 'col B'), //not array('col B', 'col A')
column group in heading
Is it somehow possible to generate Tables like this?
With a grouped heading above some defined columns.
Re: column group in heading
hi, not possible now. But could be good feature!
Re: Hierarchical merge
Hi Vitalets,
still not working, I change the order, but resulting the same.
either
'mergeColumns' => array('location', 'transDate'),
or
'mergeColumns' => array('transDate', 'location'),
results are the same.
Re: Hierarchical merge
Hi, Daniel,
I found that it is controlled by
mergeType
option that was not documented.Please try to set
'mergeType' => 'nested',
I will update docs.
You are the Man!
great work.. you saved my life :D
echo "Thank You ~Vitalets~";
data provider
hello i was wondering if 'extraRowColumns' => array('name'), can also be a dataprovider?
is this extension possible for different models?
i have model1 and model2.
I need the model1 to be in the extraRowColumns and model2 as the content of the extraRowColumns.
they're both being passed in my view as dataprovider
group columns when its value has CHtml::link
hello all,
I have trouble with groupgridview when i merge the column which value has CHtml::link, mergeColumns not work at that time for me.
Reply me as soon as possible.
Thanks in advance.
Re: data provider & CHtml::link
@Bluenica
To have model1 in extrarow and model2 in content rows I think you should combine two dataproviders into single one. And then use it in groupgridview. Otherwise grid will not understand when exactly to insert extrarows.
@hiral darji
Could you show the your code snippet ?
RE: Vitalets
this is my code
$this->widget('ext.groupgridview.GroupGridView', array( 'id' => 'hostel-room-category-facility-grid', 'dataProvider' => $dataProvider, 'mergeColumns' => ('room_category_name'), 'filter'=>$model, 'columns' => array( array( 'header'=>'SN.', 'class'=>'IndexColumn', ), //'hostel_room_category_facility_id', array( 'name'=>'room_category_name', 'type'=>'raw', 'value'=>'CHtml::link($data->Rel_hostel_room_category->room_category_name,array("hostelRoomCategoryFacility/update","id"=>$data->hostel_room_category_facility_id))', ), array( 'name'=>'facility_name', // 'type'=>'raw', 'value'=> '$data->Rel_hostel_room_facility->facility_name', ), array( 'name'=>'room_category_facility_start_date', //'type'=>'raw', 'value'=>'date_format(new DateTime($data->room_category_facility_start_date), "d-m-Y")', ), array( 'name'=>'room_category_facility_end_date', 'value'=>'($data->room_category_facility_end_date == NULL) ? "Not Set" : date_format(new DateTime($data->room_category_facility_end_date),"d-m-Y")', ), array('name'=>'Organization', 'value'=>'Organization::model()->findByPk($data->hostel_room_category_facility_organization_id)->organization_name', 'filter' => false, ), array( 'class'=>'CButtonColumn', ), ), 'pager'=>array( 'class'=>'AjaxList', 'maxButtonCount'=>$model->count(), // 'maxButtonCount'=>25, 'header'=>'' ), ));
this is my code for grideview in this i want to group room_category_name field but with link it is not group. without link its work fine.
Re: CHtml::link
@hiral darji
It seems links are not grouped because they have different URLs (due to different
"id"=>$data->hostel_room_category_facility_id
).And that is correct. Otherwise, if they merged you will loss some urls from your grid.
You can try without id - it should work:
array( 'name'=>'room_category_name', 'type'=>'raw', 'value'=>'CHtml::link($data->Rel_hostel_room_category->room_category_name,array("hostelRoomCategoryFacility/update"))', ),
Hierarchical merge
Hi,
I was wondering is that possible to merge hierarchically? For example:
Maybe it relate to the database's table structure?
Now I resolved it by using javascript.
PDF embedding
Nice worked well, I was able to embedded a nicely grouped/merged grid onto a pdf
Update extraRowColumns via ajax
Hi people,
is there any way to update extraRowColumns dinamically via Ajax?
Thanks in advance :
Best Regards:
Martin
expandability
I alterd this a bit to add the possibility to expand and collapse the groups with the extrarow:
added options:
//the grouping options public $groupOptions = array( //allow collapsing of groups "expandable" => false, //set default collapsed value "expandDefault" => "expand", );
added jquery:
public function renderTableBody() { if(!empty($this->mergeColumns) || !empty($this->extraRowColumns)) { $this->groupByColumns(); } parent::renderTableBody(); //set collapse options if(isset($this->groupOptions['expandable'])) { if($this->groupOptions['expandable']) { //collapse click event Yii::app()->clientScript->registerScript('_collapse', " $('.extrarow').click(function(){ $(this).nextUntil('tr.extrarow').toggle(); }); "); //set the default collapse option if(isset($this->groupOptions['expandDefault'])) { if($this->groupOptions['expandDefault'] === 'contract') { //contract all groups Yii::app()->clientScript->registerScript('_collapse_trigger', " $('.extrarow').trigger('click'); ",CClientScript::POS_LOAD); } else { //expand all groups (default value) }} }} }
I needed the shift the extrarow class to the tr element, was the td element before:
echo '<tr class="extrarow">'; echo '<td colspan="'.$colspan.'">'.$content.'</td>'; echo "</tr>\r\n";
edit: extrarow can only be placed above the groups
Awesome extension!
Is it possible to get a mergeColumnExpression parameter that acts like the extraRowExpression param? I'd love to be able to display some totals along with the merge column.
Thanks again for the great work!
'extraRowTotals' example does not work
I tryed to use the 'extraRowTotals', but is not working.
Should i put this example in the view, right?
Best Regards
Thanks in advance
Martin
Export GroupGrid View
Hi,
Is it possible to export this gridview in excel?
use aliases for extraRowColumns
Hi, Vitalets.
Thanks for extension.
Is possible to use alieases for extraRowColumns and how?
I have sql and "year_num " is alias.
SELECT *, YEAR(t.created_at) as `year_num` FROM `order` `t` ORDER BY t.created_at ASC
'extraRowColumns' => array('year_num')
getting error "Column or attribute "year_num" not found! "
If remove $data->hasAttribute($column) from string #252 of GroupGridView.php that is working fine.
row number each group
halo Vitalets.. this is a great extension to display data by groups.. thank you! and now i want to display number of each row (continuously) by each groups..
@Harry Maulana
Use $row 'magic' variable that exists for each line being rendered. Example:
columns = array( array( 'header'=>'Number', 'value'=>'$row+1', // row is zero based ), )
There are recipes for this on the web, like this one. Search and you shall find...
get key by javascript ie : $.fn.yiiGridView.getSelection(id)
If you want to react when selection change you have a line like that in your grid :
'selectionChanged'=>"function(id){window.location='" . Yii::app()->urlManager->createUrl('controller/action', array('id'=>'')) . "' + $.fn.yiiGridView.getSelection(id);}",
But this didn't work well because of extra rows added by the extraRowColumns property of the grid. In order to make it work you have to override the renderKeys method from CBaseListView. Just add the code below into the GroupGridView class file.
/** * override CBaseListView->renderKeys to add an extra <span> tag * so the keys take in account extra row from GroupGridView */ public function renderKeys() { echo CHtml::openTag('div',array( 'class'=>'keys', 'style'=>'display:none', 'title'=>Yii::app()->getRequest()->getUrl(), )); $keys = $this->dataProvider->getKeys() ; $n=count($keys); for($row=0;$row<$n;++$row) { $extraRowEdge = null; if(count($this->extraRowColumns)) { $colName = $this->extraRowColumns[0]; $extraRowEdge = $this->isGroupEdge($colName, $row); if($this->extraRowPos == 'above' && isset($extraRowEdge['start'])) { echo "<span></span>"; } } echo "<span>".CHtml::encode($keys[$row])."</span>"; if(count($this->extraRowColumns) && $this->extraRowPos == 'below' && isset($extraRowEdge['end'])) { echo "<span></span>"; } } echo "</div>\n"; }
Hope it help.
mergeColumns with master property
Hi!
I'm trying to merge column with master property (i'm presenting details on the grid), but I'm failing.
My code is the following:
$this->widget('ext.groupgridview.GroupGridView', array( 'id' => 'grid1', 'dataProvider' => $invitations, 'mergeColumns' => 'invitation_id', 'summaryText'=>'', 'columns' => array( array( 'name' => 'Entidade', 'value' => '$data->invitation->entity', ), 'name', 'docId', ), ));
On mergeColumns, I would like to use $data->invitation->entity, but I can't seem to make it work. Exception:
Column or attribute "$data->invitation->entity" not found!
With invitation_id it doesn't group. It just shows all the rows.
Simple and perferct
Thanks bro, it's simple and perfect.
Merging columns with Chtml::link
That does not work because each row's link has a different 'id' attached to its 'text' value... besides, having a link in a grouped column, you actually won't have a specific record to point to. Remove the link for that column and it will group nicely.
If you have any questions, please ask in the forum instead.
Signup or Login in order to comment.