0

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.

4
  • Please give me your search method code Commented Jan 12, 2016 at 3:58
  • 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. Commented Jan 12, 2016 at 9:50
  • 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. Commented Feb 15, 2016 at 12:54
  • @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 :) Commented Feb 15, 2016 at 13:05

1 Answer 1

0

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]),
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks. But this code doesn't work. You can try it. In searchModel params loads array with only one values
Thanks. But this code doesn't work. You can try it. When SearchModel load params , column status has array with only one value. prntscr.com/9xzu8i screenshot. I think problem in javascript of gridview... PS . $query->andFilterWhere( "status IN (".implode(',',$this->stats).")" ) it same like $query->andFilterWhere("status" => $this->status); ActiveRecord automatically understand that $this->status is array, and add IN operator for that.
It works! I've done composer update, than delete all assets, and it begin works.
You can ignore the if statement if you use the andFilterWhereMethod in the following way: $query->andFilterWhere(['in', 'status', $this->stats]);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.