Yii实现数据分页

因公司在使用yii框架进程网站开发,前段时间在开发中遇到数据分页问题,在此整理了下yii两种分页方式的实现代码。

第一种:CListView分页(针对对象形式的数据分页)
【controler】代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
public function actionIndex() {
$criteria = new CDbCriteria();
$criteria->order = ‘rid ASC';
$dataProvider = new CActiveDataProvider(‘RobotModel’,array(
‘pagination’ => array(
‘pageSize’ => Yii::app()->params[‘pagesize’],
‘pageVar’ => Yii::app()->params[‘pagevar’],
),
‘criteria’ => $criteria,
));

$this->render(‘index’,array(
‘dataProvider’ => $dataProvider,
));
}

【view】代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
<?php
$this->widget(‘zii.widgets.CListView’,array(
‘dataProvider’ => $dataProvider, //数据
‘itemView’ => ‘_view’,
‘id’ => Yii::app()->controler->id,
‘itemsTagName’ => ‘ul’,
‘ajaxVar’ => ”, //默认为page或ajax 去掉此项后url更简洁
‘htmlOptions’ => array(‘class’ => Yii::app()->controller->id),
‘loadingCssClass’ => ‘loading’,
‘template’ => ‘{summary}{sorter}{items}{pager}’, //显示顺序
‘ajaxUpdate’ => false, //是否ajax分页 值为false或分页显示的容器id
‘beforeAjaxUpdate’ => ‘before_ajax_update’, //回调函数,在common.js里完成
‘afterAjaxUpdate’ => ‘after_ajax_update’,
’emptyText’ => ‘<div class=”alert alert-waning”>暂无数据!</div>’,
pagerCssClass’ => ‘pagination’, //分页的class
‘pager’ => array(
‘header’ => ”,
‘pages’ => $pager,
‘selectedPageCssClass’ => ‘active’, //选中样式
‘hiddenPageCssClass’ => ‘disabled’, //没选中样式
‘maxButtonCount’ => 5, //显示最大页数
‘htmlOptions’ => array(‘class’ => ”, ‘id’ => ”), //html属性
firstPageLabel’ => ‘<<‘, //首页
prevPageLabel’ => ‘<‘, //上一页
nextPageLabel’ => ‘>’, //下一页
lastPageLabel’ => ‘>>’, //末页
),
));
?>

第2种:CLinkPager分页 针对数组形式的数据分页
【controler】代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
public function actionIndex(){
$criteria = new CDbCriteria();
$criteria->order = ‘rid ASC';

//获取总记录数
$count = RobotModel::model()->count($criteria);
$pager = new CPagination($count);
$pager->pageSize = 10; //设置每页数据条数
$pager->applyLimit($criteria); //执行分页

// 获得总页数
$pages = ceil($count/$pager->pageSize);
$data = (array)RobotModel::model()->findAll($criteria);

$this->render(‘index’,array(
‘data’ => $data,
‘pager’ => $pager,
‘count’ => $count,
‘currentPage’ => $pager->currentPage+1,
‘pages’ => $pages,
));
}

【view】代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
<div>
<table>
<?php foreach ($data as $items):?>
<tr>
<td><?php echo $items[‘rid’];?></td>
<td><?php echo $items[‘nickname’];?></td>
<td><?php echo $userInfo[$items[‘uid’]];?></td>
<td><?php echo $items[‘largest_access’];?></td>
<td><?php echo $items[‘status’] ? “开启” : “关闭”;?></td>
<td><?php echo $items[‘welcome’];?></td>
<td><?php echo $items[‘created’];?></td>
<td class=”button-column”>
<a class=”btn robot-update” href=”javascript:;”>修改</a>
<a class=”btn robot-save” href=”javascript:;” style=”display:none;”>保存</a>
<a class=”btn robot-del” href=”javascript:;”>删除</a>
</td>
</tr>
<?php endforeach;?>
</table>

<div class=”pagination”>
<div id=”yw3class=”pagination”>
<?php
$this->widget(‘CLinkPager’,array(
‘header’ => ”,
‘pages’ => $pager,
‘selectedPageCssClass’ => ‘active’,
‘hiddenPageCssClass’ => ‘disabled’,
‘maxButtonCount’ => 5,
‘htmlOptions’ => array(‘class’ => ”, ‘id’ => ”),
firstPageLabel’ => ‘<<‘,
prevPageLabel’ => ‘<‘,
nextPageLabel’ => ‘>’,
lastPageLabel’ => ‘>>’,
));
?>
</div>
</div>
</div>