Grouping Rows In Cgridview

Hi,

I have listed the values of two tables in a Cgridview.

I would like to know how to group two or more rows which have same values in one column (Eg: To display a user belonging to multiple groups in a single row)




<?php

$this->widget('zii.widgets.grid.CGridView', array(

        'id'=>'user-usergroup',

        'dataProvider'=> UserGroup::model()->search(),

        'columns'=>array(

            array(

'class'=>'CCheckBoxColumn',

                'selectableRows'=>'2',

),

        'user.admin_userid',


        array('name'=>'user.admin_user_name',

                'value'=>'$data->user->username'),

        'user.email',

        'user.handphone',

   

        array(

                'name'=>'UserGroup.name',

                'value' => '$data->UserGroup->username'

       ),

 

?>







Can anyone help me on how to achieve this?

Thanks & Regards,

Vidhya

Use Groupgridview

http://www.yiiframework.com/extension/groupgridview/

Hi Rahul,

Thank you for your reply.

GroupGridView does not help me to group the entire rows based on two column values.

Say for eg: I have a grid view with the following columns:

  1. username

2)Groupnames

3)Status

4)email

If I group based on username , then I get two rows for all other columns.

If I group based on username and status, I get most of the users combined.

But what I want is a single rows, which displays username, groupnames (separated by comma),status and email.

Kindly advice me on how to achieve the above.

Thanks & Regards,

Vidhya

http://www.yiiframework.com/wiki/278/cgridview-render-customized-complex-datacolumns/

Group user with groupname

write a method username() on model

‘columns’=>array(

         array(


                'header'=&gt;'username',


                'value'=&gt;array(&#036;model,'username'),


                'sortable'=&gt;true,


     ),

),

In model

public function username(&#036;data,&#036;row)


{


	&#036;user = USER::model()-&gt;findAllByAttributes(array('groupname'=&gt;&#036;data-&gt;groupname));


	if(&#036;user &#33;=NULL)


	{


	//return username of all users as a string


	}


	else


	{


		return '-';


	}


	


}

Hi Ranjith,

Thank you for your reply.

Could you please let me know if it is possible to display the categories in same row.

Thanks & Regards,

Vidhya

Hi Rahul,

I understood the flow.

But am getting the below error.

Could you please let me know where am going wrong?

Thanks & Regards,

Vidhya Prabha

Hi,

you can change the function like .

for ex…




public function Showusernamedd()

    {

        $user_details = User::model()->findAll();

        $dd_data = array();

        if(isset($user_details) && !empty($user_details))

        {

            foreach($user_details AS $user)

            {

                $dd_data[$user->id] = $user->firstname.' '.$user->lastname.' ('.$user->username.') - '.$user->user_type;                  

            }

        }      

        return $dd_data;

    }

using findAll methods will result a 2d array. $user is a 2d array. use it according to your use.

find categories in the function. and return as you wish. as comma separated ,as links etc…any thing.

Hi All,

Thank you so much for your help.

Thanks & Regards,

Vidhya

:) is it solved?

Hi Ranjith,

Solved it. Thank you :)

Regards,

Vidhya

Rajith!!!!

Noted Rajith…

Regards,

Vidhya

:) :)