Home > php教程 > PHP开发 > Detailed explanation of the use of Yii2 paging and its extension methods

Detailed explanation of the use of Yii2 paging and its extension methods

高洛峰
Release: 2016-12-23 17:02:05
Original
1680 people have browsed it

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

View:

<?php 
$this->widget(&#39;zii.widgets.CListView&#39;, array( 
    &#39;dataProvider&#39; => $dataProvider, //数据 
    &#39;itemView&#39; => &#39;_view&#39;, //显示的模版 
    &#39;id&#39; => Yii::app()->controller->id, 
    &#39;itemsTagName&#39; => &#39;ul&#39;, 
    &#39;ajaxVar&#39; => &#39;&#39;, //默认为page或ajax 去掉后url更简洁 
    &#39;htmlOptions&#39; => array(&#39;class&#39; => Yii::app()->controller->id), 
    &#39;loadingCssClass&#39; => &#39;loading&#39;, //默认为list-view-loading 
    //&#39;template&#39; => &#39;{summary}{sorter}{items}{pager}&#39;,//显示的顺序 
    //&#39;ajaxUpdate&#39; => false, //是否ajax分页  false或分页显示的容器id 
    //&#39;beforeAjaxUpdate&#39; => &#39;before_ajax_update&#39;,   //回调函数 在common.js里完成 
    //&#39;afterAjaxUpdate&#39; => &#39;after_ajax_update&#39;,   
    &#39;emptyText&#39; => &#39; 
<DIV class="alert alert-waning"> 
    暂无数据! 
</DIV> 
&#39;, //无数据时显示内容 
                    &#39;pagerCssClass&#39; => &#39;pagination&#39;, //分页的class 
                    &#39;pager&#39; => array( 
                        &#39;selectedPageCssClass&#39; => &#39;active&#39;, //当前页的class 
                        &#39;hiddenPageCssClass&#39; => &#39;disabled&#39;, //禁用页的class 
                        &#39;header&#39; => &#39;&#39;, //分页前显示的内容 
                        &#39;maxButtonCount&#39; => 10, //显示分页数量 
                        &#39;htmlOptions&#39; => array(&#39;class&#39; => &#39;&#39;), 
                        &#39;firstPageLabel&#39; => &#39;首页&#39;, 
                        &#39;nextPageLabel&#39; => &#39;下一页&#39;, 
                        &#39;prevPageLabel&#39; => &#39;上一页&#39;, 
                        &#39;lastPageLabel&#39; => &#39;末页&#39;, 
                    ), 
                )); 
?>
Copy after login

The second type: CLinkPager for data paging in the form of arrays

Controller:

public function actionIndex() { 
  
        $criteria = new CDbCriteria(); 
        $criteria->order = &#39;news_id DESC&#39;; 
        $criteria->condition = &#39;user_id = 1&#39;; 
  
        $count = News::model()->count($criteria); 
        $pages = new CPagination($count); 
  
        $pages->pageSize = 10; 
        $pages->applyLimit($criteria); 
        $list = News::model()->findAll($criteria); 
  
        $this->render(&#39;index&#39;, array(&#39;list&#39; => $list, &#39;pages&#39; => $pages)); 
}
Copy after login

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(&#39;CLinkPager&#39;, array( 
        &#39;pages&#39; => $pages, 
        &#39;selectedPageCssClass&#39; => &#39;active&#39;, //当前页的class 
        &#39;hiddenPageCssClass&#39; => &#39;disabled&#39;, //禁用页的class 
        &#39;header&#39; => &#39;&#39;, //分页前显示的内容 
        &#39;maxButtonCount&#39; => 10, //显示分页数量 
        &#39;htmlOptions&#39; => array(&#39;class&#39; => &#39;&#39;), 
        &#39;firstPageLabel&#39; => &#39;首页&#39;, 
        &#39;nextPageLabel&#39; => &#39;下一页&#39;, 
        &#39;prevPageLabel&#39; => &#39;上一页&#39;, 
        &#39;lastPageLabel&#39; => &#39;末页&#39;, 
            ) 
    ); 
    ?> 
