yii2网格过滤不起作用 [英] yii2 grid filtering is not working

查看:81
本文介绍了yii2网格过滤不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的网格视图过滤器未过滤类别,我已将类别ID指定给nesserray场所,但尚未根据其类别ID过滤我的帖子,请帮助我. 这是我的源代码:
post-> index.php

my grid view filter is not filtering categories, I have given category_id to nesserray places but yet it is not filtering my posts according to their category_id, please help me. here are my source codes:
post-->index.php

<?= GridView::widget([
        'dataProvider' => $dataProvider,
        'filterModel' => $searchModel,
        'columns' => [
            ['class' => 'yii\grid\SerialColumn'],

            'title',
            [
            'attribute'=>'category_id',
             'value'=>function($model) {
            return $model->category->name;
             },
             'filter'=>$category
            ],
                [
                    'attribute'=>'user_id',
                    'value'=>function($model) {
                        return $model->user->fullname;
                    },
                    'filter'=>$user,
                    'label'=>'user'
                ],

            'category.name',
            'user.fullname',
//            'user_id',

//            'description',
//            'content:html',
             'count_view',
             'status',
             'created_at',

            ['class' => 'yii\grid\ActionColumn'],
        ],
    ]); ?>

这是model/postSearch.php

and here is model/postSearch.php

<?php

namespace app\models\search;

use Yii;
use yii\base\Model;
use yii\data\ActiveDataProvider;
use app\models\post;

/**
 * PostSearch represents the model behind the search form about `app\models\post`.
 */
class PostSearch extends post
{
    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['id', 'user_id','category_id', 'count_view'], 'integer'],
            [['title', 'description', 'content', 'status', 'created_at'], 'safe'],
        ];
    }

    /**
     * @inheritdoc
     */
    public function scenarios()
    {
        // bypass scenarios() implementation in the parent class
        return Model::scenarios();
    }

    /**
     * Creates data provider instance with search query applied
     *
     * @param array $params
     *
     * @return ActiveDataProvider
     */
    public function search($params)
    {
        $query = post::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,
            'category_id' => $this->category_id,
            'user_id' => $this->user_id,
            'count_view' => $this->count_view,
            'created_at' => $this->created_at,
        ]);

        $query->andFilterWhere(['like', 'title', $this->title])
            ->andFilterWhere(['like', 'description', $this->description])
            ->andFilterWhere(['like', 'content', $this->content])
            ->andFilterWhere(['like', 'status', $this->status]);

        return $dataProvider;
    }
}

推荐答案

它奏效了,我不得不删除jQuery文件

it worked, I had to removle jQuery files

这篇关于yii2网格过滤不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