-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathLinkPageSizer.php
More file actions
107 lines (95 loc) · 3.48 KB
/
Copy pathLinkPageSizer.php
File metadata and controls
107 lines (95 loc) · 3.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
<?php
namespace nkovacs\pagesizer;
use Yii;
use yii\base\InvalidConfigException;
use yii\helpers\Html;
use yii\base\Widget;
use yii\data\Pagination;
/**
* LinkPageSizer displays a list of hyperlinks that lead to different page sizes of a target.
*
* LinkPageSizer works with a [[Pagination]] object which specifies the total number
* of pages and the current page number.
*
* Note that LinkPageSizer only generates the necessary HTML markups. In order for it
* to look like a real pager, you should provide some CSS styles for it.
* With the default configuration, LinkPageSizer should look good using Twitter Bootstrap CSS framework.
*/
class LinkPageSizer extends Widget
{
/**
* @var Pagination the pagination object that this pager is associated with.
* You must set this property in order to make LinkPageSizer work.
*/
public $pagination;
/**
* @var array available page sizes. Array keys are sizes, values are labels.
*/
public $availableSizes = [10 => '10', 20 => '20', 50 => '50'];
/**
* @var array HTML attributes for the pager container tag.
* @see \yii\helpers\Html::renderTagAttributes() for details on how attributes are being rendered.
*/
public $options = ['class' => 'pagination'];
/**
* @var array HTML attributes for the link in a pager container tag.
* @see \yii\helpers\Html::renderTagAttributes() for details on how attributes are being rendered.
*/
public $linkOptions = [];
/**
* @var string the CSS class for the active (currently selected) page size button.
*/
public $activePageSizeCssClass = 'active';
/**
* 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 size buttons.
*/
public function run()
{
echo $this->renderPageSizeButtons();
}
/**
* Renders the page buttons.
* @return string the rendering result
*/
protected function renderPageSizeButtons()
{
if (count($this->availableSizes) === 0) {
return '';
}
$buttons = [];
$currentPageSize = $this->pagination->getPageSize();
foreach ($this->availableSizes as $size => $label) {
$buttons[]=$this->renderPageSizeButton($label, $size, null, $size==$currentPageSize);
}
return Html::tag('ul', implode("\n", $buttons), $this->options);
}
/**
* Renders a page size button.
* You may override this method to customize the generation of page size buttons.
* @param string $label the text label for the button
* @param integer $pageSize the page size
* @param string $class the CSS class for the page button.
* @param boolean $active whether this page button is active
* @return string the rendering result
*/
protected function renderPageSizeButton($label, $pageSize, $class, $active)
{
$options = ['class' => $class === '' ? null : $class];
if ($active) {
Html::addCssClass($options, $this->activePageSizeCssClass);
}
$linkOptions = $this->linkOptions;
$linkOptions['data-page-size'] = $pageSize;
return Html::tag('li', Html::a($label, PageSize::createSizeUrl($this->pagination, $pageSize), $linkOptions), $options);
}
}