</DIV>
Copy after login

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(&#39;:offset&#39;, $pages->currentPage*$pages->pageSize); 
$result->bindValue(&#39;:limit&#39;, $pages->pageSize); 
$posts=$result->query();
$this->render(&#39;report&#39;,array( 
&#39;posts&#39;=>$posts, 
&#39;pages&#39;=>$pages, 
));
}
Copy after login

View layer:

<?php foreach($posts as $row):?> 
 <?php echo CHtml::link($row["remitdate"],array(&#39;delivery/view&#39;,&#39;remitdate&#39;=>$row["sumrate"]));?>
 <?php echo $row["sumrate"]."<br />" ?>
 <?php endforeach;?>
 <?php 
 //分页widget代码: 
 $this->widget(&#39;CLinkPager&#39;,array(&#39;pages&#39;=>$pages));
?>
Copy after login

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 = &#39;remitdate, sum(rate) AS dayrate&#39;;
$criteria->group = &#39;remitdate&#39;;
return new CActiveDataProvider(get_class($this), array(
&#39;criteria&#39;=>$criteria,
&#39;sort&#39;=>array(
// 表头设置点击排序的字段
&#39;attributes&#39;=>array(
&#39;remitdate&#39;,
&#39;dayrate&#39;=>array(
&#39;asc&#39;=>&#39;dayrate&#39;,
&#39;desc&#39;=>&#39;dayrate DESC&#39;,
)
),
&#39;defaultOrder&#39;=>&#39;remitdate desc&#39;,
),
)); 
}
Copy after login

Controller layer:

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

View layer:

<?php $this->widget(&#39;zii.widgets.grid.CGridView&#39;, array(
&#39;id&#39;=>&#39;delivery-grid&#39;,
&#39;dataProvider&#39;=>$model->statistics(),
&#39;filter&#39;=>$model,
&#39;columns&#39;=>array(
&#39;remitdate&#39;,
&#39;dayrate&#39;,
array(
&#39;class&#39;=>&#39;CButtonColumn&#39;,
),
),
));
?>
Copy after login

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

$(&#39;#listview .yiiPager a&#39;).live(&#39;click&#39;,function(){
        $.ajax({
            url:$(this).attr(&#39;href&#39;),
            success:function(html){
                $(&#39;#listview&#39;).html(html);
            }
        });
        return false;//阻止a标签
});
Copy after login

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(&#39;_comments&#39;,array(
    &#39;model&#39; => $model,
    &#39;comments&#39; => $comments,//在局部视图中foreach得到每条数据
    &#39;pages&#39; => $pages,
   ));
    Yii::app()->end();
}
Copy after login

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 = &#39;`create_time` DESC&#39;;
$dataProvider = new CActiveDataProvider(&#39;Comments&#39;, array(
    &#39;pagination&#39;=>array(
      &#39;pageSize&#39;=>Yii::app()->params[&#39;commentsPerPage&#39;],//设置分页条数以确定取出数据的条数
  ),
  &#39;criteria&#39;=>$criteria,
  ));
$this->render(&#39;comments&#39;,array(
         &#39;model&#39; => $model,
         &#39;dataProvider&#39; => $dataProvider,
));
Copy after login

Then in the view:

<?php $this->widget(&#39;zii.widgets.CListView&#39;, array(
  &#39;dataProvider&#39;=>$dataProvider,
  &#39;itemView&#39;=>&#39;_comments&#39;,
  //&#39;ajaxUpdate&#39;=> false,//这样就不会AJAX翻页
  &#39;pager&#39; => array(//pager 的配置信息。默认为<CODE>array(&#39;class&#39;=>&#39;CLinkPager&#39;)</CODE>.也可以自己配置
   &#39;nextPageLabel&#39; => &#39;下一页 »&#39;,
   &#39;prevPageLabel&#39; => &#39;« 上一页&#39;
  ),
//在这里还可以配置一些排序规则,具体可以查阅手册
));
?>
Copy after login

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!


Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template