Skip to content
Advertisement

Yii2- Select2 button as Grid view filter doesn’t filter

I want to use Select2 dr as filter in Yii Grid view, but it doesn’t filter at all, only refreshes the page.

I take the data from 2 tables – from accounts I take only user_id, and from credits I take everything else. And every filter works, except And the ‘user_id’ one.

<?php

 echo GridView::widget(
     [
         'dataProvider' => $dataProvider,
         'filterModel' => $searchModel,
         'columns' => [
             [
                 'attribute' => 'wp_id',
                 'value' => 'accounts.user_id',
                 'filter' => Select2::widget(
                     [
                         'model' => $searchModel,
                         'attribute' => 'wp_id',
                         'data' => ArrayHelper::map(Accounts::find()->all(), 'wp_id', 'user_id'),
                         'options' => ['placeholder' => ' --Filter by user id-- '],
                         'language' => 'en',
                         'pluginOptions' => [
                             'allowClear' => true,
                         ],
                     ]
                 ),
             ],
         ],
     ]
); ?> 

Here is the action in the controller.

<?php
 public function actionIndex()
    {

        $searchModel = new CreditsSearch();
        $dataProvider = $searchModel->search(Yii::$app->request->queryParams);

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

And this is the search model method

<?php   
public function search($params)
    {
        $query = Credits::find()->with('accounts');

        $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(!empty($this->user_id)) {
            $query->andFilterWhere(['=', 'accounts.user_id', $this->user_id]);
        }
        // grid filtering conditions
        $query->andFilterWhere([
            'user_id' => $this->user_id,
            'wp_id' => $this->wp_id,
            'credits_bought' => $this->credits_bought,
            'purchase_date' => $this->purchase_date,
            'id' => $this->id,
        ]);

        return $dataProvider;
    }
} ?> 

Thanks in advance!

Advertisement

Answer

Because you are passing the wp_id under the GridView filter and selecting the wp_id from the Accounts model as the value of the select2-dropdown

ArrayHelper::map(Accounts::find()->all(), 'wp_id', 'user_id')

Although it is confusing that you use the gridview column attribute wp_id and use the select2 to filter the user_id for the same column but if you are looking to do that you might need to change the above to

ArrayHelper::map(Accounts::find()->all(), 'user_id', 'wp_id')
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement