php Rewrite the pager CLinkPager instance
1. Where is the custom pager class placed?
There are two places to put it.
The first one is to put it in protected/extensions. When using it, import it, or in Import in the config file;
The second type is placed in protected/components and exists as a component. There is no need to import
2. It is best to use the derived method The
class MyPager extends CLinkPager
The entry function is: public function run(). When the pager is displayed, run() is called, and the output inside will be displayed in the corresponding position;
Others are completely customizable. If you don’t know the previous page, next page, first page, last page, total number of pages, current page number and other information, you can refer to the source code of CLinkPager, yii/frameworks/web/widgets/pagers/CLinkPager .php
<?php class MyPager extends CLinkPager { const CSS_FIRST_PAGE='first'; const CSS_LAST_PAGE='last'; const CSS_PREVIOUS_PAGE='previous'; const CSS_NEXT_PAGE='next'; const CSS_INTERNAL_PAGE='page'; const CSS_HIDDEN_PAGE='hidden'; const CSS_SELECTED_PAGE='selected'; /** * @var string the CSS class for the first page button. Defaults to 'first'. * @since 1.1.11 */ public $firstPageCssClass=self::CSS_FIRST_PAGE; /** * @var string the CSS class for the last page button. Defaults to 'last'. * @since 1.1.11 */ public $lastPageCssClass=self::CSS_LAST_PAGE; /** * @var string the CSS class for the previous page button. Defaults to 'previous'. * @since 1.1.11 */ public $previousPageCssClass=self::CSS_PREVIOUS_PAGE; /** * @var string the CSS class for the next page button. Defaults to 'next'. * @since 1.1.11 */ public $nextPageCssClass=self::CSS_NEXT_PAGE; /** * @var string the CSS class for the internal page buttons. Defaults to 'page'. * @since 1.1.11 */ public $internalPageCssClass=self::CSS_INTERNAL_PAGE; /** * @var string the CSS class for the hidden page buttons. Defaults to 'hidden'. * @since 1.1.11 */ public $hiddenPageCssClass=self::CSS_HIDDEN_PAGE; /** * @var string the CSS class for the selected page buttons. Defaults to 'selected'. * @since 1.1.11 */ public $selectedPageCssClass=self::CSS_SELECTED_PAGE; /** * @var integer maximum number of page buttons that can be displayed. Defaults to 10. */ public $maxButtonCount=10; /** * @var string the text label for the next page button. Defaults to 'Next >'. */ public $nextPageLabel; /** * @var string the text label for the previous page button. Defaults to '< Previous'. */ public $prevPageLabel; /** * @var string the text label for the first page button. Defaults to '<< First'. */ public $firstPageLabel; /** * @var string the text label for the last page button. Defaults to 'Last >>'. */ public $lastPageLabel; /** * @var string the text shown before page buttons. Defaults to 'Go to page: '. */ public $header; /** * @var string the text shown after page buttons. */ public $footer=''; /** * @var mixed the CSS file used for the widget. Defaults to null, meaning * using the default CSS file included together with the widget. * If false, no CSS file will be used. Otherwise, the specified CSS file * will be included when using this widget. */ public $cssFile; /** * @var array HTML attributes for the pager container tag. */ public $htmlOptions=array(); /** * Initializes the pager by setting some default property values. */ public function init() { if($this->nextPageLabel===null) $this->nextPageLabel=Yii::t('yii','Next >'); if($this->prevPageLabel===null) $this->prevPageLabel=Yii::t('yii','< Previous'); //if($this->firstPageLabel===null) // $this->firstPageLabel=Yii::t('yii','<< First'); //if($this->lastPageLabel===null) // $this->lastPageLabel=Yii::t('yii','Last >>'); if($this->header===null) $this->header=Yii::t('yii','Go to page: '); if(!isset($this->htmlOptions['id'])) $this->htmlOptions['id']=$this->getId(); if(!isset($this->htmlOptions['class'])) $this->htmlOptions['class']='yiiPager'; } /** * Executes the widget. * This overrides the parent implementation by displaying the generated page buttons. */ public function run() { $this->registerClientScript(); $buttons=$this->createPageButtons(); if(empty($buttons)) return; echo $this->header; // echo CHtml::tag('ul',$this->htmlOptions,implode("\n",$buttons)); echo implode("\n",$buttons); echo $this->footer; } /** * Creates the page buttons. * @return array a list of page buttons (in HTML code). */ protected function createPageButtons() { if(($pageCount=$this->getPageCount())<=1) return array(); list($beginPage,$endPage,$ellipsis)=$this->getPageRange(); $currentPage=$this->getCurrentPage(false); // currentPage is calculated in getPageRange() $buttons=array(); // first page //$buttons[]=$this->createPageButton($this->firstPageLabel,0,$this->firstPageCssClass,$currentPage<=0,false); // prev page if(($page=$currentPage-1)<0) $page=0; if($currentPage == 0){ $buttons[] = "<span style='background:#a3a3a3'><上一頁</span>"; }else{ $buttons[]=$this->createPageButton($this->prevPageLabel,$page,$this->previousPageCssClass,$currentPage<=0,false); } // internal pages start // first $buttons[]=$this->createPageButton(1,0,$this->internalPageCssClass,false,$i==$currentPage); //middle if($ellipsis == 'both'){ $buttons[] = "<span style='background:#a3a3a3'>...</span>"; } for($i=$beginPage;$i<=$endPage;++$i){ if($ellipsis == 'left' && $i == $beginPage){ $buttons[] = "<span style='background:#a3a3a3'>...</span>"; } $buttons[]=$this->createPageButton($i+1,$i,$this->internalPageCssClass,false,$i==$currentPage); if($ellipsis == 'right' && $i == $endPage){ $buttons[] = "<span style='background:#a3a3a3'>...</span>"; } } if($ellipsis == 'both'){ $buttons[] = "<span style='background:#a3a3a3'>...</span>"; } // last $buttons[]=$this->createPageButton($pageCount,$pageCount - 1,$this->internalPageCssClass,false,$i==$currentPage); // internal pages end // next page if(($page=$currentPage+1)>=$pageCount-1) $page=$pageCount-1; if($currentPage == ($pageCount-1)){ $buttons[] = "<span style='background:#a3a3a3'>下一頁></span>"; }else{ $buttons[]=$this->createPageButton($this->nextPageLabel,$page,$this->nextPageCssClass,$currentPage>=$pageCount-1,false); } // last page //$buttons[]=$this->createPageButton($this->lastPageLabel,$pageCount-1,$this->lastPageCssClass,$currentPage>=$pageCount-1,false); return $buttons; } /** * Creates a page button. * You may override this method to customize the page buttons. * @param string $label the text label for the button * @param integer $page the page number * @param string $class the CSS class for the page button. * @param boolean $hidden whether this page button is visible * @param boolean $selected whether this page button is selected * @return string the generated button */ protected function createPageButton($label,$page,$class,$hidden,$selected) { if($hidden || $selected) $class.=' '.($hidden ? $this->hiddenPageCssClass : $this->selectedPageCssClass); if ($selected) { $result = "<span>" . ++$page . "</span>"; } else { $result = CHtml::link($label,$this->createPageUrl($page)); } return $result; } /** * @return array the begin and end pages that need to be displayed. */ protected function getPageRange() { $currentPage=$this->getCurrentPage(); $pageCount=$this->getPageCount(); /*$beginPage=max(0, $currentPage-(int)($this->maxButtonCount/2)); if(($endPage=$beginPage+$this->maxButtonCount-1)>=$pageCount) { $endPage=$pageCount-1; $beginPage=max(0,$endPage-$this->maxButtonCount+1); }*/ if($pageCount > $this->maxButtonCount){ if($currentPage > 4 && $currentPage < ($pageCount - 4)){ // print_r('a'); $beginPage = $currentPage - 2; $endPage = $currentPage + 2; $ellipsis = 'both'; }else{ $beginPage=max(1, $currentPage-(int)($this->maxButtonCount/2)); if($beginPage == 1){ $ellipsis = 'right'; }else{ $ellipsis = 'left'; } if(($endPage=$beginPage+$this->maxButtonCount-1)>=$pageCount) { // print_r('b'); $endPage=$pageCount-2; $beginPage=max(1,$endPage-$this->maxButtonCount+1); }elseif(($endPage=$beginPage+$this->maxButtonCount-1)>=$pageCount-2){ // print_r('c'); $endPage=$pageCount-2; } } }else{ $beginPage=max(1, $currentPage-(int)($this->maxButtonCount/2)); if(($endPage=$beginPage+$this->maxButtonCount-1)>=$pageCount) { $endPage=$pageCount-2; $beginPage=max(1,$endPage-$this->maxButtonCount+1); } } return array($beginPage,$endPage, $ellipsis); } /** * Registers the needed client scripts (mainly CSS file). */ public function registerClientScript() { if($this->cssFile!==false) self::registerCssFile($this->cssFile); } /** * Registers the needed CSS file. * @param string $url the CSS URL. If null, a default CSS URL will be used. */ public static function registerCssFile($url=null) { if($url===null) $url=CHtml::asset(Yii::getPathOfAlias('system.web.widgets.pagers.pager').'.css'); Yii::app()->getClientScript()->registerCssFile($url); } }
3. Calling method
In the corresponding widget in View, define the pager class as the custom pager class name. Reference:
$this->widget('zii.widgets.CListView', array( 'dataProvider'=>$dataProvider, 'itemView'=>'_view_t', 'pager'=>array( 'class'=>'MyPager', ) ));
The above is the detailed content of Sample code sharing for PHP rewriting pager CLinkPager. For more information, please follow other related articles on the PHP Chinese website!

php把负数转为正整数的方法:1、使用abs()函数将负数转为正数,使用intval()函数对正数取整,转为正整数,语法“intval(abs($number))”;2、利用“~”位运算符将负数取反加一,语法“~$number + 1”。

实现方法:1、使用“sleep(延迟秒数)”语句,可延迟执行函数若干秒;2、使用“time_nanosleep(延迟秒数,延迟纳秒数)”语句,可延迟执行函数若干秒和纳秒;3、使用“time_sleep_until(time()+7)”语句。

php除以100保留两位小数的方法:1、利用“/”运算符进行除法运算,语法“数值 / 100”;2、使用“number_format(除法结果, 2)”或“sprintf("%.2f",除法结果)”语句进行四舍五入的处理值,并保留两位小数。

判断方法:1、使用“strtotime("年-月-日")”语句将给定的年月日转换为时间戳格式;2、用“date("z",时间戳)+1”语句计算指定时间戳是一年的第几天。date()返回的天数是从0开始计算的,因此真实天数需要在此基础上加1。

方法:1、用“str_replace(" ","其他字符",$str)”语句,可将nbsp符替换为其他字符;2、用“preg_replace("/(\s|\ \;||\xc2\xa0)/","其他字符",$str)”语句。

php判断有没有小数点的方法:1、使用“strpos(数字字符串,'.')”语法,如果返回小数点在字符串中第一次出现的位置,则有小数点;2、使用“strrpos(数字字符串,'.')”语句,如果返回小数点在字符串中最后一次出现的位置,则有。

在PHP中,可以利用implode()函数的第一个参数来设置没有分隔符,该函数的第一个参数用于规定数组元素之间放置的内容,默认是空字符串,也可将第一个参数设置为空,语法为“implode(数组)”或者“implode("",数组)”。

php字符串有下标。在PHP中,下标不仅可以应用于数组和对象,还可应用于字符串,利用字符串的下标和中括号“[]”可以访问指定索引位置的字符,并对该字符进行读写,语法“字符串名[下标值]”;字符串的下标值(索引值)只能是整数类型,起始值为0。


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

SublimeText3 Mac version
God-level code editing software (SublimeText3)

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Dreamweaver CS6
Visual web development tools

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
