新葡亰496net 新葡亰编程 Yii分页用法实例详解_php实例_脚本之家

Yii分页用法实例详解_php实例_脚本之家

Yii实现分页的两种方法,一种是用DAO实现,另外一种是在widget实现.

在控制器:

下面我总结了在Yii常用的一些yii分页方式与实例代码,这里有普通分页与ajax实现分页,希望此文章对大家会有所帮助。

各有优点吧,第一种效率会高一点, 第二种可以使用自带的表格,方便一些.

 代码如下

第一种:CListView分页 针对对象形式的数据分页

一. DAO实现分页.

class PostController extends Controller
{
    function actionIndex()
    {
        $criteria = new CDbCriteria();
        $count=Post::model()->count($criteria);
        $pages=new CPagination($count);
 
        // elements per page
        $pages->pageSize=5;
        $pages->applyLimit($criteria);
 
        // sorting
        $sort = new CSort(‘Post’);
        $sort->attributes = array(‘id’, ‘title’,);
        $sort->applyOrder($criteria);
 
        $models = Post::model()->findAll($criteria);
 
        $this->render(‘index’, array(
            ‘models’ => $models,
            ‘pages’ => $pages,
            ‘sort’ => $sort,
        ));
    }
}

Controller:复制代码 代码如下:public
function actionAjax() { $criteria = new CDbCriteria();
//$criteria->order = ‘news_id DESC’; $criteria->condition =
‘user_id = 1’; $dataProvider = new CActiveDataProvider(‘News’, array(
‘pagination’ => array( ‘pageSize’ =>
Yii::app()->params[‘pagesize’], ‘pageVar’ =>
Yii::app()->params[‘pagevar’], ), ‘criteria’ => $criteria, ));
$this->render(‘view’, array( ‘dataProvider’ => $dataProvider, ));
}View:复制代码
代码如下:widget(‘zii.widgets.CListView’, array( ‘dataProvider’ =>
$dataProvider, //数据 ‘itemView’ => ‘_view’, //显示的模版 ‘id’ =>
Yii::app()->controller->id, ‘itemsTagName’ => ‘ul’, ‘ajaxVar’
=> ”, //默认为page或ajax 去掉后url更简洁 ‘htmlOptions’ =>
array->controller->id), ‘loadingCssClass’ => ‘loading’,
//默认为list-view-loading //’template’ =>
‘{summary}{sorter}{items}{pager}’,//显示的顺序 //’ajaxUpdate’ =>
false, //是否ajax分页 false或分页显示的容器id //’beforeAjaxUpdate’ =>
‘before_ajax_update’, //回调函数 在common.js里完成 //’afterAjaxUpdate’
=> ‘after_ajax_update’, ’emptyText’ => ‘

[Controller层]

在视图文件中:

暂无数据!

public function actionReport() { $sql = "select remitdate, sum sumrate from td_delivery group by remitdate order by remitdate desc"; $criteria=new CDbCriteria(); $result = Yii::app()->db->createCommand; $pages=new CPagination; $pages->pageSize=2; $pages->applyLimit; $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, )); }

  $row["sumrate"]));?> " ?>  widget('CLinkPager',array; ?>

 代码如下

‘, //无数据时显示内容 ‘pagerCssClass’ => ‘pagination’, //分页的class
‘pager’ => array( ‘selectedPageCssClass’ => ‘active’,
//当前页的class ‘hiddenPageCssClass’ => ‘disabled’, //禁用页的class
‘header’ => ”, //分页前显示的内容 ‘maxButtonCount’ => 10,
//显示分页数量 ‘htmlOptions’ => array, ‘firstPageLabel’ => ‘首页’,
‘nextPageLabel’ => ‘下一页’, ‘prevPageLabel’ => ‘上一页’,
‘lastPageLabel’ => ‘末页’, ), )); ?>

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

<p><?php echo $sort->link(‘id’)?></p>
<p><?php echo $sort->link(‘title’)?></p>
<ol>
<?php foreach($models as
$model):?>
    <li>
        <h2><?php echo $model->id?> – <?php echo
$model->title?></h2>
    </li>
<?php endforeach?>
</ol>
 
<?php $this->widget(‘CLinkPager’, array(
    ‘pages’ => $pages,
))?>

第二种:CLinkPager 针对数组形式的数据分页

二. widget实现分页

一. DAO实现分页.

Controller:复制代码 代码如下:public
function actionIndex() { $criteria = new CDbCriteria();
$criteria->order = ‘news_id DESC’; $criteria->condition =
‘user_id = 1’; $count = News::model; $pages = new CPagination;
$pages->pageSize = 10; $pages->applyLimit; $list = News::model;
$this->render(‘index’, array(‘list’ => $list, ‘pages’ =>
$pages)); }

/** * @var string attribute : 日运费  * 需要对新增加的字段做个声明 */ public $dayrate; /* * 统计功能: 统计每日的运费 */ public function statistics() { $criteria = new CDbCriteria; $criteria->select = 'remitdate, sum AS dayrate'; $criteria->group = 'remitdate'; return new CActiveDataProvider, array( 'criteria'=>$criteria, 'sort'=>array( // 表头设置点击排序的字段 'attributes'=>array( 'remitdate', 'dayrate'=>array( 'asc'=>'dayrate', 'desc'=>'dayrate DESC', ) ), 'defaultOrder'=>'remitdate desc', ), )); }

[Controller层]

View:复制代码 代码如下:

标签:

相关文章

发表评论

电子邮件地址不会被公开。 必填项已用*标注

网站地图xml地图