当前位置:Gxlcms > PHP教程 > Yii使用CLinkPager进行的分页

Yii使用CLinkPager进行的分页

时间:2021-07-01 10:21:17 帮助过:2人阅读

这篇文章主要介绍了Yii使用CLinkPager分页的方法,需要的朋友可以参考下

本文主要讲解了YII中使用CLinkPager分页的方法,这里我们采用物件的形式来定义分页:

首先在components中自定义LinkPager,并继承CLinkPager

具体代码如下:

  1. <?php
  2. /**
  3. * CLinkPager class file.
  4. *
  5. * @link http://www.yiiframework.com/
  6. * @copyright Copyright © 2008-2011 Yii Software LLC
  7. * @license http://www.yiiframework.com/license/
  8. */
  9. /**
  10. * CLinkPager displays a list of hyperlinks that lead to different pages of target.
  11. *
  12. * @version $Id$
  13. * @package system.web.widgets.pagers
  14. * @since 1.0
  15. */
  16. class LinkPager extends CLinkPager
  17. {
  18. const CSS_TOTAL_PAGE='total_page';
  19. const CSS_TOTAL_ROW='total_row';
  20. /**
  21. * @var string the text label for the first page button. Defaults to '<< First'.
  22. */
  23. public $totalPageLabel;
  24. /**
  25. * @var string the text label for the last page button. Defaults to 'Last >>'.
  26. */
  27. public $totalRowLabel;
  28. /**
  29. * Creates the page buttons.
  30. * @return array a list of page buttons (in HTML code).
  31. */
  32. protected function createPageButtons()
  33. {
  34. $this->maxButtonCount=8;
  35. $this->firstPageLabel="首页";
  36. $this->lastPageLabel='末页';
  37. $this->nextPageLabel='下一页';
  38. $this->prevPageLabel='上一页';
  39. $this->header="";
  40. if(($pageCount=$this->getPageCount())<=1)
  41. return array();
  42. list($beginPage,$endPage)=$this->getPageRange();
  43. $currentPage=$this->getCurrentPage(false); // currentPage is calculated in getPageRange()
  44. $buttons=array();
  45. // first page
  46. $buttons[]=$this->createPageButton($this->firstPageLabel,0,self::CSS_FIRST_PAGE,$currentPage<=0,false);
  47. // prev page
  48. if(($page=$currentPage-1)<0)
  49. $page=0;
  50. $buttons[]=$this->createPageButton($this->prevPageLabel,$page,self::CSS_PREVIOUS_PAGE,$currentPage<=0,false);
  51. // internal pages
  52. for($i=$beginPage;$i<=$endPage;++$i)
  53. $buttons[]=$this->createPageButton($i+1,$i,self::CSS_INTERNAL_PAGE,false,$i==$currentPage);
  54. // next page
  55. if(($page=$currentPage+1)>=$pageCount-1)
  56. $page=$pageCount-1;
  57. $buttons[]=$this->createPageButton($this->nextPageLabel,$page,self::CSS_NEXT_PAGE,$currentPage>=$pageCount-1,false);
  58. // last page
  59. $buttons[]=$this->createPageButton($this->lastPageLabel,$pageCount-1,self::CSS_LAST_PAGE,$currentPage>=$pageCount-1,false);
  60. // 页数统计
  61. $buttons[]=$this->createTotalButton(($currentPage+1)."/{$pageCount}",self::CSS_TOTAL_PAGE,false,false);
  62. // 条数统计
  63. $buttons[]=$this->createTotalButton("共{$this->getItemCount()}条",self::CSS_TOTAL_ROW,false,false);
  64. return $buttons;
  65. }
  66. protected function createTotalButton($label,$class,$hidden,$selected)
  67. {
  68. if($hidden || $selected)
  69. $class.=' '.($hidden ? self::CSS_HIDDEN_PAGE : self::CSS_SELECTED_PAGE);
  70. return '<li class="'.$class.'">'.CHtml::label($label,false).'</li>';
  71. }
  72. /**
  73. * Registers the needed client scripts (mainly CSS file).
  74. */
  75. public function registerClientScript()
  76. {
  77. if($this->cssFile!==false)
  78. self::registerCssFile($this->cssFile);
  79. }
  80. /**
  81. * Registers the needed CSS file.
  82. * @param string $url the CSS URL. If null, a default CSS URL will be used.
  83. */
  84. public static function registerCssFile($url=null)
  85. {
  86. if($url===null)
  87. $url=CHtml::asset(Yii::getPathOfAlias('application.components.views.LinkPager.pager').'.css');
  88. Yii::app()->getClientScript()->registerCssFile($url);
  89. }
  90. }

