【问题标题】:yii2 grid filtering is not workingyii2 网格过滤不起作用
【发布时间】:2017-11-16 17:48:56
【问题描述】:

我的网格视图过滤器没有过滤类别,我已将 category_id 分配给 nesserray 地方,但它没有根据他们的 category_id 过滤我的帖子,请帮助我。 这是我的源代码:
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

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

【问题讨论】:

标签: php yii2 frameworks


【解决方案1】:

成功了,我必须删除 jQuery 文件

【讨论】:

    猜你喜欢
    • 2020-04-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-23
    • 1970-01-01
    相关资源
    最近更新 更多