YII CLinkPager page Type Extension adds the total number of pages displayed, yiiclinkpager
By default, yii's pagination CLinkPager does not support displaying a total of x pages. Now we need to display the total number of pages after the pages. What should we do? Let's look at the solution
1. Default CLinkPager Display Effect
The css style is written here. Let's look at the pager code:
<Div class = "page-link"> <? Php $ this-> widget ('clinkpager', array ('header' => '', 'firstpagelabel '=> 'homepage', 'lastpagelabel' => 'tail ', 'prevpagelabel '=>' <', 'nextpagelabel' => ', 'pages' => $ pages, 'maxbuttoncount' => 5, 'htmlopexception' => array ('class' => 'page-link'), // css style to be used for paging);?> </Div>
2. Let's look at the desired paging effect.
That is to say, how many pages are added later? Here, I expanded the widget component CLinkPager a little bit. It looks very simple. It's not a lot of nonsense. Let's take a look at the code first:
<? Php/*** build the ClinkPager extension by page * @ description page-tab-tog is the pagination style class * @ author <[<xm Hangzhou>]> * @ time 2016-01-29 * @ example * <div class = "page-tab-tog"> * <? Php $ this-> widget ('mlinkpager', array (* 'header' => '', * 'firstpagelabel '=> 'homepage ', * 'lastpagelabel '=> 'tail', * 'prevpagelabel' => '<', * 'nextpagelabel '=>', * 'page' => $ pages, * 'maxbuttoncount' => 5, * 'htmlopexception' => array ('class' => 'page-tab-tog'), *);?> * </Div> */class MLinkPager extends CLinkPager {// when set to true, a total of X pages are displayed. $ this-> forceTotalPage value takes precedence over public $ mCountPage = false; // whether to forcibly display a total of x pages. When set to true, $ this-> mCountPage and $ this-> getPageRange () are invalid 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 ('lil ', array ('class' => 'totle'), 'col '. $ this-> getPageCount (). 'page');} else {if ($ this-> mCountPage & $ endPage> 0) {$ buttons [] = CHtml: tag ('lil ', array ('class' => 'totle'), 'col '. $ this-> getPageCount (). 'page') ;}} if (empty ($ buttons) return; echo $ this-> header; echo CHtml: tag ('div', $ this-> htmlOptions, implode ("\ n", $ buttons); echo $ this-> footer ;}}
Some people have said that, looking at a bunch of code, you have a headache. How can you see the results as quickly as possible? Let's continue to see how to use it. First, you need to copy the extended MLinkPager to the MlinkPager file in the local components directory. What, you don't have this file, create by yourself, ^ ~ ^! Now let's take a look at how the view is used. It's simple and cannot be too simple.
<Div class = "page-tab-tog"> <? Php $ this-> widget ('mlinkpager', array ('header' => '', 'firstpagelabel '=> 'homepage', 'lastpagelabel' => 'tail ', 'prevpagelabel '=>' <', 'nextpagelabel' => ', 'pages' => $ pages, 'maxbuttoncount' => 5, 'mcountpage' => true, //!!! Note: Here, you can add a line of code to 'htmloptions '=> array ('class' => 'page-tab-tog');?> </Div>
What? Why didn't you see the difference when you woke up? Check mCountPage, the configuration item of MLinkPager. If this parameter is set to true, everything is fine!
NOTE: If there is no data in your list, the page number is not displayed, but if there is a requirement for the product, there is no list data, however, you must display a total of 0 pages. For MlinkPager, you only need to set the configuration item forceTotalPage to true. In this case, setting mCountPager is invalid. For details, see the MlinkPage class, the secondary class can be expanded by itself
The following describes how to use pagination in yii.
Using paging in yii is very convenient, as shown in the following two methods:
In the controller:
1,
$ Criteria = new CDbCriteria (); // new cdbcriteria database <br> $ criteria-> id = 'id ASC '; // sorting rule $ count = Exchange: model () -> count ($ criteria); $ pager = new CPagination ($ count); $ pager-> pageSize = 30; $ pager-> applyLimit ($ criteria ); $ categoryInfo = Category: model ()-> findAll ($ criteria); // query by condition
2,
$ Criteria = new CDbCriteria (); $ criteria-> order = 'id ASC '; $ criteria-> addCondition ('status = 1 '); // query $ criteria-> addCondition ('exchange _ status = 0') based on conditions; $ count = exchange: model ()-> count ($ criteria ); $ pager = new CPagination ($ count); $ pager-> pageSize = 30; $ pager-> applyLimit ($ criteria); $ exchangeInfo = Exchange: model () -> findAll ($ criteria );
Input parameters in render:
array("pages" => $pager)
Add to view:
$ This-> widget ('clinkpager', array ('header' => '', 'firstpagelabel '=> 'homepage', 'lastpagelabel '=> 'last page ', 'prevpagelabel '=> 'previous page', 'nextpagelabel' => 'Next page', 'pages' => $ pages, 'maxbuttoncount' => 8 ,));
Paging idea:
1. calculate the total number of items in the database
2. Page size
3. Set the offset limit.
In Yii, it is very important to use CDBcritria for database queries during paging, so that paging is simple.
Articles you may be interested in:
- Detailed description of Yii paging instance using CLinkPager