首頁  >  文章  >  php教程  >  Yii2分頁的使用及其擴展方法詳解

Yii2分頁的使用及其擴展方法詳解

高洛峰
高洛峰原創
2016-12-23 17:02:051639瀏覽

下面我總結了在Yii常用的一些yii分頁方式與實例代碼,這裡有普通分頁與ajax實現分頁,希望這篇文章對大家會有所幫助。

第一種:CListView分頁  物件形式的資料分頁

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(&#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;, 
                    ), 
                )); 
?>

第二種:CLinkPager  針對數組形式的資料分頁

三種: DAO實現分頁.

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)); 
}

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>

優點: DAO效率高; 缺點: view層需要自己寫一些樣式,有點麻煩

優點:widget實現第四種:widget實現第四種

model層:

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

Controller層:

<?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));
?>

View層:

/**
* @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;,
),
)); 
}

優點: 可以使用自帶的樣式; 缺點: 效率略低.

ajax分頁有多種實作方法,比較傳統的就是在view中寫JS來實現,大概的就是這樣:

在view中js大概邏輯是這樣

/**
* 运单统计功能:
* 按日期统计
*/
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,
));
}

然後在controller中判斷ajax請求,再使用renderPartial方法渲染局部List視圖,然後局部視圖會被view中的ajax方法填入局部刷新的div。 controller的大概邏輯是:

<?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;,
),
),
));
?>

後來發現YII中的CListview比較方便,封裝了分頁,foreach顯示list,也支援資料排序。具體的可以在YII的API手冊中發掘。使用CListview是預設ajax分頁的,使用方法如下:


controller中:

$(&#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标签
});

然後在view中:

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();
}

這樣就實現了Ajax分頁,很方便。


希望本文所述對大家以Yii框架為基礎的PHP程式設計有所幫助。

更多Yii2分頁的使用及其擴充方法詳解相關文章請關注PHP中文網!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn