2015-10-19 79 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'值仍然是空的.... –