0

My gridview data

    day          modeler    total  
    2018-1-05     ABC        5                         
    2018-1-06     DEF        8        
    2018-1-06     CAB        3   
    2018-1-06     GHI        3   
    2018-1-06     KLM        3   

I have a gridview like this. Right now and can only filter modeler one by one. Can I have a multiple line searchbox and just paste in "ABC DEF CAB" and it will filter 3 results like below?

day          modeler    total  
2018-1-05     ABC        5                         
2018-1-06     DEF        8        
2018-1-06     CAB        3

My controller

public function actionIndex()
{
    $searchModel = new ModelerSearch();
    $dataProvider = $searchModel->search(Yii::$app->request->queryParams);

    return $this->render('index', [
        'searchModel' => $searchModel,
        'dataProvider' => $dataProvider,
    ]);
}

My search model

   public function search($params)
    {
        $query = Modeler::find();

        // add conditions that should always apply here

        $dataProvider = new ActiveDataProvider([
            'query' => $query,
        ]);

        $this->load($params);

        if (!$this->validate()) {
            // uncomment the following line if you do not want to return any records when validation fails
            // $query->where('0=1');
            return $dataProvider;
        }

        // grid filtering conditions
        $query->andFilterWhere([
            'id' => $this->id,
        ]);

        $query->andFilterWhere(['like', 'modeler', $this->modeler])
            ->andFilterWhere(['like', 'total', $this->total]);

        return $dataProvider;
    }
}

Thank you!

7
  • cant understand what you mean by "currently i filter models one by one" ? Commented Mar 2, 2018 at 8:04
  • i can i can just filter "ABC" or "DEF" only Commented Mar 2, 2018 at 8:24
  • so what i understand is that you want a single text field where if you enter "ABC" it should show you matching record and if you enter "ABC DEF" with space it should show you 2 records matching either one of them ? if it is correct you need to add the code related to your searchmodel , view and controller action Commented Mar 2, 2018 at 9:08
  • Sorry for the late update. I used Gii to generate CRUD. I think i don't need to add the code :) Commented Mar 6, 2018 at 1:19
  • i know but for suggesting update i wont be writing all the code :) , so its better you add the related code and i will update it accordingly where every needed, this way you can just replace the code at your end and test if it is correct or not, so kindly add the controller action and the search() function from the SearchModel Commented Mar 6, 2018 at 1:22

1 Answer 1

1

You need to search via the modeler field in the gridview and you want it to work in a way that if you enter "ABC" it should show you matching record and if you enter "ABC DEF" with space it should show you 2 records matching either one of them.

So, first of all, on top of your search Model add

private $_selections = [];

And then update your search function to the following

public function search( $params ) {
    $query = Modeler::find ();

    // add conditions that should always apply here

    $dataProvider = new ActiveDataProvider ( [
        'query' => $query ,
            ] );

    $this->load ( $params );

    if ( !$this->validate () ) {
        // uncomment the following line if you do not want to return any records when validation fails
        // $query->where('0=1');
        return $dataProvider;
    }

    if ( $this->modeler !== null && $this->modeler !== '' ) {
        $this->_selections = preg_split ( '/\s+/i' , $this->modeler );
        $query->andFilterWhere ( [ 'IN' , 'modeler' , $this->_selections ] );
    }
    // grid filtering conditions
    $query->andFilterWhere ( [
        'id' => $this->id ,
    ] );

    $query->andFilterWhere ( [ 'like' , 'total' , $this->total ] );

    return $dataProvider;
}

Now go to your gridview and in the modeler column type in the filter input "ABC DEF" and watch it happen.

Sign up to request clarification or add additional context in comments.

5 Comments

@SaoHo i updated the answer there was an extra check the second one it could be replaced with the first check if ( $this->modeler !== null && $this->modeler !== '' ) { so we can move the line $query->andFilterWhere ( [ 'IN' , 'modeler' , $this->_searchQuery ] ); inside it. see updated code.
yes i figured it out :) thank you, by the way, i tried filter about a thousand modeler, it returns error, the URL length exceeds the capacity limit. Do you know if I can fix it by increasing the limit?
are you getting error in console against the ajax request
Apache server only support 4000 characters in URL. If I filter a lot of value it will return this errors. Can I use "POST" instead of "GET" method for search model? If not then it's okay :)
yes you can but you need to change the queryParams to post in controller action and change the options for pjax see this post yiiframework.com/forum/index.php/topic/… @SaoHo

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.