【问题标题】:How to set a TbButtonColumn's visibility depending on a database field?如何根据数据库字段设置 TbButtonColumn 的可见性?
【发布时间】:2014-11-30 08:00:40
【问题描述】:

如何根据数据库字段使我的 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;
    }
}

编辑: 解决了!感谢小字节!为了其他遇到同样问题的人,我就是这样做的。

'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;
                            }
                        },

【问题讨论】:

    标签: php database yii cgridview


    【解决方案1】:
    '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
                        },
    

    【讨论】:

    • 我应该如何处理 $row_number @tinybyte ?
    • 哦,我现在明白了!太感谢了!我将操纵代码以满足我的需要。谢谢!
    猜你喜欢
    • 2016-11-24
    • 1970-01-01
    • 2017-08-25
    • 1970-01-01
    • 2023-03-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多