2015-10-19 73 views
2

我在yii2高級應用一個gridview,我想用post方法爲網格,這是沒有問題的搜索數據,我可以做到這一點使用下面的代碼使用POST方法的GridView pjax搜索在yii2

<?php Pjax::begin(['id' => 'grid', 'timeout' => false ,'clientOptions' => ['method' => 'POST'] ]); ?> 

<?php Pjax::end(); ?> 

但現在的問題是我該如何獲得數據發送使用postSearch模型的表。

現在我所做的就是檢查$ _ POST並分配到代碼$params,但我想知道有沒有更好的辦法做到這一點比這下面的代碼

public function search($params) 
{ 
    if(isset ($_POST)){ 
     $params = $_POST; 
    } 

    $query = Event::find()-> where(['organiser_id'=>Yii::$app->user->identity->id]); 

    $this->load($params); 

    $query->joinWith(['interest']); 

    if (!$this->validate()) { 

     return $dataProvider; 
    } 

    $query->andFilterWhere([ 
     'id' => $this->id, 
     'organiser_id' => $this->organiser_id, 
     'is_active' => $this->is_active, 
    ]); 

    $query->andFilterWhere(['like', 'title', $this->title]) 

    return $dataProvider; 
} 

非常感謝你多

回答

1
public function search($params) 
{ 
    if(isset ($_POST['SearchModel'])){ 
     $this->load = \Yii::$app->request->post(); 
    } 

    //searching code here 
} 

在你的索引操作,其中搜索實現使用如:

public function actionIndex() 
    { 
     $searchModel = new SearchModel(); //Your Search Model Class 
     $post = Yii::$app->request->post(); 
     $dataProvider = $searchModel->search($post); 

     return $this->render('index', [ 
      'searchModel' => $searchModel, 
      'dataProvider' => $dataProvider, 
     ]); 
    } 

In Search Model Class ::

public function search($params) 
    { 


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

     $this->load($params); 
} 
+0

我不明白這將如何工作,但我會盡力回覆你,因爲即使在添加代碼後,'$ params'值仍然是空的.... –