Yii2 gridView打破了分页

时间:2018-03-16 14:36:18

标签: php gridview yii2 yii2-advanced-app

我有带gridview的bootstrap模态。 dataProvider的分页设置为5.问题是我在gridview的第一页上只有一行,其他页面似乎运行良好。 这是gridview代码

 <?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(); ?>

和搜索功能

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

其他gridviews适用于相同的dataprovider。 PageSize 5在控制器中设置,我在其中调用函数search()

1 个答案:

答案 0 :(得分:0)

在返回之前尝试使用这两行:

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