3

im using Pjax gridview,After I search the results,when I click pagination ,the results changed to default results page not search result. and my search function is like

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;
}

Edit#1: my grid view file is :

<?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();?>
Jackhad
  • 331
  • 1
  • 6
  • 16

2 Answers2

4

Change your POST to GET, this should make the pagination work along with any of your other filters.

Subbu
  • 95
  • 4
0

I have solved this problem by "self join".

  1. In your model file write this code for self join.

    
    public function getUserLogs()
    {
        return $this->hasOne(UserLogs::className(), ['user_id' => 'user_id', 'user_log_id' => 'user_log_id']);
    }
    
  2. In your search function write this code

    
    
    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;
    }