Yii 1.1: groupgridview

This extension allows to group data in your grid
91 followers

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 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.

Note

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

Total 20 comments

#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
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

#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

Hi,

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:

array(
                'name'=>'room_category_name',
                'type'=>'raw',
              'value'=>'CHtml::link($data->Rel_hostel_room_category->room_category_name,array("hostelRoomCategoryFacility/update"))',
        ),
#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'),
      '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.

#12624 report it
Vitalets at 2013/04/02 03:11am
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 ?

#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~";
#11790 report it
Vitalets at 2013/02/04 06:30am
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.

#11783 report it
Daniel at 2013/02/03 10:06pm
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.

#11636 report it
Vitalets at 2013/01/24 05:47am
Re: column group in heading

hi, not possible now. But could be good feature!

#11635 report it
horizons at 2013/01/24 04:26am
column group in heading

Is it somehow possible to generate Tables like this? With a grouped heading above some defined columns.

Col A  | Group Heading   | Col D 
       | Col B  | Col C  |       
---------------------------------
A123   | bbb    | c123   | d123  
---------------------------------
A123   | bbb    | c456   | d123  
---------------------------------
A456   | bbb    | c999   | d123  
---------------------------------
A456   | bbb    | c888   | d123  
---------------------------------
A456   | bzz    | c777   | d123
#11466 report it
Vitalets at 2013/01/15 06:42am
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')

Leave a comment

Please to leave your comment.

Create extension