定义CSS样式

  1. /**
  2. * 翻页样式
  3. */
  4. .page_blue{
  5. margin: 3px;
  6. padding: 3px;
  7. text-align: center;
  8. font: 12px verdana, arial, helvetica, sans-serif;
  9. }
  10. ul.bluePager,ul.yiiPager
  11. {
  12. font-size:11px;
  13. border:0;
  14. margin:0;
  15. padding:0;
  16. line-height:100%;
  17. display:inline;
  18. text-aligin:center;
  19. }
  20. ul.bluePager li,ul.yiiPager li
  21. {
  22. display:inline;
  23. }
  24. ul.bluePager a:link,ul.yiiPager a:link,
  25. ul.bluePager a:visited,ul.yiiPager a:visited,
  26. ul.bluePager .total_page label,ul.yiiPager .total_page label,
  27. ul.bluePager .total_row label,ul.yiiPager .total_row label
  28. {
  29. border: #ddd 1px solid;
  30. color: #888888 !important;
  31. padding:2px 5px;
  32. text-decoration:none;
  33. }
  34. ul.bluePager .page a,ul.yiiPager .page a
  35. {
  36. font-weight:normal;
  37. }
  38. ul.bluePager a:hover,ul.yiiPager a:hover
  39. {
  40. color:#FFF !important; border:#156a9a 1px solid; background-color:#2b78a3
  41. }
  42. ul.bluePager .selected a,ul.yiiPager bluePager .selected a
  43. {
  44. color:#3aa1d0 !important;
  45. border: 1px solid #3aa1d0;
  46. }
  47. ul.bluePager .selected a:hover,ul.yiiPager .selected a:hover
  48. {
  49. color:#FFF !important;
  50. }
  51. ul.bluePager .hidden a,ul.yiiPager .hidden a
  52. {
  53. border:solid 1px #DEDEDE;
  54. color:#888888;
  55. }
  56. ul.bluePager .hidden,ul.yiiPager .hidden
  57. {
  58. display:none;
  59. }

controller中操作:

  1. //分页操作
  2. $criteria=new CDbCriteria;
  3. $criteria->order='id DESC';
  4. $criteria->select=array('id','uid','username','title','thumb','url','clicks','time','dateline','countfavorite','quality');
  5. $criteria->condition=$sql;
  6. $total = Video::model()->count($criteria);
  7. $pages = new CPagination($total);
  8. $pages->pageSize=self::PAGE_SIZE;
  9. $pages->applyLimit($criteria);
  10. $list = Video::model()->findAll($criteria);
  11. $title = CommonClass::model()->find(array(
  12. 'select'=>array('cname'),
  13. 'condition'=>'id = '.$id,
  14. ));
  15. $this->render('application.views.video.list',array(
  16. 'array'=>$array,
  17. 'arr'=>$arr,
  18. 'result'=>$result,
  19. 'list'=>$list,
  20. 'pages'=>$pages,
  21. 'title'=>$title,
  22. ));

在views/video/list.php中引用:

  1. <?php
  2. $this->widget('LinkPager', array('pages' => $pages,));
  3. ?>

以上就是本文的全部内容,希望对大家的学习有所帮助,更多相关内容请关注PHP中文网!

相关推荐:

yii2组件实现下拉框带搜索功能

对于Yii2.0表关联查询的分析

如何用yii去掉必填项中的星号

以上就是Yii使用CLinkPager进行的分页的详细内容,更多请关注Gxl网其它相关文章!

人气教程排行