mazraara
(Mazraara)
June 21, 2012, 8:01am
1
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.
mazraara
(Mazraara)
June 21, 2012, 8:10am
3
[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]
bettor
(Live Webscore)
June 21, 2012, 8:33am
5
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
mbi
(mbi)
June 21, 2012, 8:34am
6
maybe this helps you, its german site, but you understand the code hopefully
http://blog.mbischof.de/anonyme-funktionen
Sisko
(Cjaegle)
June 21, 2012, 8:37am
7
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.
Sisko
(Cjaegle)
June 21, 2012, 8:40am
8
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.
mazraara
(Mazraara)
June 21, 2012, 8:48am
9
Thank all. but still confused.
mazraara
(Mazraara)
June 21, 2012, 9:53am
10
bettor
(Live Webscore)
June 21, 2012, 10:32am
11
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
mazraara
(Mazraara)
June 22, 2012, 5:27am
12
bettor:
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.
traprajith
(Traprajith)
October 3, 2012, 11:30am
14
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.?
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 '-';
}
}
Ravi005
(Bhalodiyaravi)
December 26, 2013, 1:02pm
16
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.