Skip to content
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 59 additions & 0 deletions src/LinkPager.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

namespace yii\bootstrap5;

use Yii;
use yii\base\InvalidConfigException;
use yii\data\Pagination;
use yii\helpers\ArrayHelper;
Expand Down Expand Up @@ -148,6 +149,17 @@ class LinkPager extends Widget
*/
public $disableCurrentPageButton = false;

/**
* @var array Number of records displayed per page. If the array is empty, this area will not be displayed.
* e.g. [5=>5, 10 => 10, 20 => 20, 50 => 50]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* e.g. [5=>5, 10 => 10, 20 => 20, 50 => 50]
* e.g. [5 => 5, 10 => 10, 20 => 20, 50 => 50]

What's the meaning of keys/values?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@samdark I've updated the comments

*/
public $pageSizeOptions = [];

/**
* @var string|bool the text label for the "goto" page area. Note that this will NOT be HTML-encoded.
* If this property is false, the "goto" page area will not be displayed.
*/
public $goToPageLabel = false;

/**
* Initializes the pager.
Expand Down Expand Up @@ -208,6 +220,8 @@ protected function renderPageButtons(): string
}

$buttons = [];
$buttons[] = $this->renderSelectPageSize();

$currentPage = $this->pagination->getPage();

// first page
Expand Down Expand Up @@ -274,12 +288,57 @@ protected function renderPageButtons(): string
);
}

$buttons[] = $this->renderGoToPage();

$options = $this->listOptions;
$tag = ArrayHelper::remove($options, 'tag', 'ul');

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

protected function renderSelectPageSize(): string
{
$html = '';
if (!empty($this->pageSizeOptions)) {
$html .= '<li class="me-2">';
$html .= Html::dropDownList(
$this->pagination->pageSizeParam,
$this->pagination->getPageSize(),
$this->pageSizeOptions,
[
'class' => 'form-select',
'onchange' => "location.href='" . $this->pagination->createUrl(0) . "&" . $this->pagination->pageSizeParam . "=' + this.value;",
],
);
$html .= '</li>';
}
return $html;
}

protected function renderGoToPage(): string
{
$currentPage = $this->pagination->getPage();
$totalPages = $this->pagination->getPageCount();

$html = '';
if ($this->goToPageLabel !== false) {
$text = $this->goToPageLabel === true ? Yii::t('yii', 'View') : $this->goToPageLabel;
$html .= '<li class="d-flex ms-2">';
$html .= Html::input('number', 'page', $currentPage + 1, [
'class' => 'form-control form-control-sm',
'min' => 1,
'max' => $totalPages,
'id' => 'custom-page-input',
]);
$html .= Html::button($text, [
'class' => 'form-control btn btn-outline-secondary',
'onclick' => "location.href='" . $this->pagination->createUrl(0) . "&" . $this->pagination->pageParam . "=' + (this.parentElement.querySelector('input').value);",
]);
$html .= '</li>';
}
return $html;
}

/**
* Renders a page button.
* You may override this method to customize the generation of page buttons.
Expand Down
Loading