//某一页的记录列表 public function getPagerList($modelClass, $page = 1, $where = '') { $pageSize = Yii::app()->params['pagesize']; $offset = ($page - 1) * $pageSize; $sql = $this->_listSql($modelClass, $where, '*'); $command = Yii::app()->db->createCommand($sql . " LIMIT :offset,:limit"); $command->bindValue(':offset', $offset); $command->bindValue(':limit', $pageSize); return $command->queryAll(); } private function _listSql($modelClass, $where, $field = 'COUNT(*)') { $sql = "SELECT $field FROM " . CActiveRecord::model($modelClass)->tableName(); if ($where) $sql .= " WHERE {$where}"; return $sql; } //记录总数 public function getCount($modelClass, $where = "") { $sql = $this->_listSql($modelClass, $where); return Yii::app()->db->createCommand($sql)->queryScalar(); } $pages = new CPagination($count); $pages->pageSize = 4; //$pages->setCurrentPage($page-1); $pdata =Yii::app()->db->createCommand($sql." LIMIT :offset,:limit"); $pdata->bindValue(':offset', $pages->getOffset(); $pdata->bindValue(':limit', $pages->getLimit()); $info = $pdata->queryAll(); PostController.php <?php class PostController extends Controller { function actionIndex() { $criteria = new CDbCriteria(); $criteria->compare("log_id",">0"); $count=Ipad3SerialNumber::model()->count($criteria); $pages=new CPagination($count); // elements per page $pages->pageSize=2; $pages->applyLimit($criteria); // sorting $sort = new CSort('Ipad3SerialNumber'); $sort->attributes = array( 'number', 'get_time' ); $sort->defaultOrder = array('get_time'=>true); $sort->applyOrder($criteria); $models = Ipad3SerialNumber::model()->findAll($criteria); $this->render('list', array( 'models' => $models, 'pages' => $pages, 'sort' => $sort, )); } }
view
<?php <p><?php echo $sort->link('id')?></p> <p><?php echo $sort->link('title')?></p> <ol> <?php foreach($models as $model):?> <li> <h2><?php echo $model->id?> - <?php echo $model->title?></h2> </li> <?php endforeach?> </ol> <?php $this->widget('CLinkPager', array( 'pages' => $pages, 'header'=>'' )); $this->widget('CListPager', array( 'pages'=>$pages, 'header'=>'-' )); ?>
点击$sort的link可以动态排序
2系统也有封装的分页 ,排序GRIDView 覆盖public $pager = 'CardPager'属性和renderPager()方法
enableSorting=false全部禁用排序,单列禁用排序sortable =false,enablePagination=false
public function actionAR(){ /****用法一:*****/ $criteria = new CDbCriteria(); $criteria->compare('serviceType',$arr); $dataProvider=new CActiveDataProvider('Post',array( 'criteria' => $criteria, )); /****用法二:*****/ $dataProvider=new CActiveDataProvider('Post', array( /*'criteria'=>array( 'condition'=>'status=1 AND tags like :tags', 'params'=>array(':tags'=>$_GET['tags']), 'order'=>'create_time DESC', 'with'=>array('author'), ),*/ 'pagination'=>array( 'pageSize'=>100, ), 'sort'=>array( 'defaultOrder'=> array('title'=>true), ), )); $this->render('index', array( 'dataProvider' => $dataProvider, )); } public function actionArray(){ //推荐不用,数组大内存溢出 $rawData= array(array( 'name'=>'István Beregszászi', 'duty'=>'core framework development', 'active'=>false, )); //$rawData=Yii::app()->db->createCommand($sql)->queryAll(); $dataProvider = new CArrayDataProvider($rawData, array( 'sort'=>array( 'attributes'=>array('name', 'id', 'active'), 'defaultOrder'=>array('active' => true, 'name' => false), ), 'pagination'=>array( 'pageSize'=>10, ), )); $this->render('index', array( 'dataProvider' => $dataProvider, )); } public function actionSQL(){ $count = Yii::app()->db->createCommand('SELECT COUNT(*) FROM film')->queryScalar(); $sql = 'SELECT film_id,title FROM film'; $dataProvider=new CSqlDataProvider($sql,array( 'keyField'=>'film_id', //sql in select table column default id 'totalItemCount'=>$count, 'sort'=>array( 'attributes'=>array('title'), //sql table columns 'defaultOrder'=>array('title' => true), ), 'pagination'=>array( 'pageSize'=>100, ), )); $this->render('index', array( 'dataProvider' => $dataProvider, )); }
view
<?php foreach($dataProvider->data as $film):?> <?php echo $film->title?> <?php endforeach?> <?php $this->widget('CLinkPager',array( 'pages'=>$dataProvider->pagination))?> 也可以套CGridView(推荐) summaryText,{summary}自定义总计信息 <?php $this->widget('zii.widgets.grid.CGridView', array( 'dataProvider'=>$dataProvider, 'template'=>'{items}{summary}{pager}', 'summaryText'=>"第{start}-{end}条, 共 {count} 条,页数:{page}/{pages}页", 'columns'=>array( array( 'name' => 'title', 'header' => '订单号', 'value'=>'$data[title]', //if is_array ) ), ));?>
3分页扩展
mypager是 一个非常简单的 扩展 , 结合在一个 CListPager 和 CLinkPager , 允许 用户选择 他们想要 去 或者 点击 一个页面 按钮 CLinkPager ) , 或 选择 从 下拉列表中 的 页面 ( CListPager ) 页 CGridView 。
CLinkPager首页,末页CSS默认不显示
/** * Hide first and last buttons by default. */ ul.yiiPager .first, ul.yiiPager .last { display:none; }
<?php $this->widget('zii.widgets.grid.CGridView', array( 'id'=>'customer-grid', 'dataProvider'=>$model->search(), 'pager'=>array( //'class'=>'CLinkPager', //LinkListPager 'maxButtonCount'=>8, 'firstPageLabel'=>'首页', 'lastPageLabel'=>'末页', 'nextPageLabel'=>'下一页', 'prevPageLabel'=>'上一页', 'header'=>'', 'cssFile'=>false, //如果使用主css文件,那么这里可以写false,如果使用其它附加css文件,我们这里填写css文件名。 ), ));?>