本文总结了在Yii常用的一些yii分页方式与实例代码,有普通分页与ajax实现分页,希望此文章对大家会有所帮助。
第一种:CLinkPager 针对数组形式的数据分页
Controller页面:
public function actionIndex() {
$criteria = new CDbCriteria();
$criteria->order = 'news_id DESC';
$criteria->condition = 'user_id = 1';
$count = News::model()->count($criteria);
$pages = new CPagination($count);
$pages->pageSize = 10;
$pages->applyLimit($criteria);
$list = News::model()->findAll($criteria);
$this->render('index', array('list' => $list, 'pages' => $pages));
}
View页面:
<UL>
<?php foreach ($list as $item): ?>
<LI>
<DIV class=page-header>
<?php echo $item--->news_title; ?>
</DIV>
<DIV class=content>
<?php echo $item--->news_intro; ?>
</DIV>
</LI>
<?php endforeach; ?>
</UL>
<DIV class=pagination>
<?php
$this--->widget('CLinkPager', array(
'pages' => $pages,
'selectedPageCssClass' => 'active', //当前页的class
'hiddenPageCssClass' => 'disabled', //禁用页的class
'header' => '', //分页前显示的内容
'maxButtonCount' => 10, //显示分页数量
'htmlOptions' => array('class' => ''),
'firstPageLabel' => '首页',
'nextPageLabel' => '下一页',
'prevPageLabel' => '上一页',
'lastPageLabel' => '末页',
)
);
?>
</DIV>
第二种: DAO实现分页
Controller页:
public function actionReport()
{
$sql = "select remitdate, sum(rate) sumrate from td_delivery
group by remitdate
order by remitdate desc";
$criteria=new CDbCriteria();
$result = Yii::app()->db->createCommand($sql)->query();
$pages=new CPagination($result->rowCount);
$pages->pageSize=2;
$pages->applyLimit($criteria);
$result=Yii::app()->db->createCommand($sql." LIMIT :offset,:limit");
$result->bindValue(':offset', $pages->currentPage*$pages->pageSize);
$result->bindValue(':limit', $pages->pageSize);
$posts=$result->query();
$this->render('report',array(
'posts'=>$posts,
'pages'=>$pages,
));
}
View页:
<?php foreach($posts as $row):?>
<?php echo CHtml::link($row["remitdate"],array('delivery/view','remitdate'=>$row["sumrate"]));?>
<?php echo $row["sumrate"]."<br />" ?>
<?php endforeach;?>
<?php
//分页widget代码:
$this->widget('CLinkPager',array('pages'=>$pages));
?>
声明:如需转载,请注明来源于www.webym.net并保留原文链接:http://www.webym.net/jiaocheng/951.html





















