**分 页**
在写分页之前,你需要在控制器引入use yii\data\Pagination 对象为其填充数据,然后在进行下面的操作。
public function actionShow(){
$data=Mark::find();
$countQuery = clone $data;
$pages = new Pagination(['totalCount' => $countQuery->count(),'pageSize'=>'5']);
$models = $data->offset($pages->offset)
->limit($pages->limit)
->all();
return $this->render('sort', [
'data' => $models,
'pages' => $pages,
]);
}
上面就是控制器的代码,接下来就该写视图层了。
同样在写视图层时要引入 use yii\widgets\LinkPager;
<?php
use yii\widgets\LinkPager;
?>
<div>
你要显示的内容。
</div>
<?php
echo LinkPager::widget([
'pagination' => $pages,
]);?>
以上就是yii2的分页了。
ajax分页
同样先引入 use yii\web\View; 然后在加以下代码
<script>
<?php $this->beginBlock('abc'); ?>
$(document).on('click','.pagination a',function(e){
e.preventDefault();
var url=$(this).attr('href');
$.get(url,function(msg){
$('#lists').html(msg);
});
});
<?php $this->endBlock(); $this->registerJs($this->blocks['abc'],View::POS_END); ?>
</script>
这样就可以了,当然别忘了引入jQuery文件,也可以引用线上的。
链接: https://blog.csdn.net/jeason_1
yii2 分页 和 ajax分页
最新推荐文章于 2021-12-22 16:52:40 发布