Yii分页用法实例详解(2)

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));
?>

优点: DAO效率高; 缺点: view层需要自己写一些样式,稍显麻烦一点

第四种:widget实现分页

model层:

复制代码 代码如下:

/**
* @var string attribute : 日运费 (统计用)
* 需要对新增加的字段做个声明
*/
public $dayrate;

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:http://www.heiqu.com/92c3d2772c4c40ee72625bd134b02872.html