过滤结果分页在yii2 gridview中不起作用

时间:2016-06-09 08:34:31

标签: gridview pagination yii2 pjax yii2-basic-app

我正在使用Pjax gridview,在我搜索结果后,当我点击分页时,结果更改为默认结果页面而不是搜索结果。 我的搜索功能就像

public function search($params)
{
    $query = UserLogs::find();
    $dataProvider = new ActiveDataProvider([
        'query' => $query,'pagination'=> ['defaultPageSize' => PAGE_SIZE],
        'sort' => [
            'defaultOrder' => [
                'user_log_id' => SORT_DESC,
            ]
        ],
    ]);

    $this->load($params);

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

    $query->joinWith('user');

    // grid filtering conditions
    $query->andFilterWhere([
        'user_log_id' => $this->user_log_id,
        //'user_id' => $this->user_id,
        'user_logs.user_type_id' => $this->user_type_id,
        'login_time' => $this->login_time,
        'logout_time' => $this->logout_time,
    ]);

    $query->andFilterWhere(['like', 'login_ip', $this->login_ip])
          ->andFilterWhere(['like', 'juser.firstname', $this->user_id]);             

    return $dataProvider;
}

编辑#1:我的网格视图文件是:

<?php Pjax::begin(['clientOptions' => ['method' => 'POST']]);?>
                <?= GridView::widget([
                    'dataProvider' => $dataProvider,
                    'filterModel' => $searchModel,
                    'columns' => [
                        ['class' => 'yii\grid\SerialColumn'],

                       .....                                         

                        ['class' => 'yii\grid\ActionColumn','template' => '{view}'],
                    ],
                ]); ?>
                <?php Pjax::end();?>

2 个答案:

答案 0 :(得分:2)

将您的POST更改为GET,这应该使分页与您的任何其他过滤器一起使用。

答案 1 :(得分:0)

我已经通过“自我加入”解决了这个问题。

  1. 在模型文件中编写以下代码进行自我连接。

    
    public function getUserLogs()
    {
        return $this->hasOne(UserLogs::className(), ['user_id' => 'user_id', 'user_log_id' => 'user_log_id']);
    }
    
  2. 在搜索功能中编写以下代码

    
    
    public function search($params)
    {
        $query = UserLogs::find();
        $query->joinWith('user_logs as u_l');
        $dataProvider = new ActiveDataProvider([
            'query' => $query,'pagination'=> ['defaultPageSize' => PAGE_SIZE],
            'sort' => [
                'defaultOrder' => [
                    'user_log_id' => SORT_DESC,
                ]
            ],
        ]);
    
        $this->load($params);
    
        if (!$this->validate()) {
            return $dataProvider;
        }
    
        $query->joinWith('user');
    
        // grid filtering conditions
        $query->andFilterWhere([
            'u_l.user_log_id' => $this->user_log_id,
            'u_l.user_id' => $this->user_id,
            'u_l.user_type_id' => $this->user_type_id,
            'u_l.login_time' => $this->login_time,
            'u_l.logout_time' => $this->logout_time,
        ]);
    
        $query->andFilterWhere(['like', 'u_l.login_ip', $this->login_ip])
              ->andFilterWhere(['like', 'user.firstname', $this->user_id]);             
    
        return $dataProvider;
    }