Pagination represents information relevant to pagination of data items.
When data needs to be rendered in multiple pages, Pagination can be used to represent information such as total item count, page size, current page, etc. These information can be passed to pagers to render pagination buttons or links.
The following example shows how to create a pagination object and feed it to a pager.
Controller action:
public function actionIndex()
{
$query = Article::find()->where(['status' => 1]);
$countQuery = clone $query;
$pages = new Pagination(['totalCount' => $countQuery->count()]);
$models = $query->offset($pages->offset)
->limit($pages->limit)
->all();
return $this->render('index', [
'models' => $models,
'pages' => $pages,
]);
}
View:
foreach ($models as $model) {
}
echo LinkPager::widget([
'pagination' => $pages,
]);
For more details and usage information on Pagination, see the guide article on pagination.
Property DetailsThe default page size. This property will be returned by $pageSize when page size cannot be determined by $pageSizeParam from $params.
Whether to always have the page parameter in the URL created by createUrl(). If false and $page is 0, the page parameter will not be put in the URL.
The limit of the data. This may be used to set the LIMIT value for a SQL statement for fetching the current page of data. Note that if the page size is infinite, a value -1 will be returned.
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.
The offset of the data. This may be used to set the OFFSET value for a SQL statement for fetching the current page of data.
The zero-based current page number.
Name of the parameter storing the current page index.
See also $params.
The number of items per page. If it is less than 1, it means the page size is infinite, and thus a single page contains all items.
The page size limits. The first array element defines the minimum page size, and the second the maximum page size. If this is false, it means $pageSize should always return the value of $defaultPageSize.
Name of the parameter storing the page size.
See also $params.
Parameters (name => value) that should be used to obtain the current page number and to create new pagination URLs. If not set, all parameters from $_GET will be used instead.
In order to add hash to all links use array_merge($_GET, ['#' => 'my-hash'])
.
The array element indexed by $pageParam is considered to be the current page number (defaults to 0); while the element indexed by $pageSizeParam is treated as the page size (defaults to $defaultPageSize).
The route of the controller action for displaying the paged contents. If not set, it means using the currently requested route.
The URL manager used for creating pagination URLs. If not set, the "urlManager" application component will be used.
Whether to check if $page is within valid range. When this property is true, the value of $page will always be between 0 and ($pageCount-1). Because $pageCount relies on the correct value of $totalCount which may not be available in some cases (e.g. MongoDB), you may want to set this property to be false to disable the page number validation. By doing so, $page will return the value indexed by $pageParam in $params.
Method DetailsDefined in: yii\base\BaseObject::__call()
Calls the named method which is not a class method.
Do not call this method directly as it is a PHP magic method that will be implicitly called when an unknown method is being invoked.
public function __call($name, $params)
{
throw new UnknownMethodException('Calling unknown method: ' . get_class($this) . "::$name()");
}
Defined in: yii\base\BaseObject::__construct()
Constructor.
The default implementation does two things:
$config
.If this method is overridden in a child class, it is recommended that
$config
here.Name-value pairs that will be used to initialize the object properties
public function __construct($config = [])
{
if (!empty($config)) {
Yii::configure($this, $config);
}
$this->init();
}
Defined in: yii\base\BaseObject::__get()
Returns the value of an object property.
Do not call this method directly as it is a PHP magic method that will be implicitly called when executing $value = $object->property;
.
See also __set().
public function __get($name)
{
$getter = 'get' . $name;
if (method_exists($this, $getter)) {
return $this->$getter();
} elseif (method_exists($this, 'set' . $name)) {
throw new InvalidCallException('Getting write-only property: ' . get_class($this) . '::' . $name);
}
throw new UnknownPropertyException('Getting unknown property: ' . get_class($this) . '::' . $name);
}
public boolean __isset ( $name ) $name string
The property name or the event name
return booleanWhether the named property is set (not null).
public function __isset($name)
{
$getter = 'get' . $name;
if (method_exists($this, $getter)) {
return $this->$getter() !== null;
}
return false;
}
Defined in: yii\base\BaseObject::__set()
Sets value of an object property.
Do not call this method directly as it is a PHP magic method that will be implicitly called when executing $object->property = $value;
.
See also __get().
public function __set($name, $value)
{
$setter = 'set' . $name;
if (method_exists($this, $setter)) {
$this->$setter($value);
} elseif (method_exists($this, 'get' . $name)) {
throw new InvalidCallException('Setting read-only property: ' . get_class($this) . '::' . $name);
} else {
throw new UnknownPropertyException('Setting unknown property: ' . get_class($this) . '::' . $name);
}
}
Defined in: yii\base\BaseObject::__unset()
Sets an object property to null.
Do not call this method directly as it is a PHP magic method that will be implicitly called when executing unset($object->property)
.
Note that if the property is not defined, this method will do nothing. If the property is read-only, it will throw an exception.
See also https://www.php.net/manual/en/function.unset.php.
public function __unset($name)
{
$setter = 'set' . $name;
if (method_exists($this, $setter)) {
$this->$setter(null);
} elseif (method_exists($this, 'get' . $name)) {
throw new InvalidCallException('Unsetting read-only property: ' . get_class($this) . '::' . $name);
}
}
Defined in: yii\base\BaseObject::canGetProperty()
Returns a value indicating whether a property can be read.
A property is readable if:
$checkVars
is true);See also canSetProperty().
public function canGetProperty($name, $checkVars = true)
{
return method_exists($this, 'get' . $name) || $checkVars && property_exists($this, $name);
}
Defined in: yii\base\BaseObject::canSetProperty()
Returns a value indicating whether a property can be set.
A property is writable if:
$checkVars
is true);See also canGetProperty().
public boolean canSetProperty ( $name, $checkVars = true ) $name stringThe property name
$checkVars booleanWhether to treat member variables as properties
return booleanWhether the property can be written
public function canSetProperty($name, $checkVars = true)
{
return method_exists($this, 'set' . $name) || $checkVars && property_exists($this, $name);
}
Deprecated since 2.0.14. On PHP >=5.5, use ::class
instead.
public static function className()
{
return get_called_class();
}
Creates the URL suitable for pagination with the specified page number.
This method is mainly called by pagers when creating URLs used to perform pagination.
See also:
public string createUrl ( $page, $pageSize = null, $absolute = false ) $page integerThe zero-based page number that the URL should point to.
$pageSize integer|nullThe number of items on each page. If not set, the value of $pageSize will be used.
$absolute booleanWhether to create an absolute URL. Defaults to false
.
The created URL
public function createUrl($page, $pageSize = null, $absolute = false)
{
$page = (int) $page;
$pageSize = (int) $pageSize;
if (($params = $this->params) === null) {
$request = Yii::$app->getRequest();
$params = $request instanceof Request ? $request->getQueryParams() : [];
}
if ($page > 0 || $page == 0 && $this->forcePageParam) {
$params[$this->pageParam] = $page + 1;
} else {
unset($params[$this->pageParam]);
}
if ($pageSize <= 0) {
$pageSize = $this->getPageSize();
}
if ($pageSize != $this->defaultPageSize) {
$params[$this->pageSizeParam] = $pageSize;
} else {
unset($params[$this->pageSizeParam]);
}
$params[0] = $this->route === null ? Yii::$app->controller->getRoute() : $this->route;
$urlManager = $this->urlManager === null ? Yii::$app->getUrlManager() : $this->urlManager;
if ($absolute) {
return $urlManager->createAbsoluteUrl($params);
}
return $urlManager->createUrl($params);
}
public integer getLimit ( ) return integer
The limit of the data. This may be used to set the LIMIT value for a SQL statement for fetching the current page of data. Note that if the page size is infinite, a value -1 will be returned.
public function getLimit()
{
$pageSize = $this->getPageSize();
return $pageSize < 1 ? -1 : $pageSize;
}
Returns a whole set of links for navigating to the first, last, next and previous pages.
public array getLinks ( $absolute = false ) $absolute booleanWhether the generated URLs should be absolute.
return arrayThe 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 = [Link::REL_SELF => $this->createUrl($currentPage, null, $absolute)];
if ($pageCount > 0) {
$links[self::LINK_FIRST] = $this->createUrl(0, null, $absolute);
$links[self::LINK_LAST] = $this->createUrl($pageCount - 1, null, $absolute);
if ($currentPage > 0) {
$links[self::LINK_PREV] = $this->createUrl($currentPage - 1, null, $absolute);
}
if ($currentPage < $pageCount - 1) {
$links[self::LINK_NEXT] = $this->createUrl($currentPage + 1, null, $absolute);
}
}
return $links;
}
public integer getOffset ( ) return integer
The offset of the data. This may be used to set the OFFSET value for a SQL statement for fetching the current page of data.
public function getOffset()
{
$pageSize = $this->getPageSize();
return $pageSize < 1 ? 0 : $this->getPage() * $pageSize;
}
Returns the zero-based current page number.
public integer getPage ( $recalculate = false ) $recalculate booleanWhether to recalculate the current page based on the page size and item count.
return integerThe zero-based current page number.
public function getPage($recalculate = false)
{
if ($this->_page === null || $recalculate) {
$page = (int) $this->getQueryParam($this->pageParam, 1) - 1;
$this->setPage($page, true);
}
return $this->_page;
}
public function getPageCount()
{
$pageSize = $this->getPageSize();
if ($pageSize < 1) {
return $this->totalCount > 0 ? 1 : 0;
}
$totalCount = $this->totalCount < 0 ? 0 : (int) $this->totalCount;
return (int) (($totalCount + $pageSize - 1) / $pageSize);
}
public integer getPageSize ( ) return integer
The number of items per page. If it is less than 1, it means the page size is infinite, and thus a single page contains all items.
public function getPageSize()
{
if ($this->_pageSize === null) {
if (empty($this->pageSizeLimit) || !isset($this->pageSizeLimit[0], $this->pageSizeLimit[1])) {
$pageSize = $this->defaultPageSize;
$this->setPageSize($pageSize);
} else {
$pageSize = (int) $this->getQueryParam($this->pageSizeParam, $this->defaultPageSize);
$this->setPageSize($pageSize, true);
}
}
return $this->_pageSize;
}
Returns the value of the specified query parameter.
This method returns the named parameter value from $params. Null is returned if the value does not exist.
protected function getQueryParam($name, $defaultValue = null)
{
if (($params = $this->params) === null) {
$request = Yii::$app->getRequest();
$params = $request instanceof Request ? $request->getQueryParams() : [];
}
return isset($params[$name]) && is_scalar($params[$name]) ? $params[$name] : $defaultValue;
}
Defined in: yii\base\BaseObject::hasMethod()
Returns a value indicating whether a method is defined.
The default implementation is a call to php function method_exists()
. You may override this method when you implemented the php magic method __call()
.
public function hasMethod($name)
{
return method_exists($this, $name);
}
Defined in: yii\base\BaseObject::hasProperty()
Returns a value indicating whether a property is defined.
A property is defined if:
$checkVars
is true);See also:
public boolean hasProperty ( $name, $checkVars = true ) $name stringThe property name
$checkVars booleanWhether to treat member variables as properties
return booleanWhether the property is defined
public function hasProperty($name, $checkVars = true)
{
return $this->canGetProperty($name, $checkVars) || $this->canSetProperty($name, false);
}
Defined in: yii\base\BaseObject::init()
Initializes the object.
This method is invoked at the end of the constructor after the object is initialized with the given configuration.
public function init()
{
}
Sets the current page number.
public void setPage ( $value, $validatePage = false ) $value integerThe zero-based index of the current page.
$validatePage booleanWhether to validate the page number. Note that in order to validate the page number, both $validatePage and this parameter must be true.
public function setPage($value, $validatePage = false)
{
if ($value === null) {
$this->_page = null;
} else {
$value = (int) $value;
if ($validatePage && $this->validatePage) {
$pageCount = $this->getPageCount();
if ($value >= $pageCount) {
$value = $pageCount - 1;
}
}
if ($value < 0) {
$value = 0;
}
$this->_page = $value;
}
}
public void setPageSize ( $value, $validatePageSize = false ) $value integer
The number of items per page.
$validatePageSize booleanWhether to validate page size.
public function setPageSize($value, $validatePageSize = false)
{
if ($value === null) {
$this->_pageSize = null;
} else {
$value = (int) $value;
if ($validatePageSize && isset($this->pageSizeLimit[0], $this->pageSizeLimit[1])) {
if ($value < $this->pageSizeLimit[0]) {
$value = $this->pageSizeLimit[0];
} elseif ($value > $this->pageSizeLimit[1]) {
$value = $this->pageSizeLimit[1];
}
}
$this->_pageSize = $value;
}
}
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4