system system.base system.caching system.caching.dependencies system.collections system.console system.db system.db.ar system.db.schema system.db.schema.cubrid system.db.schema.mssql system.db.schema.mysql system.db.schema.oci system.db.schema.pgsql system.db.schema.sqlite system.gii system.i18n system.i18n.gettext system.logging system.test system.utils system.validators system.web system.web.actions system.web.auth system.web.filters system.web.form system.web.helpers system.web.renderers system.web.services system.web.widgets system.web.widgets.captcha system.web.widgets.pagers zii.behaviors zii.widgets zii.widgets.grid zii.widgets.jui

CPagination

system.web
继承 class CPagination » CComponent
可用自 1.0
源码 framework/web/CPagination.php
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=Article::model()->findAll($criteria);

    $this->render('index', array(
    'models' => $models,
         'pages' => $pages
    ));
}


View:
<?php foreach($models as $model): ?>
    // display a model
<?php endforeach; ?>

// display pagination
<?php $this->widget('CLinkPager', array(
    'pages' => $pages,
)) ?>

公共属性

隐藏继承的属性

属性类型描述被定义在
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 of parameters (name=>value) that should be used instead of GET when generating pagination URLs. CPagination
route string the route (controller ID and action ID) for displaying the paged contents. CPagination
validateCurrentPage boolean whether to ensure currentPage is returning a valid page number. CPagination

公共方法

隐藏继承的方法

方法描述被定义在
__call() Calls the named method which is not a class method. CComponent
__construct() Constructor. CPagination
__get() Returns a property value, an event handler list or a behavior based on its name. CComponent
__isset() Checks if a property value is null. CComponent
__set() Sets value of a component property. CComponent
__unset() Sets a component property to be null. CComponent
applyLimit() Applies LIMIT and OFFSET to the specified query criteria. CPagination
asa() Returns the named behavior object. CComponent
attachBehavior() Attaches a behavior to this component. CComponent
attachBehaviors() Attaches a list of behaviors to the component. CComponent
attachEventHandler() Attaches an event handler to an event. CComponent
canGetProperty() Determines whether a property can be read. CComponent
canSetProperty() Determines whether a property can be set. CComponent
createPageUrl() Creates the URL suitable for pagination. CPagination
detachBehavior() Detaches a behavior from the component. CComponent
detachBehaviors() Detaches all behaviors from the component. CComponent
detachEventHandler() Detaches an existing event handler. CComponent
disableBehavior() Disables an attached behavior. CComponent
disableBehaviors() Disables all behaviors attached to this component. CComponent
enableBehavior() Enables an attached behavior. CComponent
enableBehaviors() Enables all behaviors attached to this component. CComponent
evaluateExpression() Evaluates a PHP expression or callback under the context of this component. CComponent
getCurrentPage() Returns the zero-based index of the current page. Defaults to 0. CPagination
getEventHandlers() Returns the list of attached event handlers for an event. CComponent
getItemCount() Returns total number of items. Defaults to 0. CPagination
getLimit() Returns 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. CPagination
getOffset() Returns 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. CPagination
getPageCount() Returns number of pages CPagination
getPageSize() Returns number of items in each page. Defaults to 10. CPagination
hasEvent() Determines whether an event is defined. CComponent
hasEventHandler() Checks whether the named event has attached handlers. CComponent
hasProperty() Determines whether a property is defined. CComponent
raiseEvent() Raises an event. CComponent
setCurrentPage() Sets the zero-based index of the current page. CPagination
setItemCount() Sets total number of items. CPagination
setPageSize() Sets number of items in each page CPagination

属性详情

currentPage 属性
public integer getCurrentPage(boolean $recalculate=true)
public void setCurrentPage(integer $value)

the zero-based index of the current page. Defaults to 0.

itemCount 属性
public integer getItemCount()
public void setItemCount(integer $value)

total number of items. Defaults to 0.

limit 属性 只读 (自版本 v1.1.0 可用)
public integer getLimit()

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.

offset 属性 只读 (自版本 v1.1.0 可用)
public integer getOffset()

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.

pageCount 属性 只读
public integer getPageCount()

number of pages

pageSize 属性
public integer getPageSize()
public void setPageSize(integer $value)

number of items in each page. Defaults to 10.

pageVar 属性
public string $pageVar;

name of the GET variable storing the current page index. Defaults to 'page'.

params 属性
public array $params;

of parameters (name=>value) that should be used instead of GET when generating pagination URLs. Defaults to null, meaning using the currently available GET parameters.

