Yii 1.1: groupgridview

This extension allows to group data in your grid

GroupGridView extends CGridView with following features:

  • group equal values in merged cells
  • add extra rows for grouped values



Developed and tested on Yii 1.1.9.


Put groupgridview folder from zip to your protected/extensions.


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 of rowspan)
  • 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.


Extension uses only data that are on current grid page. You should take care about sorting to get correct results.

Total 20 comments

#20118 report it
vortal at 2017/08/20 12:05pm
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.

#19376 report it
Sigit Purnomo at 2015/06/08 10:20pm
Simple and perferct

Thanks bro, it's simple and perfect.

#18881 report it
kAIOSHIN at 2015/01/28 06:13pm
mergeColumns with master property


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',
   'columns' => array(
         'name' => 'Entidade',
         'value' => '$data->invitation->entity',

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.

#17959 report it
Jean Martin at 2014/08/18 02:52am
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(
        $keys = $this->dataProvider->getKeys() ;
        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.

#17846 report it
Boaz at 2014/07/29 07:07am
@Harry Maulana

Use $row 'magic' variable that exists for each line being rendered. Example:

columns = array(
    'value'=>'$row+1',       //  row is zero based

There are recipes for this on the web, like this one. Search and you shall find...

#16852 report it
Harry Maulana at 2014/04/03 09:40pm
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..

#16359 report it
maxxdev at 2014/02/14 07:00am
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.

#15963 report it
Vinod Agarwal at 2014/01/04 04:07am
Export GroupGrid View

Hi, Is it possible to export this gridview in excel?

#15947 report it
Martin Pascualon at 2014/01/02 12:50pm
'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

#15738 report it
.nate at 2013/12/10 05:15pm
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!

#15606 report it
martijnjonkers at 2013/11/28 04:02am

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)) {
    //set collapse options
    if(isset($this->groupOptions['expandable'])) {
    if($this->groupOptions['expandable']) {
        //collapse click event
        Yii::app()->clientScript->registerScript('_collapse', "
        //set the default collapse option
        if(isset($this->groupOptions['expandDefault'])) {
        if($this->groupOptions['expandDefault'] === 'contract') {
            //contract all groups
            Yii::app()->clientScript->registerScript('_collapse_trigger', "
        } 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

#15452 report it
Martin Pascualon at 2013/11/11 08:50am
Update extraRowColumns via ajax

Hi people,

is there any way to update extraRowColumns dinamically via Ajax?

Thanks in advance : Best Regards: Martin

#15405 report it
Paul_Kish at 2013/11/07 04:15am
PDF embedding

Nice worked well, I was able to embedded a nicely grouped/merged grid onto a pdf

#13351 report it
jinmmd at 2013/05/24 01:37am
Hierarchical merge


I was wondering is that possible to merge hierarchically? For example:

Col A  |     Col B  
       |      bbc
A123   |        | c123
       | bbb    |------
       |        | c456
       |        | c999
       | bbb    |------
A456   |        | c888
       | bzz    | c777

Maybe it relate to the database's table structure?

Now I resolved it by using javascript.

#12698 report it
Vitalets at 2013/04/06 06:58am
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:

#12680 report it
hiral darji at 2013/04/05 02:46am
RE: Vitalets

this is my code

$this->widget('ext.groupgridview.GroupGridView', array(
      'id' => 'hostel-room-category-facility-grid',
      'dataProvider' => $dataProvider,
      'mergeColumns' => ('room_category_name'),
      'columns' => array(
              //  'type'=>'raw',        
                'value'=> '$data->Rel_hostel_room_facility->facility_name',
                'value'=>'date_format(new DateTime($data->room_category_facility_start_date), "d-m-Y")',
                'value'=>'($data->room_category_facility_end_date == NULL) ? "Not Set" : date_format(new DateTime($data->room_category_facility_end_date),"d-m-Y")',
            'filter' => false,
//      'maxButtonCount'=>25,

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.

#12624 report it
Vitalets at 2013/04/02 03:11am
Re: data provider & CHtml::link

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 ?

#12622 report it
hiral darji at 2013/04/02 02:54am
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.

#12566 report it
Bluenica at 2013/03/29 03:47am
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

#11945 report it
junaidatari at 2013/02/14 03:05pm
You are the Man!

great work.. you saved my life :D

echo "Thank You ~Vitalets~";

Leave a comment

Please to leave your comment.

Create extension