Reputation: 984
I have a problem with Yii2 pagination. I use ListView widget to show data. Also I am using SetSort in my model to sort data. My search model code:
public function search($params)
{
$query = Items::find();
$dataProvider = new ActiveDataProvider([
'query' => $query,
]);
$dataProvider->setSort([
'defaultOrder' => ['position' => SORT_ASC],
'attributes' => [
'position' => [
'asc' => ['items.is_top'=>SORT_ASC,'items.position' => SORT_DESC],
'label' => 'By popularity',
],
'pricerup' => [
'asc' => ['prices.price' => SORT_ASC],
'label' => 'Exprensive to cheap'
],
'pricerdown' => [
'asc' => ['prices.price' => SORT_DESC],
'label' => 'Cheap to expensive'
],
]
]);
if (!($this->load($params) && $this->validate())) {
$query->joinWith(['prices']);
return $dataProvider;
}
$query->andFilterWhere([
'discount' => $this->discount,
'present_id' => $this->present_id,
'is_top' => $this->is_top,
'manufacturer_id' => $this->manufacturer_id,
]);
$query->andFilterWhere(['like', 'name', $this->name])
->andFilterWhere(['like', 'description', $this->description])
->andFilterWhere(['like', 'taste', $this->taste])
->andFilterWhere(['like', 'country', $this->country])
->andFilterWhere(['like', 'slug', $this->slug])
->andFilterWhere(['like', 'short_desc', $this->short_desc])
->andFilterWhere(['like', 'thumbnail', $this->thumbnail]);
return $dataProvider;
}
I know that this is not a very good decision to divide DESC and ASC logic for price, but this is needed for current design. My controller code:
public function actionIndex()
{
$searchModel = new ItemsSearch();
$dataProvider = $searchModel->search(Yii::$app->request->getQueryParams());
return $this->render('index', [
'dataProvider'=>$dataProvider,
'searchModel'=>$searchModel,
]);
}
As I said, in view I use ListView. This is my view code:
<? Pjax::begin(['id' => 'items', 'clientOptions' => ['method' => 'POST']]);?>
<?
$dataProvider->pagination = [
'defaultPageSize' => 8,
];
echo ListView::widget( [
'id'=>'items',
'pager' => [
'firstPageLabel' => "",
'disabledPageCssClass'=>'swiper-button-disabled',
'lastPageLabel' => "",
'nextPageLabel' => ">",
'prevPageLabel' => "<",
'maxButtonCount'=>0,
'nextPageCssClass'=>'total-button-next', 'prevPageCssClass'=>'total-button-prev',
'options'=>['id'=>'poplinks','class'=>'col-md-12 total-slider-orders margin-right-null padding-left-right-yes total-down-arrow']
],
'dataProvider' => $dataProvider,
'itemView' => '_item',
'summary'=>'',
] );
Pjax::end();?>
As a result I get something like this:
As you can see, it doesn't have last element on the first page, on the next pages everything is ok. I think that this is something with the offset,limit. Can someone tell me, where is an error? Thanks!
Upvotes: 1
Views: 1207
Reputation: 984
Because of many relation, AR show item with two prices as two items, so it show 7 items on page instead of 8. Resolved it just by adding to search model one line:
$query->groupBy('items.id');
Upvotes: 2