Yii分页用法实例详解
前端之家收集整理的这篇文章主要介绍了
Yii分页用法实例详解,
前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
下面我总结了在Yii常用的一些yii分页方式与实例代码,这里有普通分页与ajax实现分页,希望此文章对大家会有所帮助。
第一种:CListView分页 针对对象形式的数据分页
Controller:
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:
<div class="codetitle">
<a style="CURSOR: pointer" data="16927" class="copybut" id="copybut16927" onclick="doCopy('code16927')"> 代码如下: <div class="codebody" id="code16927"><?php
$this->widget('zii.widgets.CListView',//数据
'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">
暂无数据!
',//无数据时
显示内容
'pagerCssClass' => 'pagination',//
分页的class
'pager' => array(
'selectedPageCssClass' => 'active',//当前页的class
'hiddenPageCssClass' => 'disabled',//禁用页的class
'header' => '',//
分页前
显示的
内容
'maxButtonCount' => 10,//
显示分页数量
'htmlOptions' => array('class' => ''),
'firstPageLabel' => '
首页',
'nextPageLabel' => '
下一页',
'prevPageLabel' => '
上一页',
'lastPageLabel' => '末页',
));
?>
$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));
}
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,
));
}
$row["sumrate"]));?>
PHP echo $row["sumrate"]."
" ?>
widget('CLinkPager',array('pages'=>$pages));
?>
统计用)
* 需要对新
增加的字段做个声明
*/
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',
),
));
}
unsetAttributes(); // clear any default values
$this->render('report',array(
'model'=>$model,
));
}