roymj88
(Roymj88)
May 9, 2013, 6:57am
1
Is it possible to check 3 conditions in the cgridview column ??
I have done the below one for checking two conditions :
array(
'name' => 'active',
'type' => 'raw',
'htmlOptions'=> array('class'=>'width50'),
'headerHtmlOptions'=>array('class' => 'width50'),
'header' => 'Active',
'value' => '(($data->active == 1) ? "Yes" : "No")',
),
Is it possible like :
array(
'name' => 'active',
'type' => 'raw',
'htmlOptions'=> array('class'=>'width50'),
'headerHtmlOptions'=>array('class' => 'width50'),
'header' => 'Active',
'value' => '(($data->active == 1) ? "Yes" : " check another condition here ? ")',
),
Or any suggestions for the best way to get around this ?
Tsunami
(Erik)
May 9, 2013, 7:28am
2
Of course, whatever you write there is evaluated as PHP, and you can nest ternary operators as much as you want.
'value' => 'expr1 ? expr2 : (expr3 ? expr4 : (expr5 ? expr6 : expr7) : expr8)',
If you do it too much it becomes hard to read though.
You can also just add a method to your model that returns the text and use ‘$data->methodName()’ instead.
roymj88
(Roymj88)
May 9, 2013, 8:59am
3
Tsunami:
Of course, whatever you write there is evaluated as PHP, and you can nest ternary operators as much as you want.
'value' => 'expr1 ? expr2 : (expr3 ? expr4 : (expr5 ? expr6 : expr7) : expr8)',
If you do it too much it becomes hard to read though.
You can also just add a method to your model that returns the text and use ‘$data->methodName()’ instead.
So basically it would be like this ? :
array(
'name' => 'active',
'type' => 'raw',
'htmlOptions'=> array('class'=>'width50'),
'headerHtmlOptions'=>array('class' => 'width50'),
'header' => 'Active',
'value' => '(($data->active == 1) ? "Yes" : " ($data->active == 0) ? "No" : "Nil" ")',
),
How will i go around the quotes (") issue ?.. .
mirunho
(D Mirecki)
May 9, 2013, 9:13am
4
Also You can use annonymous function, for example:
'value'=>function($data){
if ($data->active == 1){
$class = 'Yes';
}
else if ($data->active == 0){
$class = 'No';
}
else{
$class = 'Nil';
}
return $class;
},
And it seems a little bit more elegant for me.
roymj88
(Roymj88)
May 9, 2013, 9:26am
5
Awesome Mirunho,… Worked like magic… This was the method i was looking for … Just didnt know i could just write a function there… I was thinking of going all the way back to controller for this… …
Thanks you guys Mirunho & Tsunami… Cheers…
mirunho
(D Mirecki)
May 9, 2013, 9:31am
6
Glad to help, have a nice day.