1

Can you help me out with implementing filters to GridView in Yii2? Right now, my rendered table does not respond to my actions (search GET params are not added, nothing changes if I enter a query to a filter input). Here's my code: Controller:

$searchModel = new UserSearch();
        $dataprovider = $searchModel->search(\Yii::$app->request->get());
        return $this->render('index', [
            'dataProvider' => $dataprovider,
            'searchModel' => $searchModel
        ]);

Model (UserSearch.php):

public $fullname;

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

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

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

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

View:

GridView::widget([
            'dataProvider' => $dataProvider,
            'filterModel' => $searchModel,
            'columns' => [
                'id',
                'fullname'
                ],
            ]);
HugeD
  • 45
  • 2
  • 8

3 Answers3

0

I have the same problem when filtering in DataView. Perhaps the problem is in the client side. Check again if your jquery called twice on your page ( browser/source code ). May be your problem related with this also : jQuery(...).yiiGridView is not a function

Community
  • 1
  • 1
anjarwidi83
  • 446
  • 3
  • 8
  • 2
    You should add a brief description of the solution, not only the link. Furthermore, if the question is already answered, you should flag the question as duplicated – Iván Rodríguez Torres Jan 23 '17 at 20:42
0

There should be no need for a solution since 4 years have passed. But, problem is in next statement:

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

Change it to:

    $this->load($params);

    if (!$this->validate()) {
        return $dataProvider;
    }
neolodor
  • 75
  • 1
  • 7
0

possibly issue is jquery.min.js You should referencing not more than once Try to see if you have repeated.

  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Albert Logic Einstein Aug 23 '22 at 14:44