Is this possible in YII Frameworks cGridview ???

Hi,

please have a look a the attached image.

I need to display multiple values in a gridview coloumn.

for example. when a user has many roles, i want the user to be displayed as a one rwo in gridview and have comma separated values for his roles.

like, user hello can have admin, employee & superadmin roles.

now what i am getting is 3 rows for the same user called hello… where in each cgridview rows roles are different.

hello | admin

hello | employee

hello | super admin

but i want them to be this way, hello | admin,employee,superadmin

please let em know i am not explaining clearly.

Thanks.

Any help highly appreciated.

up

[size="4"]Sorry. the image is not getting uploaded here in forum. So pls take a look at the below given link. http://imageshack.us/f/4/41567199.png/

tnx alot.[/size]

Here is your image

one of the simplest approaches would be to call a method in your value attribute which returns all roles formated as you wish:


'value'=>'$model->roles($data->user_id)',

where roles() is a function which takes the user_id and queries/returns all roles

hope this helps

maybe this helps you, its german site, but you understand the code hopefully

http://blog.mbischof.de/anonyme-funktionen

I am not sure if that can be done without a lot of effort with a CActiveDataProvider. You could prepare an array for a CArrayDataProvider then it should be possible.

You could try to extend a CDataColumn and group the roles together. But that seems like a hack to me and I did not think this to the end. Or use a grid in grid. I think there were some threads or wiki entries regarding that.

Maybe someone else has a better solution.

You can get almost everything in a column, but if I see this right, the sql query results in unwanted rows which should be merged partly into one row.

Thank all. but still confused. :(

Anyone there to help me plsss ??????????????????????????????

try what mbi suggested:




'columns' => array(

    array(

        'name' => 'Roles',

        'type' => 'html',

        'value' => function($data) {

            ...here select the roles for the respective id such as:

            $roles = Roles::model()->findAll('userid=:id', array(':id'=>$data=>userid));

            foreach($roles as $role)

              $display .= $role.',';

             return $display;

        }

    ),

)



and then just customize the print

Thanks alot. it helped me. :)

i am done with that job.

Can you explain how the roles need to be assigned.

This is the simplest method




'columns'=>array(

		

		

		array(

		    'header'=>'Student Name',

			'value'=>array($model,'studentname'),//function name in the model

			'name'=> 'firstname',

            'sortable'=>true,


		

		),



in model




public function studentname($data,$row)

    {

		$student = Students::model()->findByAttributes(array('id'=>$data->student_id));

		if($student!=NULL)

		{

		return $student->first_name;

		}

		else

		{

			return '-';

		}

		

	}



Actually this is not working .Can you specify some other methods.?

Try this,




  array(

           'header'=>'Student Name',

           'value'=>'$data->myfunction($data->id)',//myfunction is function name in the model

           'name'=> 'firstname',

       ),



Create function in same model for which you want to display data in gridview.

In Model,




public function myfunction($i)

{

   $s_mod = StudentModel::model()->findByPk($i)

   $n = $s_mod->first_name.'|'.$s_mod->middle_name.'|'.$s_mod->last_name;

   return $n;

}



This is just an example. By this way you can return any value which you want to display in gridview.