后端代码
public function actionList()
{
//接收查询的关键字
$keywords = Yii::$app->request->get('keywords',"");
//获取页码,参数需和Pagination中配置的参数一致
$page=Yii::$app->request->get('page');
$page=($page-1)>0?($page-1):0;
$pageSize=5;
$goods = Goods::find()
->where(['status'=>1])
->offset($page*$pageSize)
->limit($pageSize)
->orderBy('id');
//判断查询是否为空
if (!empty($keywords)) {
$goods->where("name like '%{$keywords}%' OR goods_no like '%{$keywords}%'");
}
//设置分页配置
$config=[
'totalCount' => $goods->count(),
'defaultPageSize' => $pageSize,
];
//实例化分页器,将配置参数传入到Pagination中
$pages = new Pagination($config);
$categoryModel = GoodsCategory::find()->asArray()->all();
$brands = Brand::find()->asArray()->all();
$goods = $goods->all();
return $this->render('list',[
'category'=>$categoryModel,
'brands' =>$brands,
'goods' => $goods,
'pages' => $pages
]);
}
前端代码
<tr>
<td align="right" nowrap="true" colspan="6">
<div id="turn-page">
<?=\yii\widgets\LinkPager::widget([
'pagination' => $pages,
'nextPageLabel' => '下一页',
'prevPageLabel' => '上一页',
'firstPageLabel' => '首页',
'lastPageLabel' => '尾页',
'maxButtonCount' => 5,
'hideOnSinglePage' => false,
])?>
</div>
</td>
</tr>