M
M
Maxim2016-06-15 16:39:10
Yii
Maxim, 2016-06-15 16:39:10

How to make automatic adding buttons first page, last page in pagination?

Tables are displayed on the pages through GridView::widget, so that everyone does not prescribe

'pager' => [
        'firstPageLabel' => 'First',
        'lastPageLabel' => 'Last',
    ],

I want to automate this somehow. I found the getLinks method in Pagination.php, it just says what you need
public function getLinks($absolute = false)
{
    $currentPage = $this->getPage();
    $pageCount = $this->getPageCount();
    $links = [
        Link::REL_SELF => $this->createUrl($currentPage, null, $absolute),
    ];
    if ($currentPage > 0) {
        $links[self::LINK_FIRST] = $this->createUrl(0, null, $absolute);
        $links[self::LINK_PREV] = $this->createUrl($currentPage - 1, null, $absolute);
    }
    if ($currentPage < $pageCount - 1) {
        $links[self::LINK_NEXT] = $this->createUrl($currentPage + 1, null, $absolute);
        $links[self::LINK_LAST] = $this->createUrl($pageCount - 1, null, $absolute);
    }

    return $links;
}

But how and where is it correct to call this method?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Maxim Fedorov, 2016-06-15
@KidsBout

no need to call any methods because it will be a crutch solution.
In a LinkPager (which uses a GridView), by default the firstPageLabel and lastPageLabel properties are set to false - i.e. do not display these buttons.
If you want to get rid of code duplication, you can simply create your own GridView class that inherits from the standard one and change the default values ​​​​of the corresponding properties in it.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question