【问题标题】:Filter a gridview column that is processed by a function in yii2过滤一个由yii2中的函数处理的gridview列
【发布时间】:2018-03-15 05:37:25
【问题描述】:

我有一个名为wp_status 的列。如果 A 人 B 人 C 人 D 批准某项工作,则wp_status 列的值将更改为Approved,否则该值将与数据库中的值保持一致 - Assigned

gridview中动态改变值的代码是——

[
               'label' => 'Status',
               'attribute'=>'wp_status',
'value' => function ($model) {
                   return $model->Status();
               }
            ],

模型Workpermit中的函数Status是-

public function Status()
    {

        //$data = Workpermit::findOne($this->id);
        $total = $this->wp_status;

        if($this->wp_type == 'Safe Work Permit' && $this->wp_apoapproval == 'Approved' && $this->wp_spsapproval == 'Approved' && $this->wp_officerapproval == 'Approved'){ 
           $total = 'Approved';            
        }
        return $total;
    }

到目前为止,这工作正常。但我不确定如何使用 Kartik Select2 小部件对其进行过滤。我试过像以下 -

[
               'label' => 'Status',
               'attribute'=>'wp_status',
               'filterType'=>GridView::FILTER_SELECT2,
                'filter'=>ArrayHelper::map(Workpermit::Status()->asArray()->all(), 'total', 'total'),
                'filterWidgetOptions'=>[
                'pluginOptions'=>['allowClear'=>true],
                                    ],
                'filterInputOptions'=>['placeholder'=>'Permit Status'],
               'value' => function ($model) {
                   return $model->Status();
               }
            ],

在这里我遇到了错误 - Using $this when not in object context

【问题讨论】:

  • 您在过滤器值中调用Workpermit::Status(),该过滤器值将此方法视为静态。不应该是Workpermit::find() 吗?
  • 您好 Bizley,在数据库中的 wp_status 列中,我没有值“已批准”。当 4 人批准时,它会动态分配给该列。如果我将过滤器设置为 (Workpermit::find()->asArray()->all(), 'wp_status', 'wp_status' ) 我在过滤器选项中没有得到值 'Approved'。
  • 好吧,但是你不能调用静态方法并在内部使用$this而不实例化新对象。
  • 是的。我也是从其他帖子中了解到的。但是解决方法是什么?我的目标是将 wp_status 列的值更改为 Approved,当 3 个字段(wp_apoapproval、wp_spsapproval、wp_officerapproval)更新为 Appoved 时。我还需要使用 Kartik select2 小部件过滤列。让我知道解决它的其他方法。

标签: yii2


【解决方案1】:

可以使用吸气剂

public function getStatus()
{

    //$data = Workpermit::findOne($this->id);
    $total = $this->wp_status;

    if($this->wp_type == 'Safe Work Permit' && $this->wp_apoapproval == 'Approved' && $this->wp_spsapproval == 'Approved' && $this->wp_officerapproval == 'Approved'){ 
       $total = 'Approved';            
    }
    return $total;
}

调用$model->status(状态小写)

[
           'label' => 'Status',
           'attribute'=>'wp_status',
           'value' => function ($model) {
               return $model->status;
           }
        ],

【讨论】:

  • 嗨 scaisEdge,出现以下错误 - '调用未知方法:frontend\modules\workpermit\models\Workpermit::status()'
  • @Tanmay 使用return $model->status or $model->getStatus()
  • 现在,我得到了输出。一个标准仍然存在。我必须过滤列。现在,在 wp_status 列中,我有 2 个条目 - 1. 已分配(来自数据库),2. 已批准(我们正在动态实施)。我们如何过滤已批准?当我过滤 Approved 时,gridview 中没有显示任何条目。我在 WorkpermitSearch 模型中有以下内容 - "->andFilterWhere(['like', 'wp_status', $this->wp_status])'
  • @Tanmay 对于过滤器,您必须更改相关的 modelSearch 。您可以查看本教程以过滤计算字段,请参阅场景 1 yiiframework.com/wiki/621/…
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-02-06
  • 2015-02-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多