CPagination
| Package |
system.web |
| Inheritance |
class CPagination »
CComponent |
| Since |
1.0 |
| Version |
$Id: CPagination.php 2236 2010-07-01 11:54:23Z alexander.makarow $ |
CPagination represents information relevant to pagination.
When data needs to be rendered in multiple pages, we can use CPagination 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.
Example:
Controller action:
function actionIndex(){
$criteria = new CDbCriteria();
$count=Article::model()->count($criteria);
$pages=new CPagination($count);
// results per page
$pages->pageSize=10;
$pages->applyLimit($criteria);
$models = Post::model()->findAll($criteria);
$this->render('index', array(
'models' => $models,
'pages' => $pages
));
}
View:
// display a model
// display pagination
$this->widget('CLinkPager', array(
'pages' => $pages,
))?>
Public Properties
Hide inherited properties
| Property | Type | Description | Defined By |
| currentPage |
integer |
the zero-based index of the current page. |
CPagination |
| itemCount |
integer |
total number of items. |
CPagination |
| limit |
integer |
the limit of the data. |
CPagination |
| offset |
integer |
the offset of the data. |
CPagination |
| pageCount |
integer |
number of pages |
CPagination |
| pageSize |
integer |
number of items in each page. |
CPagination |
| pageVar |
string |
name of the GET variable storing the current page index. |
CPagination |
| params |
array |
the additional GET parameters (name=>value) that should be used when generating pagination URLs. |
CPagination |
| route |
string |
the route (controller ID and action ID) for displaying the paged contents. |
CPagination |
Property Details
the zero-based index of the current page. Defaults to 0.
total number of items. Defaults to 0.
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.
This returns the same value as pageSize.
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.
number of pages
number of items in each page. Defaults to 10.
public string $pageVar;
name of the GET variable storing the current page index. Defaults to 'page'.
public array $params;
the additional GET parameters (name=>value) that should be used when generating pagination URLs.
Defaults to null, meaning using the currently available GET parameters.
public string $route;
the route (controller ID and action ID) for displaying the paged contents.
Defaults to empty string, meaning using the current route.
Method Details
|
public void __construct(integer $itemCount=0)
|
| $itemCount |
integer |
total number of items. |
Constructor.
|
|
| $criteria |
CDbCriteria |
the query criteria that should be applied with the limit |
Applies LIMIT and OFFSET to the specified query criteria.
public string createPageUrl( CController $controller, integer $page)
|
| $controller |
CController |
the controller that will create the actual URL |
| $page |
integer |
the page that the URL should point to. This is a zero-based index. |
| {return} |
string |
the created URL |
Creates the URL suitable for pagination.
This method is mainly called by pagers when creating URLs used to
perform pagination. The default implementation is to call
the controller's createUrl method with the page information.
You may override this method if your URL scheme is not the same as
the one supported by the controller's createUrl method.
|
public integer getCurrentPage(boolean $recalculate=true)
|
| $recalculate |
boolean |
whether to recalculate the current page based on the page size and item count. |
| {return} |
integer |
the zero-based index of the current page. Defaults to 0. |
|
public integer getItemCount()
|
| {return} |
integer |
total number of items. Defaults to 0. |
|
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.
This returns the same value as pageSize. |
|
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 integer getPageCount()
|
| {return} |
integer |
number of pages |
|
public integer getPageSize()
|
| {return} |
integer |
number of items in each page. Defaults to 10. |
|
public void setCurrentPage(integer $value)
|
| $value |
integer |
the zero-based index of the current page. |
|
public void setItemCount(integer $value)
|
| $value |
integer |
total number of items. |
|
public void setPageSize(integer $value)
|
| $value |
integer |
number of items in each page |
There can be a problem with the CListView, CPagination default functionality. If you page forward to say page three, then look at a detail view from that page (i.e. drill down), the average user would expect the browser back button to return them to page three. However, because the pagination has been handled by Ajax they are taken to page one. This is not al all user friendly. The simple solution is to disbale Ajax by adding to the CListView the line: 'ajaxUpdate'=>false, Are there better solutions to this?