0
votes

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.

1
Please give me your search method code - Hiren Bhut
My filter work correctly when dropdown list not multiple - so problem not in filter.As I understand, searchmodel code must me equal in both cases. Because Yii automaticly check $this->status (array on not array ) in where clause in serchmodel $query->andFilterWhere([ 'status' => $this->status, ]); So both variant automaticcly must work correctly. - bigferumdron
I'm having the same problem. You said it fixed after run composer update and deleting the assets. I did the same but it didn't work. Can you please write an answer with your view code (the gridview) ? Everytime i click (holding ctrl) more than one option or select all at same time, it only search for the last one. - Clyff
@bigferumdron Okay nevermind, I was running composer update in the wrong application haha, my bad. But I still think you should write your own answer and accept it, it helped me, ty :) - Clyff

1 Answers

0
votes

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]),