没有命名空间的类 yii yii\base yii\behaviors yii\caching yii\captcha yii\console yii\console\controllers yii\console\widgets yii\data yii\db yii\db\conditions yii\db\cubrid yii\db\cubrid\conditions yii\db\mssql yii\db\mssql\conditions yii\db\mysql yii\db\oci yii\db\oci\conditions yii\db\pgsql yii\db\sqlite yii\db\sqlite\conditions yii\di yii\filters yii\filters\auth yii\grid yii\helpers yii\i18n yii\log yii\mail yii\mutex yii\rbac yii\rest yii\test yii\validators yii\web yii\widgets

Class yii\data\Pagination

继承yii\data\Pagination » yii\base\BaseObject
实现yii\base\Configurable, yii\web\Linkable
可用版本自2.0
源码 https://github.com/yiichina/yii2/blob/api/framework/data/Pagination.php

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) {
    // display $model here
}

// display pagination
echo LinkPager::widget([
    'pagination' => $pages,
]);

For more details and usage information on Pagination, see the guide article on pagination.

公共属性

隐藏继承的属性

属性类型描述被定义在
$defaultPageSize integer The default page size. yii\data\Pagination
$forcePageParam boolean Whether to always have the page parameter in the URL created by createUrl(). yii\data\Pagination
$limit integer The limit of the data. yii\data\Pagination
$offset integer The offset of the data. yii\data\Pagination
$page integer The zero-based current page number. yii\data\Pagination
$pageCount integer Number of pages yii\data\Pagination
$pageParam string Name of the parameter storing the current page index. yii\data\Pagination
$pageSize integer The number of items per page. yii\data\Pagination
$pageSizeLimit array|false The page size limits. yii\data\Pagination
$pageSizeParam string Name of the parameter storing the page size. yii\data\Pagination
$params array Parameters (name => value) that should be used to obtain the current page number and to create new pagination URLs. yii\data\Pagination
$route string The route of the controller action for displaying the paged contents. yii\data\Pagination
$totalCount integer Total number of items. yii\data\Pagination
$urlManager yii\web\UrlManager The URL manager used for creating pagination URLs. yii\data\Pagination
$validatePage boolean Whether to check if $page is within valid range. yii\data\Pagination

公共方法

隐藏继承的方法

方法描述被定义在
__call() Calls the named method which is not a class method. yii\base\BaseObject
__construct() Constructor. yii\base\BaseObject
__get() Returns the value of an object property. yii\base\BaseObject
__isset() Checks if a property is set, i.e. defined and not null. yii\base\BaseObject
__set() Sets value of an object property. yii\base\BaseObject
__unset() Sets an object property to null. yii\base\BaseObject
canGetProperty() Returns a value indicating whether a property can be read. yii\base\BaseObject
canSetProperty() Returns a value indicating whether a property can be set. yii\base\BaseObject
className() Returns the fully qualified name of this class. yii\base\BaseObject
createUrl() Creates the URL suitable for pagination with the specified page number. yii\data\Pagination
getLimit() yii\data\Pagination
getLinks() Returns a whole set of links for navigating to the first, last, next and previous pages. yii\data\Pagination
getOffset() yii\data\Pagination
getPage() Returns the zero-based current page number. yii\data\Pagination
getPageCount() yii\data\Pagination
getPageSize() Returns the number of items per page. yii\data\Pagination
hasMethod() Returns a value indicating whether a method is defined. yii\base\BaseObject
hasProperty() Returns a value indicating whether a property is defined. yii\base\BaseObject
init() Initializes the object. yii\base\BaseObject
setPage() Sets the current page number. yii\data\Pagination
setPageSize() yii\data\Pagination

受保护的方法

隐藏继承的方法

方法描述被定义在
getQueryParam() Returns the value of the specified query parameter. yii\data\Pagination

常量

隐藏继承的常量

常量描述被定义在

属性详情

$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 createUrl(). If false and $page is 0, the page parameter will not be put in the URL.

public boolean $forcePageParam true
$limit 公共 只读 属性

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

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 array getLinks ( $absolute false )
$offset 公共 只读 属性

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 integer getOffset ( )
$page 公共 属性

The zero-based current page number.

public integer getPage ( $recalculate false )
public void setPage ( $value, $validatePage false )
$pageCount 公共 只读 属性

Number of pages

$pageParam 公共 属性

Name of the parameter storing the current page index.

参见 $params.

public string $pageParam 'page'
$pageSize 公共 属性

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 integer getPageSize ( )
public void setPageSize ( $value, $validatePageSize false )
$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 array|false $pageSizeLimit = [150]
$pageSizeParam 公共 属性

Name of the parameter storing the page size.

参见 $params.

public string $pageSizeParam 'per-page'
$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 array $params null
$route 公共 属性

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

public string $route null
$totalCount 公共 属性

Total number of items.

public integer $totalCount 0
$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 boolean $validatePage true

方法详情

createUrl() 公共 方法

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.

参见:

public string createUrl($page, $pageSize null, $absolute false)
$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 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.

getLinks() 公共 方法

Returns a whole set of links for navigating to the first, last, next and previous pages.

public array getLinks($absolute false)
$absolute boolean

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.

getOffset() 公共 方法

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.

getPage() 公共 方法

Returns the zero-based current page number.

public integer getPage($recalculate false)
$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 integer getPageCount()
return integer

Number of pages

getPageSize() 公共 方法

Returns the number of items per page.

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 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.

getQueryParam() 受保护 方法

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 string getQueryParam($name, $defaultValue null)
$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() 公共 方法

Sets the current page number.

public void setPage($value, $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 void setPageSize($value, $validatePageSize false)
$value integer

The number of items per page.

$validatePageSize boolean

Whether to validate page size.