I have to model: User, UserFlag In User/index added a column in CGridView:

array(
    'class' => 'CButtonColumn',
    'htmlOptions' => array("style" => 'width: 45px;'),
    'template' => '{enable}',
    'header' => 'management',
    'buttons' => array(
            'enable' => array(
                'name' => 'enable',
                'imageUrl' => Yii::app()->baseUrl."/images/ico/group.png",
                'url' => '"#".$data->username',
                'click' => 'js:function() {
                    if(confirm("Are you sure?")){
                                    changeUserStatus($(this).attr("href").replace("#", ""));
                                }
                            }',
                ),
    ),

I will read user status from UserFlag Model and if status is active I show 1.png and if status is deactive I show 2.png.

有帮助吗?

解决方案

Yeah, the $data var isn't accessible from imageUrl unfortunately. I would recommend extending from CButtonColumn like Stu's link suggested.

If you don't want to do that you could create two button columns and show them depending on the status. It would be something like this but you may need to adjust it if your active user_status value isn't 1 or you want the images reversed:

'enable' => array(
                'name' => 'enable',
                'visible'=>'$data->user_status == 1'
                'imageUrl' => Yii::app()->baseUrl."/images/ico/1.png",
                'url' => '"#".$data->username',
                'click' => 'js:function() {
                    if(confirm("Are you sure?")){
                                    changeUserStatus($(this).attr("href").replace("#", ""));
                                }
                            }',
                ),

'disable' => array(
                'name' => 'disable',
                'visible'=>'$data->user_status == 0'
                'imageUrl' => Yii::app()->baseUrl."/images/ico/0.png",
                'url' => '"#".$data->username',
                'click' => 'js:function() {
                    if(confirm("Are you sure?")){
                                    changeUserStatus($(this).attr("href").replace("#", ""));
                                }
                            }',
                ),

You would also need to add {disable} to your CButtonColumn template.

It's not ideal since you're repeating code, but at least you can do it without having to extend any classes.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top