Answer the question
In order to leave comments, you need to log in
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,
]);
}
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;
}
<?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
the first is to add:
public function scenarios()
{
return Model::scenarios();
}
// grid filtering conditions
$query->andFilterWhere([
'id' => $this->id,
]);
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question