D
D
Doniyor Mamatkulov2016-08-09 08:25:05
Yii
Doniyor Mamatkulov, 2016-08-09 08:25:05

Filters in GridView Yii2?

It is not possible to make filters to GridView in Yii2 - inputs and selects do not react at all.
Controller:

$dataprovider = new ActiveDataProvider([
           'query' => StUsers::find(),
            'pagination' => [
                'pagesize' => 10,
            ],
            'sort' => [
                'defaultOrder' => [
                    'id' => SORT_DESC,
                ],
            ],
        ]);
        $searchModel = new StUsers();
        return $this->render('index', [
            'dataProvider' => $dataprovider,
            'searchModel' => $searchModel
        ]);

Model
public function search($params) {
        $query = StUsers::find();
        $dataProvider = new ActiveDataProvider([
           'query' => $query,
        ]);

        if(!($this->load($params) && $this->validate())) {
            return $dataProvider;
        }

        $query->orFilterWhere(['LIKE', 'fullname', $this->fullname]);
        return $dataProvider;
    }

Performance:
echo GridView::widget([
               'dataProvider' => $dataProvider,
               'filterModel' => $searchModel,
               'columns' => [
                   'id',
                   'fullname',
                   [
                       'attribute' => 'phone_number',
                       'content'=>function($data){
                           return "+".$data['phone_number'];
                       },
                   ],

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Andrew, 2016-08-09
@mhthnz

And where is the processing of the phone_number field lost in your model? And why do you have orFilterWhere?
You need to add the phone_number field to the rules so that it is populated. And in the search function, add a search for it:

public function rules()
{
  return [
    [['fullname', 'phone_number', 'safe'],
  ];
}

public function search($params) 
{
    $query = StUsers::find();
    $dataProvider = new ActiveDataProvider([
        'query' => $query,
    ]);

    if(!($this->load($params) && $this->validate())) {
        return $dataProvider;
    }
    $query->andFilterWhere(['LIKE', 'phone_number', $this->phone_number]);
    $query->andFilterWhere(['LIKE', 'fullname', $this->fullname]);
    return $dataProvider;
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question