Commit f2a3dafb by Qiang Xue

Added `Pagination::getLinks()`

parent 6659919a
...@@ -145,6 +145,7 @@ Yii Framework 2 Change Log ...@@ -145,6 +145,7 @@ Yii Framework 2 Change Log
- Enh: Added `yii\web\Response::clearOutputBuffers()` (qiangxue) - Enh: Added `yii\web\Response::clearOutputBuffers()` (qiangxue)
- Enh: Improved `QueryBuilder::buildLimit()` to support big numbers (qiangxue) - Enh: Improved `QueryBuilder::buildLimit()` to support big numbers (qiangxue)
- Enh: Added support for building SQLs with sub-queries (qiangxue) - Enh: Added support for building SQLs with sub-queries (qiangxue)
- Enh: Added `Pagination::getLinks()` (qiangxue)
- Chg #1186: Changed `Sort` to use comma to separate multiple sort fields and use negative sign to indicate descending sort (qiangxue) - Chg #1186: Changed `Sort` to use comma to separate multiple sort fields and use negative sign to indicate descending sort (qiangxue)
- Chg #1519: `yii\web\User::loginRequired()` now returns the `Response` object instead of exiting the application (qiangxue) - Chg #1519: `yii\web\User::loginRequired()` now returns the `Response` object instead of exiting the application (qiangxue)
- Chg #1586: `QueryBuilder::buildLikeCondition()` will now escape special characters and use percentage characters by default (qiangxue) - Chg #1586: `QueryBuilder::buildLikeCondition()` will now escape special characters and use percentage characters by default (qiangxue)
......
...@@ -67,6 +67,12 @@ use yii\web\Request; ...@@ -67,6 +67,12 @@ use yii\web\Request;
*/ */
class Pagination extends Object class Pagination extends Object
{ {
const LINK_SELF = 'self';
const LINK_NEXT = 'next';
const LINK_PREV = 'prev';
const LINK_FIRST = 'first';
const LINK_LAST = 'last';
/** /**
* @var string name of the parameter storing the current page index. Defaults to 'page'. * @var string name of the parameter storing the current page index. Defaults to 'page'.
* @see params * @see params
...@@ -217,4 +223,28 @@ class Pagination extends Object ...@@ -217,4 +223,28 @@ class Pagination extends Object
{ {
return $this->pageSize < 1 ? -1 : $this->pageSize; return $this->pageSize < 1 ? -1 : $this->pageSize;
} }
/**
* Returns a whole set of links for navigating to the first, last, next and previous pages.
* @param boolean $absolute whether the generated URLs should be absolute.
* @return array the links for navigational purpose. The array keys specify the purpose of the links (e.g. [[LINK_FIRST]]),
* and the array values are the corresponding URLs.
*/
public function getLinks($absolute = false)
{
$currentPage = $this->getPage();
$pageCount = $this->getPageCount();
$links = [
self::LINK_SELF => $this->createUrl($currentPage, $absolute),
];
if ($currentPage > 0) {
$links[self::LINK_FIRST] = $this->createUrl(0, $absolute);
$links[self::LINK_PREV] = $this->createUrl($currentPage - 1, $absolute);
}
if ($currentPage < $pageCount - 1) {
$links[self::LINK_NEXT] = $this->createUrl($currentPage + 1, $absolute);
$links[self::LINK_LAST] = $this->createUrl($pageCount - 1, $absolute);
}
return $links;
}
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment