CListPager.php 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. <?php
  2. /**
  3. * CListPager class file.
  4. *
  5. * @author Qiang Xue <qiang.xue@gmail.com>
  6. * @link http://www.yiiframework.com/
  7. * @copyright 2008-2013 Yii Software LLC
  8. * @license http://www.yiiframework.com/license/
  9. */
  10. /**
  11. * CListPager displays a dropdown list that contains options leading to different pages of target.
  12. *
  13. * @author Qiang Xue <qiang.xue@gmail.com>
  14. * @package system.web.widgets.pagers
  15. * @since 1.0
  16. */
  17. class CListPager extends CBasePager
  18. {
  19. /**
  20. * @var string the text shown before page buttons. Defaults to 'Go to page: '.
  21. */
  22. public $header;
  23. /**
  24. * @var string the text shown after page buttons.
  25. */
  26. public $footer;
  27. /**
  28. * @var string the text displayed as a prompt option in the dropdown list. Defaults to null, meaning no prompt.
  29. */
  30. public $promptText;
  31. /**
  32. * @var string the format string used to generate page selection text.
  33. * The sprintf function will be used to perform the formatting.
  34. */
  35. public $pageTextFormat;
  36. /**
  37. * @var array HTML attributes for the enclosing 'div' tag.
  38. */
  39. public $htmlOptions=array();
  40. /**
  41. * Initializes the pager by setting some default property values.
  42. */
  43. public function init()
  44. {
  45. if($this->header===null)
  46. $this->header=Yii::t('yii','Go to page: ');
  47. if(!isset($this->htmlOptions['id']))
  48. $this->htmlOptions['id']=$this->getId();
  49. if($this->promptText!==null)
  50. $this->htmlOptions['prompt']=$this->promptText;
  51. if(!isset($this->htmlOptions['onchange']))
  52. $this->htmlOptions['onchange']="if(this.value!='') {window.location=this.value;};";
  53. }
  54. /**
  55. * Executes the widget.
  56. * This overrides the parent implementation by displaying the generated page buttons.
  57. */
  58. public function run()
  59. {
  60. if(($pageCount=$this->getPageCount())<=1)
  61. return;
  62. $pages=array();
  63. for($i=0;$i<$pageCount;++$i)
  64. $pages[$this->createPageUrl($i)]=$this->generatePageText($i);
  65. $selection=$this->createPageUrl($this->getCurrentPage());
  66. echo $this->header;
  67. echo CHtml::dropDownList($this->getId(),$selection,$pages,$this->htmlOptions);
  68. echo $this->footer;
  69. }
  70. /**
  71. * Generates the list option for the specified page number.
  72. * You may override this method to customize the option display.
  73. * @param integer $page zero-based page number
  74. * @return string the list option for the page number
  75. */
  76. protected function generatePageText($page)
  77. {
  78. if($this->pageTextFormat!==null)
  79. return sprintf($this->pageTextFormat,$page+1);
  80. else
  81. return $page+1;
  82. }
  83. }