ASCII码 ASCII码

php 重写分页器 CLinkPager的实例

发布于:2022-04-24 09:33:16  栏目:技术文档

这篇文章主要介绍了php 重写分页器 CLinkPager的实例的相关资料,希望通过本文能帮助到大家,让大家实现这样的功能,需要的朋友可以参考下

php 重写分页器 CLinkPager的实例

1、自定义的分页器类放在哪里?

有两个位置可以放,第一种是放在 protected/extensions 中,在使用是import进来,或在config文件中import进来;第二种是放在 protected/components 中,作为组件存在,不需要import

2、用派生方式是最好的

  1. class MyPager extends CLinkPager

入口函数是:public function run() ,当显示分页器时run()被调用,里面的输出就会显示在相应位置;其他的完全自定义,如果你不知道上一页、下一页、首页、尾页、总页数、当前页码等信息,可以参考CLinkPager的源码,yii/frameworks/web/widgets/pagers/CLinkPager.php

  1. <?php
  2. class MyPager extends CLinkPager
  3. {
  4. const CSS_FIRST_PAGE='first';
  5. const CSS_LAST_PAGE='last';
  6. const CSS_PREVIOUS_PAGE='previous';
  7. const CSS_NEXT_PAGE='next';
  8. const CSS_INTERNAL_PAGE='page';
  9. const CSS_HIDDEN_PAGE='hidden';
  10. const CSS_SELECTED_PAGE='selected';
  11. /**
  12. * @var string the CSS class for the first page button. Defaults to 'first'.
  13. * @since 1.1.11
  14. */
  15. public $firstPageCssClass=self::CSS_FIRST_PAGE;
  16. /**
  17. * @var string the CSS class for the last page button. Defaults to 'last'.
  18. * @since 1.1.11
  19. */
  20. public $lastPageCssClass=self::CSS_LAST_PAGE;
  21. /**
  22. * @var string the CSS class for the previous page button. Defaults to 'previous'.
  23. * @since 1.1.11
  24. */
  25. public $previousPageCssClass=self::CSS_PREVIOUS_PAGE;
  26. /**
  27. * @var string the CSS class for the next page button. Defaults to 'next'.
  28. * @since 1.1.11
  29. */
  30. public $nextPageCssClass=self::CSS_NEXT_PAGE;
  31. /**
  32. * @var string the CSS class for the internal page buttons. Defaults to 'page'.
  33. * @since 1.1.11
  34. */
  35. public $internalPageCssClass=self::CSS_INTERNAL_PAGE;
  36. /**
  37. * @var string the CSS class for the hidden page buttons. Defaults to 'hidden'.
  38. * @since 1.1.11
  39. */
  40. public $hiddenPageCssClass=self::CSS_HIDDEN_PAGE;
  41. /**
  42. * @var string the CSS class for the selected page buttons. Defaults to 'selected'.
  43. * @since 1.1.11
  44. */
  45. public $selectedPageCssClass=self::CSS_SELECTED_PAGE;
  46. /**
  47. * @var integer maximum number of page buttons that can be displayed. Defaults to 10.
  48. */
  49. public $maxButtonCount=10;
  50. /**
  51. * @var string the text label for the next page button. Defaults to 'Next >'.
  52. */
  53. public $nextPageLabel;
  54. /**
  55. * @var string the text label for the previous page button. Defaults to '< Previous'.
  56. */
  57. public $prevPageLabel;
  58. /**
  59. * @var string the text label for the first page button. Defaults to '<< First'.
  60. */
  61. public $firstPageLabel;
  62. /**
  63. * @var string the text label for the last page button. Defaults to 'Last >>'.
  64. */
  65. public $lastPageLabel;
  66. /**
  67. * @var string the text shown before page buttons. Defaults to 'Go to page: '.
  68. */
  69. public $header;
  70. /**
  71. * @var string the text shown after page buttons.
  72. */
  73. public $footer='';
  74. /**
  75. * @var mixed the CSS file used for the widget. Defaults to null, meaning
  76. * using the default CSS file included together with the widget.
  77. * If false, no CSS file will be used. Otherwise, the specified CSS file
  78. * will be included when using this widget.
  79. */
  80. public $cssFile;
  81. /**
  82. * @var array HTML attributes for the pager container tag.
  83. */
  84. public $htmlOptions=array();
  85. /**
  86. * Initializes the pager by setting some default property values.
  87. */
  88. public function init()
  89. {
  90. if($this->nextPageLabel===null)
  91. $this->nextPageLabel=Yii::t('yii','Next >');
  92. if($this->prevPageLabel===null)
  93. $this->prevPageLabel=Yii::t('yii','< Previous');
  94. //if($this->firstPageLabel===null)
  95. // $this->firstPageLabel=Yii::t('yii','<< First');
  96. //if($this->lastPageLabel===null)
  97. // $this->lastPageLabel=Yii::t('yii','Last >>');
  98. if($this->header===null)
  99. $this->header=Yii::t('yii','Go to page: ');
  100. if(!isset($this->htmlOptions['id']))
  101. $this->htmlOptions['id']=$this->getId();
  102. if(!isset($this->htmlOptions['class']))
  103. $this->htmlOptions['class']='yiiPager';
  104. }
  105. /**
  106. * Executes the widget.
  107. * This overrides the parent implementation by displaying the generated page buttons.
  108. */
  109. public function run()
  110. {
  111. $this->registerClientScript();
  112. $buttons=$this->createPageButtons();
  113. if(empty($buttons))
  114. return;
  115. echo $this->header;
  116. // echo CHtml::tag('ul',$this->htmlOptions,implode("\n",$buttons));
  117. echo implode("\n",$buttons);
  118. echo $this->footer;
  119. }
  120. /**
  121. * Creates the page buttons.
  122. * @return array a list of page buttons (in HTML code).
  123. */
  124. protected function createPageButtons()
  125. {
  126. if(($pageCount=$this->getPageCount())<=1)
  127. return array();
  128. list($beginPage,$endPage,$ellipsis)=$this->getPageRange();
  129. $currentPage=$this->getCurrentPage(false); // currentPage is calculated in getPageRange()
  130. $buttons=array();
  131. // first page
  132. //$buttons[]=$this->createPageButton($this->firstPageLabel,0,$this->firstPageCssClass,$currentPage<=0,false);
  133. // prev page
  134. if(($page=$currentPage-1)<0)
  135. $page=0;
  136. if($currentPage == 0){
  137. $buttons[] = "<span style='background:#a3a3a3'><上一頁</span>";
  138. }else{
  139. $buttons[]=$this->createPageButton($this->prevPageLabel,$page,$this->previousPageCssClass,$currentPage<=0,false);
  140. }
  141. // internal pages start
  142. // first
  143. $buttons[]=$this->createPageButton(1,0,$this->internalPageCssClass,false,$i==$currentPage);
  144. //middle
  145. if($ellipsis == 'both'){
  146. $buttons[] = "<span style='background:#a3a3a3'>...</span>";
  147. }
  148. for($i=$beginPage;$i<=$endPage;++$i){
  149. if($ellipsis == 'left' && $i == $beginPage){
  150. $buttons[] = "<span style='background:#a3a3a3'>...</span>";
  151. }
  152. $buttons[]=$this->createPageButton($i+1,$i,$this->internalPageCssClass,false,$i==$currentPage);
  153. if($ellipsis == 'right' && $i == $endPage){
  154. $buttons[] = "<span style='background:#a3a3a3'>...</span>";
  155. }
  156. }
  157. if($ellipsis == 'both'){
  158. $buttons[] = "<span style='background:#a3a3a3'>...</span>";
  159. }
  160. // last
  161. $buttons[]=$this->createPageButton($pageCount,$pageCount - 1,$this->internalPageCssClass,false,$i==$currentPage);
  162. // internal pages end
  163. // next page
  164. if(($page=$currentPage+1)>=$pageCount-1)
  165. $page=$pageCount-1;
  166. if($currentPage == ($pageCount-1)){
  167. $buttons[] = "<span style='background:#a3a3a3'>下一頁></span>";
  168. }else{
  169. $buttons[]=$this->createPageButton($this->nextPageLabel,$page,$this->nextPageCssClass,$currentPage>=$pageCount-1,false);
  170. }
  171. // last page
  172. //$buttons[]=$this->createPageButton($this->lastPageLabel,$pageCount-1,$this->lastPageCssClass,$currentPage>=$pageCount-1,false);
  173. return $buttons;
  174. }
  175. /**
  176. * Creates a page button.
  177. * You may override this method to customize the page buttons.
  178. * @param string $label the text label for the button
  179. * @param integer $page the page number
  180. * @param string $class the CSS class for the page button.
  181. * @param boolean $hidden whether this page button is visible
  182. * @param boolean $selected whether this page button is selected
  183. * @return string the generated button
  184. */
  185. protected function createPageButton($label,$page,$class,$hidden,$selected)
  186. {
  187. if($hidden || $selected)
  188. $class.=' '.($hidden ? $this->hiddenPageCssClass : $this->selectedPageCssClass);
  189. if ($selected) {
  190. $result = "<span>" . ++$page . "</span>";
  191. } else {
  192. $result = CHtml::link($label,$this->createPageUrl($page));
  193. }
  194. return $result;
  195. }
  196. /**
  197. * @return array the begin and end pages that need to be displayed.
  198. */
  199. protected function getPageRange()
  200. {
  201. $currentPage=$this->getCurrentPage();
  202. $pageCount=$this->getPageCount();
  203. /*$beginPage=max(0, $currentPage-(int)($this->maxButtonCount/2));
  204. if(($endPage=$beginPage+$this->maxButtonCount-1)>=$pageCount)
  205. {
  206. $endPage=$pageCount-1;
  207. $beginPage=max(0,$endPage-$this->maxButtonCount+1);
  208. }*/
  209. if($pageCount > $this->maxButtonCount){
  210. if($currentPage > 4 && $currentPage < ($pageCount - 4)){
  211. // print_r('a');
  212. $beginPage = $currentPage - 2;
  213. $endPage = $currentPage + 2;
  214. $ellipsis = 'both';
  215. }else{
  216. $beginPage=max(1, $currentPage-(int)($this->maxButtonCount/2));
  217. if($beginPage == 1){
  218. $ellipsis = 'right';
  219. }else{
  220. $ellipsis = 'left';
  221. }
  222. if(($endPage=$beginPage+$this->maxButtonCount-1)>=$pageCount)
  223. {
  224. // print_r('b');
  225. $endPage=$pageCount-2;
  226. $beginPage=max(1,$endPage-$this->maxButtonCount+1);
  227. }elseif(($endPage=$beginPage+$this->maxButtonCount-1)>=$pageCount-2){
  228. // print_r('c');
  229. $endPage=$pageCount-2;
  230. }
  231. }
  232. }else{
  233. $beginPage=max(1, $currentPage-(int)($this->maxButtonCount/2));
  234. if(($endPage=$beginPage+$this->maxButtonCount-1)>=$pageCount)
  235. {
  236. $endPage=$pageCount-2;
  237. $beginPage=max(1,$endPage-$this->maxButtonCount+1);
  238. }
  239. }
  240. return array($beginPage,$endPage, $ellipsis);
  241. }
  242. /**
  243. * Registers the needed client scripts (mainly CSS file).
  244. */
  245. public function registerClientScript()
  246. {
  247. if($this->cssFile!==false)
  248. self::registerCssFile($this->cssFile);
  249. }
  250. /**
  251. * Registers the needed CSS file.
  252. * @param string $url the CSS URL. If null, a default CSS URL will be used.
  253. */
  254. public static function registerCssFile($url=null)
  255. {
  256. if($url===null)
  257. $url=CHtml::asset(Yii::getPathOfAlias('system.web.widgets.pagers.pager').'.css');
  258. Yii::app()->getClientScript()->registerCssFile($url);
  259. }
  260. }

调用方式在View里的相应widget,定义pager的class为自定义的分页器类名即可,参考:

  1. $this->widget('zii.widgets.CListView', array(
  2. 'dataProvider'=>$dataProvider,
  3. 'itemView'=>'_view_t',
  4. 'pager'=>array(
  5. 'class'=>'MyPager',
  6. )
  7. ));
相关推荐
阅读 +