2017-06-14 86 views
0

我的網格視圖過濾器沒有過濾類別,我已經給nesserray的地方category_id,但它沒有根據他們的category_id過濾我的帖子,請幫助我。 這裏是我的源代碼:
後 - >的index.phpyii2網格過濾不工作

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

,這裏是模型/ 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; 
    } 
} 
+0

你應該看看這個https://stackoverflow.com/questions/35509747/yii2-gridview-implement-filter-and-sort-for-values-for-related-table-of-foreign – scaisEdge

+0

Post什麼是$類別? – tigrasti

回答

0

它的工作,我不得不removle jQuery的文件