【发布时间】:2018-03-16 14:36:17
【问题描述】:
我有带有 gridview 的引导模式。 dataProvider 的分页设置为 5。问题是我的 gridview 的第一页上只有 1 行,其他页面似乎运行良好。 这是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;
}
其他网格视图可以与相同的数据提供程序一起使用。 PageSize 5 设置在控制器中,我在这里调用函数search()
【问题讨论】:
-
最常见的原因是查询,特别是它的连接部分,它为每个父行返回多于 1 行(因此是一对多关系)。
-
@Bizley,我尝试在 gridview 中逐列删除,但没有帮助,或者我误解了你的意思?
-
数据库查询导致显示时被 GridView 删除的重复行,这就是为什么每页有不同数量的结果。您需要调整查询。
标签: php gridview yii2 yii2-advanced-app