如何根据数据库字段设置TbButtonColumn的可见性?

时间:2014-11-30 08:00:40

标签: php database yii cgridview

如何根据数据库字段使TbButtonColumn不可见?我的按钮放在CGridView中。如果某个字段为空,则应隐藏按钮。但我似乎无法使其发挥作用。谁能帮我?谢谢!这是我的代码:

cGridViewPending.php

'class'=>'bootstrap.widgets.TbButtonColumn',
        'template'=>'{updateView}{updateView2}',
        'buttons'=>array(
            'updateView'=>
                array(
                    'url'=>'$this->grid->controller->createUrl("SendEmail", array("id"=>$data["id_schedule"]))',
                    'label'=>'Approve',
                    'visible'=>$this->fetchRemarks(), //this is where I am calling the function to hide the button.
                    'click'=>'function(){return confirm("Are you sure you want to approve this reservation? It is highly recommended to put a remark before approving this reservation request.");}',
                    'options' => array (
                        'class'=>'btn btn-medium btn-success',
                        'icon'=>'icon-circle-arrow-right',
                    ),
                ),

ScheduleController.php

public function fetchRemarks()
{
    $id = Yii::app()->request->getQuery('id');
    $dataModel = Schedule::model()->findByPk($id);

    //Remarks
    $checkRemarks = "SELECT remarks FROM schedule WHERE id_schedule = $dataModel;
    $x = Yii::app()->reservation_db
        ->createCommand($checkRemarks)
        ->queryScalar();

    if ($x = null)
    {
        return false;
    }
}

编辑: 解决了!感谢tinybyte!这是我如何做到的,为了遇到同样问题的其他人。

'url'=>'$this->grid->controller->createUrl("SendEmail", array("id"=>$data["id_schedule"]))',
                    'label'=>'Approve',
                    'visible'=>function($row_number, $model){
                            if ($model->remarks == 0) //the 'remarks' is my basis for the visibility of the TbButtonColumn.
                            {
                                return false;
                            }
                            else
                            {
                                return true;
                            }
                        },

1 个答案:

答案 0 :(得分:2)

'updateView'=>
            array(
                'url'=>'$this->grid->controller->createUrl("SendEmail", array("id"=>$data["id_schedule"]))',
                'label'=>'Approve',
                'visible'=>function($row_number, $data){ // $data is your model record

                        // here check whether to show the button or not
                        return false; //not show this button
                    },