2016-02-06 38 views
1

嗨,我有一個yii代碼,我可以顯示模型中的數據,但搜索欄沒有在gridview中顯示,請大家幫忙。GridView中的YII2中的過濾器設置

的GridView代碼:

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

      // 'emp_attendance_pid', 
      [ 

        'label' => 'Employee ID', 
        'attribute' => 'emp_history_id', 
        'value' => 'emp_history_id', 
      ], 
      [ 
        'label' => Yii::t('app','First Name'), 
       // 'attribute' => 'emp_history_id', 
        'value' => 'empHistory.emp_first_name', 
       'filter' => ArrayHelper::map(app\modules\employee\models\EmpInfo::find()->all(), 'emp_info_id', 'emp_first_name') 
      ], 
      [ 
        'label' => Yii::t('app','Last Name'), 
       // 'attribute' => 'emp_history_id', 
        'value' => 'empHistory.emp_last_name', 
       'filter' => ArrayHelper::map(app\modules\employee\models\EmpInfo::find()->all(), 'emp_info_id', 'emp_last_name') 
      ], 

在這裏你可以看到過濾器,在我能夠使用arrayhelper來獲取數據,但我需要讓我在搜索框中我給的數據,搜索盒子也沒有來,只有下拉正在提供

+0

請出示相關的控制器/動作..和相關ModelSearch .. – scaisEdge

+0

$ query = EmpHistory :: find(); $ dataProvider = new ActiveDataProvider(['''''''''>'$ query',';'); $ this-> load($ params); (!$ this-> validate()){ return $ dataProvider;如果(!$ this-> validate()){ 返回$ dataProvider; ($'this-> emp_history_pid'=> $ this-> emp_history_pid,'emp_history_id'=> $ this-> emp_history_id,'emp_history_date'=> $ this-> dbDateSearch($ this-> emp_history_date), } $ query-> andFilterWhere ]); $ query-> andFilterWhere(['like','emp_history_process',$ this-> emp_history_process]) - > andFilterWhere(['like','emp_history_remarks',$ this-> emp_history_remarks]); – Paventhan

+0

這是我的搜索動作 – Paventhan

回答

1

參數屬性是必需的過濾。

如果您定義過濾器屬性,您將有一個下拉列表。 據我所知,你想要文字輸入,因此你不需要過濾器。

如果要通過相關表進行搜索,則應在SearchModel中創建公共屬性並創建驗證規則。正如我從你的例子看到的,你想過濾來自相關表的數據;

這裏是一個小例子:

設置搜索模式

public $emp_first_name; 

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

public function search($params) 
{ 
    $query = Person::find()->joinWith(['empHistory'])->groupBy(Person::tableName().'.id'); 

    $dataProvider = new ActiveDataProvider([ 
     'query' => $query, 
    ]); 

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

    $query->andFilterWhere(['like', EmpInfo::tableName().'.emp_first_name', $this->emp_first_name]); 

    return $dataProvider; 
} 

在View:

<?= GridView::widget([ 
    'dataProvider' => $dataProvider, 
    'filterModel' => $searchModel, 
    'columns' => [ 
     ['class' => 'yii\grid\SerialColumn'], 
     'emp_history_id', 
     [ 
      'attribute' => 'emp_first_name', 
      'value' => 'empHistory.emp_first_name', 
     ], 
     ['class' => 'yii\grid\ActionColumn'], 
    ] 
]); ?> 
+0

非常感謝...我會試試這個...... – Paventhan