Yii分页用法实例详解_PHP

WBOY
Release: 2016-05-31 18:15:49
Original
840 people have browsed it

下面我总结了在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' => '',
        'prevPageLabel' => ' »',
   'prevPageLabel' => '« 上一页'
  ),
//在这里还可以配置一些排序规则,具体可以查阅手册
));
?>
这样就实现了Ajax分页,很方便。

希望本文所述对大家基于Yii框架的PHP程序设计有所帮助。

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!