文章主要介绍了Yii2 数据分页显示,希望对大家的学习有所帮助

controller文件:

use yii\data\Pagination;
		$query	= Exchange::find()->orderBy('id desc');
		$count	= $query->count();
		$pagination = new Pagination(['totalCount' => $count]);
		$pagination->pageSize = 18;
		$exchanges	= $query->offset($pagination->offset)
					->limit($pagination->limit)
					->all();
		return $this->render('list', [
					'exchanges'		=> $exchanges,
					'pagination'	=> $pagination,
					]);
 

view文件:

<?php
// 中文

use yii\helpers\Html;
use yii\helpers\Url;
use yii\widgets\ActiveForm;
use yii\widgets\LinkPager;

/* @var $this yii\web\View */
/* @var $design app\models\Article */
/* @var $form ActiveForm */
?>
<link href="css/pager.css" rel="stylesheet">
<div class="article">
	<?php
		foreach($designs as $d){
			echo $d->title . ' ' . $d->build_time . '<br />';
		}
		echo LinkPager::widget(['pagination' => $pagination]);
	?>
</div><!-- article -->

pager.css样式文件:

.pagination {
    border-radius: 4px;
    display: inline-block;
    margin: 20px 0;
    padding-left: 0;
}
.pagination > li {
    display: inline;
}
.pagination > li > a, .pagination > li > span {
    background-color: #fff;
    border: 1px solid #ddd;
    color: #337ab7;
    float: left;
    line-height: 1.42857;
    margin-left: -1px;
    padding: 6px 12px;
    position: relative;
    text-decoration: none;
}
.pagination > li:first-child > a, .pagination > li:first-child > span {
    border-bottom-left-radius: 4px;
    border-top-left-radius: 4px;
    margin-left: 0;
}
.pagination > li:last-child > a, .pagination > li:last-child > span {
    border-bottom-right-radius: 4px;
    border-top-right-radius: 4px;
}
.pagination > li > a:hover, .pagination > li > span:hover, .pagination > li > a:focus, .pagination > li > span:focus {
    background-color: #eee;
    border-color: #ddd;
    color: #23527c;
    z-index: 3;
}
.pagination > .active > a, .pagination > .active > span, .pagination > .active > a:hover, .pagination > .active > span:hover, .pagination > .active > a:focus, .pagination > .active > span:focus {
    background-color: #337ab7;
    border-color: #337ab7;
    color: #fff;
    cursor: default;
    z-index: 2;
}
.pagination > .disabled > span, .pagination > .disabled > span:hover, .pagination > .disabled > span:focus, .pagination > .disabled > a, .pagination > .disabled > a:hover, .pagination > .disabled > a:focus {
    background-color: #fff;
    border-color: #ddd;
    color: #777;
    cursor: not-allowed;
}
/*.pagination-lg > li > a, .pagination-lg > li > span {
    font-size: 18px;
    line-height: 1.33333;
    padding: 10px 16px;
}
.pagination-lg > li:first-child > a, .pagination-lg > li:first-child > span {
    border-bottom-left-radius: 6px;
    border-top-left-radius: 6px;
}
.pagination-lg > li:last-child > a, .pagination-lg > li:last-child > span {
    border-bottom-right-radius: 6px;
    border-top-right-radius: 6px;
}
.pagination-sm > li > a, .pagination-sm > li > span {
    font-size: 12px;
    line-height: 1.5;
    padding: 5px 10px;
}
.pagination-sm > li:first-child > a, .pagination-sm > li:first-child > span {
    border-bottom-left-radius: 3px;
    border-top-left-radius: 3px;
}
.pagination-sm > li:last-child > a, .pagination-sm > li:last-child > span {
    border-bottom-right-radius: 3px;
    border-top-right-radius: 3px;
}*/
.pager {
    list-style: outside none none;
    margin: 20px 0;
    padding-left: 0;
    text-align: center;
}
.pager li {
    display: inline;
}
.pager li > a, .pager li > span {
    background-color: #fff;
    border: 1px solid #ddd;
    border-radius: 15px;
    display: inline-block;
    padding: 5px 14px;
    text-decoration: none;
}
.pager li > a:hover, .pager li > a:focus {
    background-color: #eee;
    text-decoration: none;
}
.pager .next > a, .pager .next > span {
    float: right;
}
.pager .previous > a, .pager .previous > span {
    float: left;
}
.pager .disabled > a, .pager .disabled > a:hover, .pager .disabled > a:focus, .pager .disabled > span {
    background-color: #fff;
    color: #777;
    cursor: not-allowed;
}
.pager > .active > a, .pager > .active > span, .pager > .active > a:hover, .pager > .active > span:hover, .pager > .active > a:focus, .pager > .active > span:focus {
    background-color: #eee;
    cursor: default;
    z-index: 2;
}