Below I have summarized some Yii paging methods and example codes commonly used in Yii. Here are ordinary paging and ajax paging. I hope this article will be helpful to everyone.
The first type: CListView paging for data in the form of objects
Controller:
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' => '末页', ), )); ?>
The second type: CLinkPager for data paging in the form of arrays
Controller:
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>
Three types: DAO implements paging.
Controller layer:
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 layer:
<?php foreach($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)); ?>
Advantages: DAO is highly efficient; Disadvantages: The view layer needs to write some styles by itself, which is a little troublesome
The fourth type: widget implements paging
model layer:
/** * @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 layer:
/** * 运单统计功能: * 按日期统计 */ public function actionReport() { $model=new Delivery('statistics'); $model->unsetAttributes(); // clear any default values $this->render('report',array( 'model'=>$model, )); }
View layer:
<?php $this->widget('zii.widgets.grid.CGridView', array( 'id'=>'delivery-grid', 'dataProvider'=>$model->statistics(), 'filter'=>$model, 'columns'=>array( 'remitdate', 'dayrate', array( 'class'=>'CButtonColumn', ), ), )); ?>
Advantages: You can use your own style; Disadvantages: Slightly less efficient.
Fifth type: Ajax paging
YII There are many ways to implement ajax paging. The more traditional one is to write JS in the view. It is roughly like this:
The approximate logic of js in the view is like this
$('#listview .yiiPager a').live('click',function(){ $.ajax({ url:$(this).attr('href'), success:function(html){ $('#listview').html(html); } }); return false;//阻止a标签 });
Then judge the ajax request in the controller, and then use the renderPartial method to render Partial List view, and then the partial view will be filled into the partially refreshed div by the ajax method in the view. The approximate logic of the controller is:
if (Yii::app()->request->isAjaxRequest) { $this->renderPartial('_comments',array( 'model' => $model, 'comments' => $comments,//在局部视图中foreach得到每条数据 'pages' => $pages, )); Yii::app()->end(); }
Later I found that CListview in YII is more convenient. It encapsulates paging, foreach displays the list, and also supports data sorting. Details can be found in the YII API manual. Ajax paging is used by default when using CListview. The usage method is as follows:
In the controller:
$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, ));
Then in the view:
<?php $this->widget('zii.widgets.CListView', array( 'dataProvider'=>$dataProvider, 'itemView'=>'_comments', //'ajaxUpdate'=> false,//这样就不会AJAX翻页 'pager' => array(//pager 的配置信息。默认为<CODE>array('class'=>'CLinkPager')</CODE>.也可以自己配置 'nextPageLabel' => '下一页 »', 'prevPageLabel' => '« 上一页' ), //在这里还可以配置一些排序规则,具体可以查阅手册 )); ?>
This way Ajax paging is implemented, which is very convenient.
I hope this article will be helpful to everyone’s PHP program design based on the Yii framework.
For more detailed explanations on the use of Yii2 paging and its extension methods, please pay attention to the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SublimeText3 Chinese version
Chinese version, very easy to use

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

Dreamweaver Mac version
Visual web development tools

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.