|
10 | 10 |
|
11 | 11 | namespace yii\bootstrap5; |
12 | 12 |
|
| 13 | +use Yii; |
13 | 14 | use yii\base\InvalidConfigException; |
14 | 15 | use yii\data\Pagination; |
15 | 16 | use yii\helpers\ArrayHelper; |
@@ -148,6 +149,17 @@ class LinkPager extends Widget |
148 | 149 | */ |
149 | 150 | public $disableCurrentPageButton = false; |
150 | 151 |
|
| 152 | + /** |
| 153 | + * @var array Number of records displayed per page. If the array is empty, this area will not be displayed. |
| 154 | + * e.g. [5=>5, 10 => 10, 20 => 20, 50 => 50] |
| 155 | + */ |
| 156 | + public $pageSizeOptions = []; |
| 157 | + |
| 158 | + /** |
| 159 | + * @var string|bool the text label for the "goto" page area. Note that this will NOT be HTML-encoded. |
| 160 | + * If this property is false, the "goto" page area will not be displayed. |
| 161 | + */ |
| 162 | + public $goToPageLabel = false; |
151 | 163 |
|
152 | 164 | /** |
153 | 165 | * Initializes the pager. |
@@ -208,6 +220,8 @@ protected function renderPageButtons(): string |
208 | 220 | } |
209 | 221 |
|
210 | 222 | $buttons = []; |
| 223 | + $buttons[] = $this->renderSelectPageSize(); |
| 224 | + |
211 | 225 | $currentPage = $this->pagination->getPage(); |
212 | 226 |
|
213 | 227 | // first page |
@@ -274,12 +288,57 @@ protected function renderPageButtons(): string |
274 | 288 | ); |
275 | 289 | } |
276 | 290 |
|
| 291 | + $buttons[] = $this->renderGoToPage(); |
| 292 | + |
277 | 293 | $options = $this->listOptions; |
278 | 294 | $tag = ArrayHelper::remove($options, 'tag', 'ul'); |
279 | 295 |
|
280 | 296 | return Html::tag($tag, implode("\n", $buttons), $options); |
281 | 297 | } |
282 | 298 |
|
| 299 | + protected function renderSelectPageSize(): string |
| 300 | + { |
| 301 | + $html = ''; |
| 302 | + if (!empty($this->pageSizeOptions)) { |
| 303 | + $html .= '<li class="me-2">'; |
| 304 | + $html .= Html::dropDownList( |
| 305 | + $this->pagination->pageSizeParam, |
| 306 | + $this->pagination->getPageSize(), |
| 307 | + $this->pageSizeOptions, |
| 308 | + [ |
| 309 | + 'class' => 'form-select', |
| 310 | + 'onchange' => "location.href='" . $this->pagination->createUrl(0) . "&" . $this->pagination->pageSizeParam . "=' + this.value;", |
| 311 | + ] |
| 312 | + ); |
| 313 | + $html .= '</li>'; |
| 314 | + } |
| 315 | + return $html; |
| 316 | + } |
| 317 | + |
| 318 | + protected function renderGoToPage(): string |
| 319 | + { |
| 320 | + $currentPage = $this->pagination->getPage(); |
| 321 | + $totalPages = $this->pagination->getPageCount(); |
| 322 | + |
| 323 | + $html = ''; |
| 324 | + if ($this->goToPageLabel !== false) { |
| 325 | + $text = $this->goToPageLabel === true ? Yii::t('yii', 'View') : $this->goToPageLabel; |
| 326 | + $html .= '<li class="d-flex ms-2">'; |
| 327 | + $html .= Html::input('number', 'page', $currentPage + 1, [ |
| 328 | + 'class' => 'form-control form-control-sm', |
| 329 | + 'min' => 1, |
| 330 | + 'max' => $totalPages, |
| 331 | + 'id' => 'custom-page-input', |
| 332 | + ]); |
| 333 | + $html .= Html::button($text, [ |
| 334 | + 'class' => 'form-control btn btn-outline-secondary', |
| 335 | + 'onclick' => "location.href='" . $this->pagination->createUrl(0) . "&" . $this->pagination->pageParam . "=' + (this.parentElement.querySelector('input').value);", |
| 336 | + ]); |
| 337 | + $html .= '</li>'; |
| 338 | + } |
| 339 | + return $html; |
| 340 | + } |
| 341 | + |
283 | 342 | /** |
284 | 343 | * Renders a page button. |
285 | 344 | * You may override this method to customize the generation of page buttons. |
|
0 commit comments