【问题标题】:remove button from CGridView with condition使用条件从 CGridView 中删除按钮
【发布时间】:2016-03-21 14:04:08
【问题描述】:

您好,我在 yii 中有 CRUD 生成的 CGridView。如果 appointment_status(CGridView 列之一)值等于 0

,我需要向 CGridView 行添加一个新按钮并隐藏它

这是我的 CGridView 代码,

$this->widget('zii.widgets.grid.CGridView', array(
    'id' => 'bookings-grid',
    'dataProvider' => $model->search(),
    'filter' => $model,
    'columns' => array(
        'id',
        'name',
        'email',
        'telephone',
        'time',
        'employee',       
        'appointment_status',
        'client_ip',
        'link' => array(
            'header' => 'Confirmation',
            'type' => 'raw',
            'value' => 'CHtml::button("$data->appointment_status",array("onclick"=>"document.location.href=\'".Yii::app()->controller->createUrl("controller/action",array("id"=>$data->id))."\'"))',
            'visible'=>$data->appointment_status==1,
        ),
        array(
            'class' => 'CButtonColumn',
        ),
    ),
));

但我得到的只是错误说明,

未定义变量:数据

如果有人能调查一下,那将是很大的帮助。

【问题讨论】:

    标签: yii cgridview


    【解决方案1】:

    您的“可见”处理列可见性而不是按钮,您可以使用模型上的自定义属性来创建和处理按钮可见性。

    添加到您的模型中:

    public function getConfirmationButton()
    {
       if ($data->appointment_status == 1) {
          return CHtml::button($this->appointment_status,array("onclick"=>"document.location.href=\'".Yii::app()->controller->createUrl("controller/action",array("id"=>$this->id))."\'"));
       } else {
          return '';
       }
    }
    

    并在你的视图中调用它:

    ..........
    'link' => array(
            'header' => 'Confirmation',
            'type' => 'raw',
            'value' => '$data->confirmationButton',
        ),
    ...........
    

    【讨论】:

      【解决方案2】:

      visible 是一个布尔值或 PHP 表达式,将被计算为一个布尔值。在评估期间,$data 被分配给使用的 dataProvider 中的当前项目。 $data 在评估函数 evaluateExpression() 之外不存在。因此,实现应该是:

      `visible` => '$data->appointment_status == 1',
      

      【讨论】:

      • 非常感谢您的回答。现在错误没有出现,但遗憾的是,即使 appointment_status 等于 0,它也会显示按钮
      【解决方案3】:

      您需要在链接数组中引用可见键的值。所以不要这样:

      'visible'=>$data->appointment_status==1
      

      应该是:

       'visible'=>'$data->appointment_status==1'
      

      它现在应该可以工作了。

      【讨论】:

      • 非常感谢您的回答。现在错误没有出现,但遗憾的是,即使 appointment_status 等于 0,它也会显示按钮
      • 您为什么不尝试获取有关它为什么不起作用的更多信息。硬编码 true 和 false 为可见,看看它是否会工作。如果它有效,那么你的问题要么出在表达式中,要么出在你写专栏的方式上。
      • 我写专栏的方式不会错,因为它来自CRUD。当我添加 true 或 false 时它会正常工作......
      • 那么,如果一切正常,您的陈述就是不正确的。尝试以某种方式 var_dump 它,看看你得到了什么。也试试这个:'(($data->appointment_status == 1) ? true : false)'
      【解决方案4】:

      你会得到未定义的变量,因为visible 不允许任何回调。

      试试这个解决方案,它是 yii2 代码,我对 Yii 了解不多。

      'delete' => function ($url, $model) {
                          return ($model->isVisible($model)) ? 
                          Html::a('<span class="glyphicon glyphicon-trash"></span>', 
                          $url, 
                          ['title' => Yii::t('app', 'Delete')]) : '';
      
      public static function isVisible($data)
      {
          return ($data->appointment_status == 1) ? true : false;
      }
      

      【讨论】:

        【解决方案5】:

        你可以这样使用:

         $this->widget('zii.widgets.grid.CGridView', array(
            'id' => 'bookings-grid',
            'dataProvider' => $model->search(),
            'filter' => $model,
            'columns' => array(
                'id',
                'name',
                'email',
                'telephone',
                'time',
                'employee',
                'appointment_status',
                'client_ip',
                'link' => array(
                    'header' => 'Confirmation',
                    'type' => 'raw',
                    'value' => function ($data) {
                        if ($data->appointment_status == 1) {
                            return CHtml::button("$data->appointment_status", array("onclick" => "document.location.href=\'" . Yii::app()->controller->createUrl("controller/action", array("id" => $data->id)) . "\'"));
                        } else {
        
                            return;
                        }
                    }
                        ),
                        array(
                            'class' => 'CButtonColumn',
                        ),
                    ),
                ));
        

        【讨论】:

          猜你喜欢
          • 2012-03-14
          • 1970-01-01
          • 1970-01-01
          • 2020-02-03
          • 1970-01-01
          • 2012-05-19
          • 2016-10-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多