简体   繁体   中英

Yii2 Gridview filtering not working

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'
                ],
            ]);

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

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;
    }

possibly issue is jquery.min.js in Asset directory try to comment and reload

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM