PHP Class yii\data\Pagination

When data needs to be rendered in multiple pages, Pagination can be used to represent information such as [[totalCount|total item count]], [[pageSize|page size]], [[page|current page]], etc. These information can be passed to [[yii\widgets\Pager|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: ~~~ 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) { display $model here } display pagination echo LinkPager::widget([ 'pagination' => $pages, ]); ~~~
Since: 2.0
Author: Qiang Xue ([email protected])
Inheritance: extends yii\base\Object
显示文件 Open project: yiisoft/yii2 Class Usage Examples

Public Properties

Property Type Description
$defaultPageSize the default page size. This property will be returned by [[pageSize]] when page size cannot be determined by [[pageSizeParam]] from [[params]].
$forcePageParam whether to always have the page parameter in the URL created by Pagination::createUrl. If false and [[page]] is 0, the page parameter will not be put in the URL.
$pageParam name of the parameter storing the current page index.
$pageSizeLimit the page size limits. The first array element stands for the minimal page size, and the second the maximal page size. If this is false, it means [[pageSize]] should always return the value of [[defaultPageSize]].
$pageSizeParam name of the parameter storing the page size.
$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]]).
$route the route of the controller action for displaying the paged contents. If not set, it means using the currently requested route.
$totalCount total number of items.
$urlManager the URL manager used for creating pagination URLs. If not set, the "urlManager" application component will be used.
$validatePage 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]].

Public Methods

Method Description
createUrl ( integer $page, integer $pageSize = null, boolean $absolute = false ) : string Creates the URL suitable for pagination with the specified page number.
getLimit ( ) : integer
getLinks ( boolean $absolute = false ) : array Returns a whole set of links for navigating to the first, last, next and previous pages.
getOffset ( ) : integer
getPage ( boolean $recalculate = false ) : integer Returns the zero-based current page number.
getPageCount ( ) : integer
getPageSize ( ) : integer Returns the number of items per page.
setPage ( integer $value, boolean $validatePage = false ) Sets the current page number.
setPageSize ( integer $value, boolean $validatePageSize = false )

Protected Methods

Method Description
getQueryParam ( string $name, string $defaultValue = null ) : string Returns the value of the specified query parameter.

Method Details

createUrl() public method

This method is mainly called by pagers when creating URLs used to perform pagination.
See also: params
See also: forcePageParam
public createUrl ( integer $page, integer $pageSize = null, boolean $absolute = false ) : string
$page integer the zero-based page number that the URL should point to.
$pageSize integer the number of items on each page. If not set, the value of [[pageSize]] will be used.
$absolute boolean whether to create an absolute URL. Defaults to `false`.
return string the created URL

getLimit() public method

public getLimit ( ) : integer
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.

getOffset() public method

public getOffset ( ) : integer
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.

getPage() public method

Returns the zero-based current page number.
public getPage ( boolean $recalculate = false ) : integer
$recalculate boolean whether to recalculate the current page based on the page size and item count.
return integer the zero-based current page number.

getPageCount() public method

public getPageCount ( ) : integer
return integer number of pages

getPageSize() public method

By default, this method will try to determine the page size by [[pageSizeParam]] in [[params]]. If the page size cannot be determined this way, [[defaultPageSize]] will be returned.
See also: pageSizeLimit
public getPageSize ( ) : integer
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.

getQueryParam() protected method

This method returns the named parameter value from [[params]]. Null is returned if the value does not exist.
protected getQueryParam ( string $name, string $defaultValue = null ) : string
$name string the parameter name
$defaultValue string the value to be returned when the specified parameter does not exist in [[params]].
return string the parameter value

setPage() public method

Sets the current page number.
public setPage ( integer $value, boolean $validatePage = false )
$value integer the zero-based index of the current page.
$validatePage boolean whether to validate the page number. Note that in order to validate the page number, both [[validatePage]] and this parameter must be true.

setPageSize() public method

public setPageSize ( integer $value, boolean $validatePageSize = false )
$value integer the number of items per page.
$validatePageSize boolean whether to validate page size.

Property Details

$defaultPageSize public_oe property

the default page size. This property will be returned by [[pageSize]] when page size cannot be determined by [[pageSizeParam]] from [[params]].
public $defaultPageSize

$forcePageParam public_oe property

whether to always have the page parameter in the URL created by Pagination::createUrl. If false and [[page]] is 0, the page parameter will not be put in the URL.
public $forcePageParam

$pageParam public_oe property

name of the parameter storing the current page index.
See also: params
public $pageParam

$pageSizeLimit public_oe property

the page size limits. The first array element stands for the minimal page size, and the second the maximal page size. If this is false, it means [[pageSize]] should always return the value of [[defaultPageSize]].
public $pageSizeLimit

$pageSizeParam public_oe property

name of the parameter storing the page size.
See also: params
public $pageSizeParam

$params public_oe property

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]]).
public $params

$route public_oe property

the route of the controller action for displaying the paged contents. If not set, it means using the currently requested route.
public $route

$totalCount public_oe property

total number of items.
public $totalCount

$urlManager public_oe property

the URL manager used for creating pagination URLs. If not set, the "urlManager" application component will be used.
public $urlManager

$validatePage public_oe property

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]].
public $validatePage