Yii cgridview最后一页分页问题

时间:2017-03-07 06:27:15

标签: php pagination yii1.x

我有简单的Yii cgridview代码和分页。分页工作正常,但在最后一页我遇到了一个问题。

例如,如果我在数据库表中有13条记录,并为每页设置10页的分页,那么第一页将显示“13个结果中的1 - 10”但是当我点击时第二页链接然后它将显示“4 - 13 of 13”而不是“11 - 13 of 13”

这是我的代码。

1)控制器:

md-maxlength="10"

2)型号:

function actiontransactionHistory(){
        $creditTransactionObj = new CreditTransaction();

        $this->render('history',array(
            'creditTransactionObj'=>$creditTransactionObj,
        ));
    }

3)查看:

public function search()
    {
        // Warning: Please modify the following code to remove attributes that
        // should not be searched.

        $criteria=new CDbCriteria;

        $criteria->compare('id',$this->id,true);
        $criteria->compare('fkasmtGroupId',$this->fkasmtGroupId);
        $criteria->compare('fkgroupSurveyUserId',$this->fkgroupSurveyUserId,true);
        $criteria->compare('fkasmtId',$this->fkasmtId);
        $criteria->compare('transaction_type',$this->transaction_type);
        $criteria->compare('credit_type',$this->credit_type,true);
        $criteria->compare('credit_qty',$this->credit_qty);
        $criteria->compare('transaction_date',$this->transaction_date,true);
        $criteria->compare('isDelete',$this->isDelete);
        $criteria->compare('status',$this->status);
        $criteria->compare('created_at',$this->created_at,true);
        $criteria->compare('modified_at',$this->modified_at,true);

        $sort = array(
            "defaultOrder" => "transaction_date DESC",
        );

        return new CActiveDataProvider($this, array(
            "criteria"=>$criteria,
            "sort" => $sort,
        ));
    }

我还附上两页截图。

enter image description here

enter image description here

任何帮助都将不胜感激。提前谢谢!

1 个答案:

答案 0 :(得分:2)

最后花了很多时间我找到了解决这个问题的方法。 实际上问题出在 fetchData()函数中 yii / framework / web / CActiveDataProvider.php 框架类文件。

fetchData()方法中,未针对最后一页分页正确计算限制。所以我做了更改以计算正确的限制。

旧代码:

protected function fetchData()
{
    $criteria=clone $this->getCriteria();

    if(($pagination=$this->getPagination())!==false)
    {
        $pagination->setItemCount($this->getTotalItemCount());
        $pagination->applyLimit($criteria);
    }

    $baseCriteria=$this->model->getDbCriteria(false);

    if(($sort=$this->getSort())!==false)
    {
        // set model criteria so that CSort can use its table alias setting
        if($baseCriteria!==null)
        {
            $c=clone $baseCriteria;
            $c->mergeWith($criteria);
            $this->model->setDbCriteria($c);
        }
        else
            $this->model->setDbCriteria($criteria);
        $sort->applyOrder($criteria);
    }

    $this->model->setDbCriteria($baseCriteria!==null ? clone $baseCriteria : null);
    $data=$this->model->findAll($criteria);
    $this->model->setDbCriteria($baseCriteria);  // restore original criteria
    return $data;
}

新代码:

protected function fetchData()
{
        $criteria=clone $this->getCriteria();

        if(($pagination=$this->getPagination())!==false)
        {
                $pagination->setItemCount($this->getTotalItemCount());
                $pagination->applyLimit($criteria);
                // update limit to the correct value for the last page 
                $limit=$pagination->getLimit();
                $offset=$pagination->getOffset();
                if ( $offset+$limit > $pagination->getItemCount() )
                        $criteria->limit = $pagination->getItemCount() - $offset;

        }
        $baseCriteria=$this->model->getDbCriteria(false);

        if(($sort=$this->getSort())!==false)
        {
                // set model criteria so that CSort can use its table alias setting
                if($baseCriteria!==null)
                {
                        $c=clone $baseCriteria;
                        $c->mergeWith($criteria);
                        $this->model->setDbCriteria($c);
                }
                else
                        $this->model->setDbCriteria($criteria);
                $sort->applyOrder($criteria);
        }

        $this->model->setDbCriteria($baseCriteria!==null ? clone $baseCriteria : null);
        $data=$this->model->findAll($criteria);
        $this->model->setDbCriteria($baseCriteria);  // restore original criteria
        return $data;
}

但请记住,永远不要更新框架的核心类文件。所以我只是在我的模型文件中扩展这个方法并写下代码。

我的模型文件中的最终代码没有更改框架文件:

class CustomActiveDataProvider extends CActiveDataProvider
{
        /**
         * Fetches the data from the persistent data storage.
         * @return array list of data items
         */
        protected function fetchData()
        {
                $criteria=clone $this->getCriteria();

                if(($pagination=$this->getPagination())!==false)
                {
                        $pagination->setItemCount($this->getTotalItemCount());
                        $pagination->applyLimit($criteria);
                        // update limit to the correct value for the last page 
                        $limit=$pagination->getLimit();
                        $offset=$pagination->getOffset();
                        if ( $offset+$limit > $pagination->getItemCount() )
                                $criteria->limit = $pagination->getItemCount() - $offset;

                }
                $baseCriteria=$this->model->getDbCriteria(false);

                if(($sort=$this->getSort())!==false)
                {
                        // set model criteria so that CSort can use its table alias setting
                        if($baseCriteria!==null)
                        {
                                $c=clone $baseCriteria;
                                $c->mergeWith($criteria);
                                $this->model->setDbCriteria($c);
                        }
                        else
                                $this->model->setDbCriteria($criteria);
                        $sort->applyOrder($criteria);
                }

                $this->model->setDbCriteria($baseCriteria!==null ? clone $baseCriteria : null);
                $data=$this->model->findAll($criteria);
                $this->model->setDbCriteria($baseCriteria);  // restore original criteria
                return $data;
        }

}

// Used this custome active data provider as shown in below.
public function search()
{
    $criteria=new CDbCriteria;

    $criteria->compare('id',$this->id,true);
    $criteria->compare('isDelete',$this->isDelete);
    $criteria->compare('status',$this->status);
    $criteria->compare('created_at',$this->created_at,true);
    $criteria->compare('modified_at',$this->modified_at,true);

    $sort = array(
        "defaultOrder" => "transaction_date DESC",
    );

    return new CustomActiveDataProvider($this, array(
        "criteria"=>$criteria,
        "sort" => $sort,
        "pagination" => array('pageSize' => (isset($_REQUEST['pageSize'])?$_REQUEST['pageSize']:10))
    ));
}

在此更改后,最后一页分页工作完全正常。 谢谢!