PHP Класс 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, ]); ~~~
С версии: 2.0
Автор: Qiang Xue ([email protected])
Наследование: extends yii\base\Object
Показать файл Открыть проект Примеры использования класса

Открытые свойства

Свойство Тип Описание
$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]].

Открытые методы

Метод Описание
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 )

Защищенные методы

Метод Описание
getQueryParam ( string $name, string $defaultValue = null ) : string Returns the value of the specified query parameter.

Описание методов

createUrl() публичный Метод

This method is mainly called by pagers when creating URLs used to perform pagination.
См. также: params
См. также: 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`.
Результат string the created URL

getLimit() публичный Метод

public getLimit ( ) : integer
Результат 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 getOffset ( ) : integer
Результат 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() публичный Метод

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.
Результат integer the zero-based current page number.

getPageCount() публичный Метод

public getPageCount ( ) : integer
Результат integer number of pages

getPageSize() публичный Метод

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.
См. также: pageSizeLimit
public getPageSize ( ) : integer
Результат 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() защищенный Метод

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]].
Результат string the parameter value

setPage() публичный Метод

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 setPageSize ( integer $value, boolean $validatePageSize = false )
$value integer the number of items per page.
$validatePageSize boolean whether to validate page size.

Описание свойств

$defaultPageSize публичное свойство

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

$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.
public $forcePageParam

$pageParam публичное свойство

name of the parameter storing the current page index.
См. также: params
public $pageParam

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

$pageSizeParam публичное свойство

name of the parameter storing the page size.
См. также: params
public $pageSizeParam

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

$route публичное свойство

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

$totalCount публичное свойство

total number of items.
public $totalCount

$urlManager публичное свойство

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

$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 $validatePage