【发布时间】:2015-10-16 17:54:30
【问题描述】:
Yii2 分页有问题。我使用 ListView 小部件来显示数据。我也在我的模型中使用 SetSort 对数据进行排序。 我的搜索模型代码:
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;
}
我知道将 DESC 和 ASC 逻辑划分为价格不是一个很好的决定,但这是当前设计所需要的。我的控制器代码:
public function actionIndex()
{
$searchModel = new ItemsSearch();
$dataProvider = $searchModel->search(Yii::$app->request->getQueryParams());
return $this->render('index', [
'dataProvider'=>$dataProvider,
'searchModel'=>$searchModel,
]);
}
正如我所说,在视图中我使用 ListView。这是我的视图代码:
<? 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();?>
如您所见,它在第一页上没有最后一个元素,在下一页上一切正常。我认为这是与偏移量限制有关的东西。谁能告诉我,哪里出错了?谢谢!
【问题讨论】:
-
检查生成的查询以获取调试模块中的项目,并尝试直接在 db 中执行它以确保它返回所需的记录数
-
@Tony 感谢您的回复,但查询的转储仅显示“SELECT
items.* FROMitemsLEFT JOINpricesONitems.id=prices。item_id"
标签: php listview model-view-controller pagination yii2