'defaultOrder'=>array('title' => false), false是升序,true是倒序
1.自定义分页
使用CPagination呈现相关信息到页码. 当数据需要渲染到多页时,我们可能使用去呈现信息,诸如itemCount, pageSize, currentPage,等等. 这些信息被传递到pagers
$sql = "SELECT * FROM USER"; $data = Yii::app()->db->createCommand($sql)->queryAll(); $sql2 = "SELECT count(*) FROM USER"; $count = Yii::app()->db->createCommand($sql2)->queryScalar(); $pages = new CPagination($count); $pages->pageSize = 4; //$pages->setCurrentPage(2); $pdata =Yii::app()->db->createCommand($sql." LIMIT :offset,:limit"); $pdata->bindValue(':offset', $pages->currentPage*$pages->pageSize);//$pages->getOffset(); $pdata->bindValue(':limit', $pages->pageSize);//$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默认不显示
Page.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文件名。 ), ));?>
YII ajax分页
$('#search_list .yiiPager a').bind('click',function(){ $.ajax({ url:$(this).attr('href'), success:function(html){ $('#search_list').html(html); } }); return false; });