首页 > php框架 > Yii分页实例使用详解

Yii分页实例使用详解

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

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

[Controller]

<?php
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]

<?php
$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' => ' 
<DIV class="alert alert-waning"> 
    暂无数据! 
</DIV> 
', //无数据时显示内容
    'pagerCssClass' => 'pagination', //分页的class
    'pager' => array(
        'selectedPageCssClass' => 'active', //当前页的class
        'hiddenPageCssClass' => 'disabled', //禁用页的class
        'header' => '', //分页前显示的内容
        'maxButtonCount' => 10, //显示分页数量
        'htmlOptions' => array(
            'class' => ''
        ) ,
        'firstPageLabel' => '首页',
        'nextPageLabel' => '下一页',
        'prevPageLabel' => '上一页',
        'lastPageLabel' => '末页',
    ) ,
));
?>

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


[Controller]

<?php
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层]

<?php
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层]

<? phpforeach ($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层]

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

[Controller层]

<?php
/**
 * 运单统计功能:
 * 按日期统计
 */
public function actionReport() {
    $model = new Delivery('statistics');
    $model->unsetAttributes(); // clear any default values
    $this->render('report', array(
        'model' => $model,
    ));
}
?>

[View层]

<?php
$this->widget('zii.widgets.grid.CGridView', array(
    'id' => 'delivery-grid',
    'dataProvider' => $model->statistics() ,
    'filter' => $model,
    'columns' => array(
        'remitdate',
        'dayrate',
        array(
            'class' => 'CButtonColumn',
        ) ,
    ) ,
));
?>

优点: 可以使用自带的样式; 缺点: 效率略低.

五,Ajax分页

YII中ajax分页有多种实现方法,比较传统的就是在view中写JS来实现,大概的就是这样:

在view中js大概逻辑是这样

$('#listview .yiiPager a').live('click',function(){
        $.ajax({
url:$(this).attr('href'),
success:function(html){
$('#listview').html(html);
}
});
return false;//阻止a标签
});

然后在controller中判断ajax请求,再使用renderPartial方法渲染局部List视图,然后局部视图会被view中的ajax

方法填充到局部刷新的div中。controller的大概逻辑是

<?php
if (Yii::app()->request->isAjaxRequest) {
    $this->renderPartial('_comments', array(
        'model' => $model,
        'comments' => $comments, //在局部视图中foreach得到每条数据
        'pages' => $pages,
    ));
    Yii::app()->end();
} < ? php后来发现YII中的CListview更方便,封装了分页,foreach显示list,还支持数据排序。具体的可以在YII的API手册中发掘。使用CListview是默认ajax分页的,使用方法如下:controller中: < ? php $criteria = new CDbCriteria();
$criteria->order = '`create_time` DESC';
$dataProvider = new CActiveDataProvider('Comments', array(
    'pagination' => array(
        'pageSize' => Yii::app()->params['commentsPerPage'], //设置分页条数以确定取出数据的条数
        
    ) ,
    'criteria' => $criteria,
));
$this->render('comments', array(
    'model' => $model,
    'dataProvider' => $dataProvider,
));
?>

然后在view中:

<?php
$this->widget('zii.widgets.CListView', array(
    'dataProvider' => $dataProvider,
    'itemView' => '_comments',
    //'ajaxUpdate'=> false,//这样就不会AJAX翻页
    'pager' => array( //pager 的配置信息。默认为<CODE>array('class'=>'CLinkPager')</CODE>.也可以自己配置
        'nextPageLabel' => '下一页 &raquo;',
        'prevPageLabel' => '&laquo; 上一页'
    ) ,
    //在这里还可以配置一些排序规则,具体可以查阅手册
    
));
?>

这样就实现了Ajax分页,很方便。


教程地址:http://www.phprm.com/frame/54055.html

欢迎转载!但请带上文章地址^^

标签:foreach select 分页 request group by ajax分页

相关文章

发表留言