ListPager.php 2.71 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12
<?php
/**
 * @link http://www.yiiframework.com/
 * @copyright Copyright (c) 2008 Yii Software LLC
 * @license http://www.yiiframework.com/license/
 */

namespace yii\widgets;

use yii\base\InvalidConfigException;
use yii\helpers\Html;
use yii\base\Widget;
13
use yii\data\Pagination;
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65

/**
 * ListPager displays a drop-down list that contains options leading to different pages.
 *
 * ListPager works with a [[Pagination]] object which specifies the totally number
 * of pages and the current page number.
 *
 * Note that ListPager requires JavaScript to work. You should consider using [[LinkPager]]
 * if you want to make your page work without JavaScript.
 *
 * @author Qiang Xue <qiang.xue@gmail.com>
 * @since 2.0
 */
class ListPager extends Widget
{
	/**
	 * @var Pagination the pagination object that this pager is associated with.
	 * You must set this property in order to make ListPager work.
	 */
	public $pagination;
	/**
	 * @var array HTML attributes for the drop-down list tag. The following options are specially handled:
	 *
	 * - prompt: string, a prompt text to be displayed as the first option.
	 *
	 * The rest of the options will be rendered as the attributes of the resulting tag. The values will
	 * be HTML-encoded using [[encode()]]. If a value is null, the corresponding attribute will not be rendered.
	 */
	public $options = array();
	/**
	 * @var string the template used to render the label for each list option.
	 * The token "{page}" will be replaced with the actual page number (1-based).
	 */
	public $template = '{page}';


	/**
	 * Initializes the pager.
	 */
	public function init()
	{
		if ($this->pagination === null) {
			throw new InvalidConfigException('The "pagination" property must be set.');
		}
	}

	/**
	 * Executes the widget.
	 * This overrides the parent implementation by displaying the generated page buttons.
	 */
	public function run()
	{
66
		$pageCount = $this->pagination->getPageCount();
67 68 69 70 71 72 73 74 75
		$currentPage = $this->pagination->getPage();

		$pages = array();
		for ($i = 0; $i < $pageCount; ++$i) {
			$pages[$this->pagination->createUrl($i)] = $this->generatePageText($i);
		}
		$selection = $this->pagination->createUrl($currentPage);

		if (!isset($this->options['onchange'])) {
resurtm committed
76
			$this->options['onchange'] = "if (this.value != '') { window.location = this.value; };";
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
		}

		echo Html::dropDownList(null, $selection, $pages, $this->options);
	}

	/**
	 * Generates the label of the list option for the specified page number.
	 * You may override this method to customize the option display.
	 * @param integer $page zero-based page number
	 * @return string the list option for the page number
	 */
	protected function generatePageText($page)
	{
		return strtr($this->template, array(
			'{page}' => $page + 1,
		));
	}
94
}