D
D
Doniyor Mamatkulov2016-08-09 15:33:49
Yii
Doniyor Mamatkulov, 2016-08-09 15:33:49

Why is filter not working in GridView in Yii2?

Actually, the GridView filters in Yii2 do not work (there are no signs of life on my actions). My Code:
Controller

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

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

Search model
public function search($params)
    {
        $query = StUsers::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,
            'passport_number' => $this->passport_number,
            'passport_issue' => $this->passport_issue,
            'passport_validity' => $this->passport_validity,
            'dob' => $this->dob,
            'discount' => $this->discount,
            'block' => $this->block,
            'block_admin_id' => $this->block_admin_id,
            'register_date' => $this->register_date,
            'alive' => $this->alive,
        ]);

        $query->andFilterWhere(['like', 'fullname', $this->fullname])
            ->andFilterWhere(['like', 'passport_serie', $this->passport_serie])
            ->andFilterWhere(['like', 'passport_authority', $this->passport_authority])
            ->andFilterWhere(['like', 'sex', $this->sex])
            ->andFilterWhere(['like', 'residence_address', $this->residence_address])
            ->andFilterWhere(['like', 'phone_number', $this->phone_number])
            ->andFilterWhere(['like', 'password', $this->password])
            ->andFilterWhere(['like', 'email', $this->email])
            ->andFilterWhere(['like', 'block_reason', $this->block_reason]);

        return $dataProvider;
    }

Performance
<?php Pjax::begin(); ?>
    <?= GridView::widget([
        'dataProvider' => $dataProvider,
        'filterModel' => $searchModel,
        'columns' => [
            'id',
            'fullname'
         ]); ?>
<?php Pjax::end(); ?>

Answer the question

In order to leave comments, you need to log in

2 answer(s)
I
Igor Vasiliev, 2016-08-11
@Isolution666

the first is to add:

public function scenarios()
    {
        return Model::scenarios();
    }

it is obligatory to indicate the bonds from above:
Second:
// grid filtering conditions
        $query->andFilterWhere([
            'id' => $this->id,
        ]);

Remove all unnecessary.
This is the logic of Yii2 :)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question