用user_status字段修改用户表中的imageUrl



我必须建模:User, UserFlag

在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("#", ""));
                                }
                            }',
                ),
    ),

我将从UserFlag模型中读取用户状态,如果状态是活动的,我显示1.png,如果状态是不活动的,我显示2.png。

是的,不幸的是,$data变量不能从imageUrl访问。我建议像Stu的链接建议的那样从CButtonColumn扩展。

如果您不想这样做,您可以创建两个按钮列,并根据状态显示它们。它应该是这样的,但如果您的活动user_status值不是1,或者您希望图像反转,则可能需要调整它:

'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("#", ""));
                                }
                            }',
                ),

你还需要在你的CButtonColumn模板中添加{disable}。

这是不理想的,因为你在重复代码,但至少你可以做到这一点,而不必扩展任何类。

最新更新