Skip to content

Commit 23d0cf8

Browse files
committed
Add pageSizeOptions and goToPageLabel options for LinkPager
1 parent bd19a9d commit 23d0cf8

File tree

1 file changed

+59
-0
lines changed

1 file changed

+59
-0
lines changed

src/LinkPager.php

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
namespace yii\bootstrap5;
1212

13+
use Yii;
1314
use yii\base\InvalidConfigException;
1415
use yii\data\Pagination;
1516
use yii\helpers\ArrayHelper;
@@ -148,6 +149,17 @@ class LinkPager extends Widget
148149
*/
149150
public $disableCurrentPageButton = false;
150151

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;
151163

152164
/**
153165
* Initializes the pager.
@@ -208,6 +220,8 @@ protected function renderPageButtons(): string
208220
}
209221

210222
$buttons = [];
223+
$buttons[] = $this->renderSelectPageSize();
224+
211225
$currentPage = $this->pagination->getPage();
212226

213227
// first page
@@ -274,12 +288,57 @@ protected function renderPageButtons(): string
274288
);
275289
}
276290

291+
$buttons[] = $this->renderGoToPage();
292+
277293
$options = $this->listOptions;
278294
$tag = ArrayHelper::remove($options, 'tag', 'ul');
279295

280296
return Html::tag($tag, implode("\n", $buttons), $options);
281297
}
282298

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+
283342
/**
284343
* Renders a page button.
285344
* You may override this method to customize the generation of page buttons.

0 commit comments

Comments
 (0)