route 属性
public string $route;

the route (controller ID and action ID) for displaying the paged contents. Defaults to empty string, meaning using the current route.

validateCurrentPage 属性 (自版本 v1.1.4 可用)
public boolean $validateCurrentPage;

whether to ensure currentPage is returning a valid page number. When this property is true, the value returned by currentPage will always be between 0 and (pageCount-1). Because pageCount relies on the correct value of itemCount, it means you must have knowledge about the total number of data items when you want to access currentPage. This is fine for SQL-based queries, but may not be feasible for other kinds of queries (e.g. MongoDB). In those cases, you may set this property to be false to skip the validation (you may need to validate yourself then). Defaults to true.

方法详情

__construct() 方法
public void __construct(integer $itemCount=0)
$itemCount integer total number of items.
源码: framework/web/CPagination.php#107 (显示)
public function __construct($itemCount=0)
{
    
$this->setItemCount($itemCount);
}

Constructor.

applyLimit() 方法
public void applyLimit(CDbCriteria $criteria)
$criteria CDbCriteria the query criteria that should be applied with the limit
源码: framework/web/CPagination.php#214 (显示)
public function applyLimit($criteria)
{
    
$criteria->limit=$this->getLimit();
    
$criteria->offset=$this->getOffset();
}

Applies LIMIT and OFFSET to the specified query criteria.

createPageUrl() 方法
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
源码: framework/web/CPagination.php#200 (显示)
public function createPageUrl($controller,$page)
{
    
$params=$this->params===null $_GET $this->params;
    if(
$page>0// page 0 is the default
        
$params[$this->pageVar]=$page+1;
    else
        unset(
$params[$this->pageVar]);
    return 
$controller->createUrl($this->route,$params);
}

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.

getCurrentPage() 方法
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.
源码: framework/web/CPagination.php#158 (显示)
public function getCurrentPage($recalculate=true)
{
    if(
$this->_currentPage===null || $recalculate)
    {
        if(isset(
$_GET[$this->pageVar]))
        {
            
$this->_currentPage=(int)$_GET[$this->pageVar]-1;
            if(
$this->validateCurrentPage)
            {
                
$pageCount=$this->getPageCount();
                if(
$this->_currentPage>=$pageCount)
                    
$this->_currentPage=$pageCount-1;
            }
            if(
$this->_currentPage<0)
                
$this->_currentPage=0;
        }
        else
            
$this->_currentPage=0;
    }
    return 
$this->_currentPage;
}

getItemCount() 方法
public integer getItemCount()
{return} integer total number of items. Defaults to 0.
源码: framework/web/CPagination.php#132 (显示)
public function getItemCount()
{
    return 
$this->_itemCount;
}

getLimit() 方法 (自版本 v1.1.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.
源码: framework/web/CPagination.php#236 (显示)
public function getLimit()
{
    return 
$this->getPageSize();
}

getOffset() 方法 (自版本 v1.1.0 可用)
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.
源码: framework/web/CPagination.php#225 (显示)
public function getOffset()
{
    return 
$this->getCurrentPage()*$this->getPageSize();
}

getPageCount() 方法
public integer getPageCount()
{return} integer number of pages
源码: framework/web/CPagination.php#149 (显示)
public function getPageCount()
{
    return (int)((
$this->_itemCount+$this->_pageSize-1)/$this->_pageSize);
}

getPageSize() 方法
public integer getPageSize()
{return} integer number of items in each page. Defaults to 10.
源码: framework/web/CPagination.php#115 (显示)
public function getPageSize()
{
    return 
$this->_pageSize;
}

setCurrentPage() 方法
public void setCurrentPage(integer $value)
$value integer the zero-based index of the current page.
源码: framework/web/CPagination.php#183 (显示)
public function setCurrentPage($value)
{
    
$this->_currentPage=$value;
    
$_GET[$this->pageVar]=$value+1;
}

setItemCount() 方法
public void setItemCount(integer $value)
$value integer total number of items.
源码: framework/web/CPagination.php#140 (显示)
public function setItemCount($value)
{
    if((
$this->_itemCount=$value)<0)
        
$this->_itemCount=0;
}

setPageSize() 方法
public void setPageSize(integer $value)
$value integer number of items in each page
源码: framework/web/CPagination.php#123 (显示)
public function setPageSize($value)
{
    if((
$this->_pageSize=$value)<=0)
        
$this->_pageSize=self::DEFAULT_PAGE_SIZE;
}