Yii2 Gridview DropDownList过滤多个选择

时间:2016-01-11 14:47:03

标签: filter yii2

请帮我解决以下问题。

我有一个页面,其中包含使用gridview显示的数据。列有一个列状态'。我需要按此列值下拉过滤器。

对于网格视图中的列,我设置了以下过滤器值:

'filter' => Html::activeDropDownList($searchModel, 'status', 
  Accounts::getStatusList(), ['class' => 'form-control', 'multiple' => true]),

下拉过滤器正确显示。但无论我选择多少选项,搜索模型都会得到一个只有一个值的数组。

我已经尝试了很多方法,但没有找到任何解决方案。感谢。

1 个答案:

答案 0 :(得分:0)

请阅读此内容。 http://www.yiiframework.com/wiki/621/filter-sort-by-calculated-related-fields-in-gridview-yii-2-0/

代码必须是:

搜索模型

/* your search attribute */
public $stats;

/* setup rules */
public function rules() {
   return [
    /* your other rules */
    [['stats'], 'safe']
   ];
}

public function search($params) {

/** some code **/

  $this->load($params);

/** some code **/

 if ($this->stats != null && count ($this->stats)>0) {
//this bad practic, but I don't find right way use " IN "
    $query->andFilterWhere( "status IN (".implode(',',$this->stats).")" );
}

}

查看

'filter' => Html::activeDropDownList($searchModel, 'stats', 
  Accounts::getStatusList(), ['class' => 'form-control', 'multiple' => true]),