bigferumdron
bigferumdron

Reputation: 49

Yii2 Gridview DropDownList Filter with multiple select

Please help me to solve the following issue.

I have a page with data that displayed with gridview. There is a column 'status'. I need dropdown filter by this column value.

For my column in grid view I set the following filter value:

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

Dropdown filter correctly display. But no matter how many options I choose, a search model gets an array with only one value .

I've tried many ways for this but doesn't find any solution. Thanks.

Upvotes: 0

Views: 5591

Answers (1)

Александр М
Александр М

Reputation: 21

Please read this. http://www.yiiframework.com/wiki/621/filter-sort-by-calculated-related-fields-in-gridview-yii-2-0/

And code must be:

search model

/* 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).")" );
}

}

view

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

Upvotes: 0

Related Questions