yii’s paging class CLinkPager does not support displaying a total of How many pages are displayed after the pagination? What should I do? Let’s look at the solution
1. The effect of the default CLinkPager display
The css style is written above. Let’s take a look at the pager code:
<div class="page-link"> <?php $this->widget('CLinkPager',array( 'header' => '', 'firstPageLabel' => '首页', 'lastPageLabel' => '尾页', 'prevPageLabel' => '<', 'nextPageLabel' => '>', 'pages' => $pages, 'maxButtonCount'=>5, 'htmlOptions' => array('class' => 'page-link'), //分页要使用的css样式 ));?> </div>
2. Let’s look at the desired paging effect
That is to say, how many pages are added and displayed later? How do you do this? Here I have slightly expanded the widget component CLinkPager, which looks very simple. Not much nonsense, let’s take a look at the code first:
<?php /** * 分页组建ClinkPager扩展 * @description page-tab-tog为分页的样式class * @author <[<xm 杭州>]> * @time 2016-01-29 * @example * <div class="page-tab-tog"> * <?php $this->widget('MLinkPager',array( * 'header' => '', * 'firstPageLabel' => '首页', * 'lastPageLabel' => '尾页', * 'prevPageLabel' => '<', * 'nextPageLabel' => '>', * 'pages' => $pages, * 'maxButtonCount'=>5, * 'htmlOptions' => array('class' => 'page-tab-tog'), * ));?> * </div> */ class MLinkPager extends CLinkPager { //设置为true的时候,显示共X页,$this->forceTotalPage值优先该值 public $mCountPage = false; //是否强制显示共x页,设置为true时,$this->mCountPage和$this->getPageRange()无效 public $forceTotalPage = false; public function init() { } public function run() { $this->registerClientScript(); $buttons=$this->createPageButtons(); list($beginPage,$endPage)=$this->getPageRange(); if ($this->forceTotalPage) { $buttons[] = CHtml::tag('li', array('class'=>'totle'),'共'.$this->getPageCount().'页'); } else { if ($this->mCountPage && $endPage > 0) { $buttons[] = CHtml::tag('li', array('class'=>'totle'),'共'.$this->getPageCount().'页'); } } if(empty($buttons)) return; echo $this->header; echo CHtml::tag('div',$this->htmlOptions,implode("\n",$buttons)); echo $this->footer; } }
Someone said that looking at such a pile of code gives me a headache. How can you achieve the fastest results with this thing? Come on, let’s continue to see how to use it. First, you need to copy the above extension MLinkPager intact to the MlinkPager file in the local components directory. What, you don’t have this file, create it yourself, ^~^! Okay After that, let's take a look at how to use it in the view. It is simple and cannot be too simple.
<div class="page-tab-tog"> <?php $this->widget('MLinkPager',array( 'header' => '', 'firstPageLabel' => '首页', 'lastPageLabel' => '尾页', 'prevPageLabel' => '<', 'nextPageLabel' => '>', 'pages' => $pages, 'maxButtonCount'=>5, 'mCountPage' => true, //!!!注意看这里,加一行代码就ok了 'htmlOptions' => array('class' => 'page-tab-tog'), ));?> </div>
What? You just woke up and your eyesight is not good, can't you see the difference? Pay attention to the configuration item mCountPage of MLinkPager. If this is set to true, everything will be fine!
Special note: If your list has no data, the page number will not be displayed for paging, but if there is a demand for Diaoman products, there is no list data, but you must, a total of 0 pages will be displayed, our MlinkPager You only need to set the configuration item forceTotalPage to true. At this time, setting mCountPager is invalid. Please see the MlinkPage class for details. The sub-class can be extended by yourself
The following will introduce to you how to use paging in yii
It is very convenient to use paging in yii, there are two methods as follows:
In controller:
1,
$criteria = new CDbCriteria(); //new cdbcriteria数据库<br>$criteria->id = 'id ASC'; //排序规则 $count = Exchange::model()->count($criteria); $pager = new CPagination($count); $pager->pageSize=30; $pager->applyLimit($criteria); $categoryInfo = Category::model()->findAll($criteria); //根据条件查询
2.
$criteria = new CDbCriteria(); $criteria->order = 'id ASC'; $criteria->addCondition('status=1'); //根据条件查询 $criteria->addCondition('exchange_status=0'); $count = Exchange::model()->count($criteria); $pager = new CPagination($count); $pager->pageSize=30; $pager->applyLimit($criteria); $exchangeInfo = Exchange::model()->findAll($criteria);
Parameters passed in render:
array("pages" => $pager)
Add to view:
$this->widget('CLinkPager',array( 'header'=>'', 'firstPageLabel' => '首页', 'lastPageLabel' => '末页', 'prevPageLabel' => '上一页', 'nextPageLabel' => '下一页', 'pages' => $pages, 'maxButtonCount'=>8, ) );
Paging ideas:
1. Calculate the total number of items in the database
2. Page size
3. Set offset limit
In Yii, it is important to use this class CDBcritria to perform database queries when paginating, so that pagination is very simple.