2017-03-07 98 views
2

我有簡單的Yii cgridview代碼與分頁。分頁工作正常,但在最後一頁我遇到了一個問題。Yii cgridview最後一頁分頁問題

例如,如果我有13條記錄在數據庫表中,每頁10頁則第一頁設置分頁它會顯示「1 - 10的13個結果」,但是當我點擊第二頁的鏈接,然後它將顯示「13 - 4」中的4 - 13「而不是」11 - 13 of 13「

這是我的代碼。

1)控制器:

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

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

2)型號:

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, 
     )); 
    } 

3)查看:

<?php 
     $this->widget('zii.widgets.grid.CGridView', array(
      'id' => 'history-grid', 
      'dataProvider' => $creditTransactionObj->search(), 
      'loadingCssClass' => '', 
      'enableSorting' => true, 
      'itemsCssClass' => 'my-teams', 
      'summaryText' => "Displaying {start} - {end} of {count} results.", 
      "emptyText" => "There is no transaction history available.", 
      'columns' => array(
       array('name' => 'transaction_date', 'header' => 'Date', 'type' => 'raw', 'value' => 'date("d-M-Y",strtotime($data->transaction_date))', 'htmlOptions' => array('class' => '')), 
       array('name' => 'credit_qty', 'header' => '# of Credits', 'sortable'=>false, 'type' => 'raw', 'value' => '($data->transaction_type == 1) ? - $data->credit_qty : $data->credit_qty', 'htmlOptions' => array('class' => '')), 
       array('name' => 'credit_type', 'header' => 'Type', 'type' => 'raw', 'value' => '$data->credit_type', 'htmlOptions' => array('class' => '')), 
       array('name' => 'transaction_type', 'header' => 'Activity', 'type' => 'raw', 'value' => '($data->transaction_type == 0) ? "Purchased" : (($data->transaction_type == 1) ? "Spent" : "Refunded")', 'htmlOptions' => array('class' => '')), 
       array('name' => 'fkasmtGroupId', 'header' => 'Group Name', 'type' => 'raw', 'value' => array($this,'getGroupName'), 'htmlOptions' => array('width' => '35%')), 
      ) 
     )); 
    ?> 

我還附上兩個頁面的屏幕截圖。

enter image description here

enter image description here

任何幫助將升值。提前致謝 !

回答

1

花了很多小時終於找到了解決方案。 其實問題出在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)) 
    )); 
} 

此更改後,最後一頁分頁工作完全正常。 謝謝!