一聚教程网:一个值得你收藏的教程网站

热门教程

Yii框架实现数据分页及排序的例子

时间:2022-06-25 00:52:56 编辑:袖梨 来源:一聚教程网

在控制器:

 代码如下 复制代码

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,
        ));
    }
}

在视图文件中:

 代码如下 复制代码

link('id')?>


link('title')?>



    foreach($models as $model):?>
       

  1.        

    id?> - title?>


       



 
widget('CLinkPager', array(
    'pages' => $pages,
))?>

一. 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层]


 $row["sumrate"]));?>
 " ?>
 
   //分页widget代码:
 $this->widget('CLinkPager',array('pages'=>$pages));
 ?>


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

二. widget实现分页

[model层]

 代码如下 复制代码

/**
     * @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层]

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


[View层]

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

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

热门栏目