0
votes

I have bootstrap modal with gridview in it. Pagination for dataProvider is set to 5. The problem is that I have only 1 row on the first page of my gridview, other pages seems to work good. Here's gridview code

 <?php Pjax::begin(); ?>
<?= GridView::widget([
                    'dataProvider' => $operations,
                    'filterModel'=>$fModel,
                    'columns' => [
                        'operation_code',
                        'amount',
                        [
                            'attribute'=>'type',
                            'content'=>function($model){
                              return ClientMoneyOperation::itemAlias('type_color',$model->type);
                            },
                            'filter'=>ClientMoneyOperation::itemAlias('type'),
                        ],
                        [
                            'header'=>'Бонус',
                            'content'=>function($model){
                                return $model->bonus->amount;
                            }
                        ],
                        [
                            'header'=>'Директорський бонус',
                            'content'=>function($model){
                                return $model->directorbonus;
                            }
                        ],
                        'comment',
                        [
                            'attribute'=>'created_at',
                            'format'=>'datetime',
                            'filter'=>false],

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

and search function

 public function 
 search($params,$city_id=null,$client_id=null,$page_size=20,$group = 
 false,$code=null,$date_from = null){
    if($city_id){
        $query = ClientMoneyOperation::find()->joinWith(['user.place'])-
>where(['place.city_id'=>$city_id]);
    } else {
        $query = ClientMoneyOperation::find();
}

$query->joinWith(['client'])->joinWith(['bonus']);
if($group){
        $query->groupBy('operation_code');
}
    $query->orderBy('created_at DESC');
if($code){
    $query->where(['operation_code'=>$code]);
}
    $dataProvider = new ActiveDataProvider([
        'query'=>$query,
        'sort'=>['attributes'=>['created_at'=>SORT_DESC]],
        'pagination'=>[
            'pageSize'=>$page_size
        ],
    ]);

    $this->load($params);

    if(!$this->validate()){
        return $dataProvider;
    }
    $query->andFilterWhere([
        'client_money_operation.type'=>$this->type,
        'client_money_operation.status'=>$this->status,
    ]);
    if($client_id){
        $query->andFilterWhere([
        'client_money_operation.client_id'=>$client_id,
    ]);
    }

    $query->andFilterWhere(['like','client_money_operation.comment',$this->comment])
          ->andFilterWhere(['like','client.card_code',$this->card_code])
          ->andFilterWhere(['like','client_bonus_operation.amount',$this->bonus_amount])
          ->andFilterWhere(['like','client_money_operation.operation_code',$this->operation_code]);
    if(!empty($this->created_at_range) && strpos($this->created_at_range, '-') !== false){
        list($start_date, $end_date) = explode(' - ', $this->created_at_range);
        $query->andFilterWhere(['between', 'client_money_operation.created_at', strtotime(date("Y-m-d",strtotime($start_date))), strtotime(date("Y-m-d",strtotime($end_date)+86400))]);
    } else {
        $query->andFilterWhere(['between', 'client_money_operation.created_at', $date_from != null ? 1 : strtotime(date("Y-m-d",time())), strtotime(date("Y-m-d",time() + 86400))]);
    }
    return $dataProvider;
}

other gridviews works ok with the same dataprovider. PageSize 5 is setted in controller, where I am calling function search()

1
Most common reason for this is the query and in particular joined part of it that returns more than 1 row for each parent row (so one-to-many relation).Bizley
@Bizley, i tried to delete columns one-by one in gridview, but it didn't help, or maybe i missunderstood you?Игорь Быстревский
Database query results in duplicated rows that are removed by the GridView when displaying that is why you've got different number of results per page. You need to adjust the query.Bizley

1 Answers

0
votes

Try with this two lines before the return:

$countQuery = clone ($query);
$dataProvider->totalCount = $countQuery->count();
return...