Home  >  Article  >  php教程  >  Yii分页用法实例详解

Yii分页用法实例详解

WBOY
WBOYOriginal
2016-06-06 20:16:081104browse

这篇文章主要介绍了Yii分页用法,以实例形式详细分析了比较常见的几种分页方法及其应用特点,非常具有实用价值,需要的朋友可以参考下

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

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

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:

复制代码 代码如下:

$this->widget('zii.widgets.CListView', array(
    'dataProvider' => $dataProvider, //数据
    'itemView' => '_view', //显示的模版
    'id' => Yii::app()->controller->id,
    'itemsTagName' => 'ul',
    'ajaxVar' => '', //默认为page或ajax 去掉后url更简洁
    'htmlOptions' => array('class' => Yii::app()->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' => '


    暂无数据!

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

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

Controller:

复制代码 代码如下:

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn