yii框架数据分页

控制器部分
<?php
use yii\data\Pagination;
public function actionList()
{
	$test=new TestForm();	//实例化model模型
	$arr=$test->find();
	//$countQuery = clone $arr;
	$pages = new Pagination([
		//'totalCount' => $countQuery->count(),
		'totalCount' => $arr->count(),
		'pageSize'   => 2   //每页显示条数
	]);
	$models = $arr->offset($pages->offset)
		->limit($pages->limit)
		->all();
	return $this->render('list', [
		'models' => $models,
		'pages'  => $pages
	]);
}
?>

view视图部分
<?php
use yii\widgets\LinkPager;
foreach($models as $k=>$v){
	echo "<tr>";
	echo "<td>".$v['u_id']."</td>";
	echo "<td>".$v['u_name']."</td>";
	echo "<td>".$v['u_state']."</td>";
	echo "</tr>";
}
echo LinkPager::widget([
	'pagination' => $pages,
]);
?>


猜你喜欢

转载自blog.csdn.net/panjiapengfly/article/details/54848897