transistor
(Jlsistemas)
1
I have this common situation where I need to display the status of a row, for example, "active" or "inactive".
This status is a button, so when I click it, I change the status of the row, from "active" to "inactive" and vice-versa.
What I usually do, is that I represent status "active" with an icon, and "inactive" with a different icon, but the button performs the same action.
How can I do this with Yii Grid?
To display a different icon when a row is active and a different one when it is not active?
Thanks.
transistor
(Jlsistemas)
2
I’ll answer my own question.
After posting this, I got an idea, since only one state is possible I could have two buttons: show the one with the status and hide the other.
The code looks like this:
array(
'class'=>'CButtonColumn',
'template'=>'{activate} {deactivate} {update} {delete} {print} {cancel}', // <-- TEMPLATE WITH THE TWO STATES
'htmlOptions'=>array(
'width'=>80,
),
'buttons' => array(
'activate'=>array(
'label'=>'Activate',
'url'=>'Yii::app()->createUrl("controller/active", array("id"=>$data->id))',
'imageUrl'=>'images/icons/inactive.png',
'visible'=> '$data->active == 0', // <-- SHOW IF ROW INACTIVE
),
'deactivate'=>array(
'label'=>'Deactivate',
'url'=>'Yii::app()->createUrl("controller/active", array("id"=>$data->id))',
'imageUrl'=>'images/icons/active.png',
'visible'=> '$data->active == 1', // <-- SHOW IF ROW ACTIVE
),
'print'=>array(
'label'=>'Print',
'url'=>'Yii::app()->createUrl("controller/print", array("id"=>$data->id))',
'imageUrl'=>'images/icons/printer.png',
),
'cancel'=>array(
'label'=>'Cancel',
'url'=>'Yii::app()->createUrl("controller/cancel", array("id"=>$data->id))',
'imageUrl'=>'images/icons/cancel.png',
),
),
),
Hope this is helpful to someone.
Magic! exactly what I needed, thanks for sharing.
doodle
even though this is an excellent idea, did you find any alternative for this solution?
tks
shah83
(Shahriza Khairudin)
5
thanks @transistor! it helps.
'buttons' => array(
'activate'=>array(
'label'=>'Activate',
'url'=>'Yii::app()->createUrl("controller/active", array("id"=>$data->id))',
'imageUrl'=>'$data->active == 0 ? "images/icons/inactive.png" : "images/icons/active.png"',
),
this may work as well , I think.
andybegin
(Zhexiao)
7
Thanks , It’s good for me .