cakephp菜鳥筆記-分頁

cakephp分頁功能十分方便,而且ajax分頁都是簡單易用。
這個方法詳細地說明了ajax的分頁。普通的分頁就是在controller去掉有註釋的那句話
function viewTaxonomy($tid = null,$pid=null)
 {
  $this->layout = 'index';
  if (!$tid) {
   $this->redirect(array('action' => 'index'));
  }
  $taxonomy = $this->Taxonomy->read(null,$tid);
  if(intval($taxonomy['Taxonomy']['lang']) != intval($this->Session->read('lang')))
  {
   $tid = ($tid >7)?($tid -7):( $tid + 7);
   $this->redirect(array('action' => 'viewTaxonomy',$tid));
  }
  $params = array('conditions' => array('taxonomy_id' =>intval($tid)),
      'order' => array('ctime' =>'desc'));
  $product = null;
  if($pid ==null)
  {
   $product = $this->Product->find('first',$params);
   
  }
  else
  {  
   $product = $this->Product->read(null,$pid);
  }
  $this->set('product',$product);//在view可以調用$product這個變量。
  $products = $this->paginate('Product', array('taxonomy_id' =>intval($tid)));
  //分頁效果可以在第二個參數加上不同的條件。如果在controller顯式用$uses就必須$thi->paginate('Product'),  //說明分頁使用的模型。
  $this->set('products',$products);
  $this->helpers['Paginator'] = array('ajax' => 'Ajax');//Ajax分頁效果
  
 }
 function viewProduct($pid= null)
 { 
  if($pid ==null)
  {
   $product = $this->Product->find('first',$params);
  }
  else
  {
   if ($this ->RequestHandler->isAjax())
   {
     $product=$this->Product->read(null, $pid);
     $this->set('product',$product);
   }
  }
  $this->render('viewProduct','ajax');
 }
 
 function productList($tid= null)
 { 
  if ($this ->RequestHandler->isAjax())
  {
   if (!$tid) {
    $this->redirect(array('action' => 'index'));
   }
   $products = $this->paginate('Product', array('taxonomy_id' =>intval($tid)));
   $this->set('products',$products);
   $this->set('product_tid',$tid);//ajax的使用
   $this->helpers['Paginator'] = array('ajax' => 'Ajax');
   $this->render('productList','ajax');//第一個是view的名稱,第二個參數是使用ajax的layout。
  }
  else
  {
   $this->redirect(array('action' => 'index'));
  }
 }

從controller處理完了分頁的基本功能後,就要從view進行處理。但先講講url的問題
/gj/langs(控制器)/viewTaxonomy(方法)/8(分類的ID,屬於arg)/page:3
這個url可以看出page:3這個參數是用於分頁的。

paginator有很多方法,在view裏面可以直接使用,$this->Paginator->hasPrev()如此例。自動生成view的有分頁功能。
分頁其他方法都可以自行查詢,都很簡單可以理解。參照bake生成代碼一般都能滿足分頁需求。

以下着重說明Ajax分頁的功能。
主要控制器方法是以上的代碼,與其相關的view有
view_taxonomy.ctp
view_product.ctp
product_list.ctp。
後面兩個的代碼是屬於第一個ctp代碼,由於ajax會自動替換一部分,所以必須保持替換區代碼完全相同。
在controller裏:
$this ->RequestHandler->isAjax()判斷是否是ajax請求,使用這個前必須加載var $components = array( 'RequestHandler' );
在view其實也可以requestAction()調用其他的controller的方法,這個可以看手冊。
$this->helpers['Paginator'] = array('ajax' => 'Ajax');
$this->render('productList','ajax');//第一個是view的名稱,第二個參數是使用ajax的layout。
這兩個是如註釋解釋,特別注意,啓用ajax的功能。

在view裏面:
$ajax使用ajax請求。
由於我採用圖片來進行ajax請求,使用了$ajax->remoteFunction函數這個函數
<div class="leftbutton"><!--Ajax上一頁 -->
  <script type="text/javascript">
   <?php  
    $prev = null;
    if($this->Paginator->hasPrev())//採用了$ajax->remoteFunction函數進行遠程調用
    {
     $prev = $ajax->remoteFunction( 
        array( 
        'url' =>array( 'controller' => 'langs', 'action' => 'productList',
         $product['Product']['taxonomy_id'],'page:'.($this->Paginator->current()-1)),//都是參數
               'update' => 'productPage' //id選擇器替換內容的ID 
           ) 
       );
      } 
     ?>
    </script>
    <?php echo $html->image('left.jpg',array('onclick'=>$prev));?>
   </div>

該圖片一onclick便會觸發$prev這個函數,這個函數遠程訪問
array( 'controller' => 'langs', 'action' => 'productList',
$product['Product']['taxonomy_id'],'page:'.($this->Paginator->current()-1)),//都是參數
'page:'.($this->Paginator->current()-1),這個就是訪問上一頁的功能。
所以我之前說了/gj/langs(控制器)/viewTaxonomy(方法)/8(分類的ID,屬於arg)/page:3就是現在可以明白。
'update' => 'productPage' //id選擇器替換內容的ID
這個從ajax返回的內容自動替換productPage區域。

要使排版不會出問題,重點就在這裏,

由於ajax返回內容update某個區的時候要一模一樣,我使用多了兩個方法,就是上圖第二第三個controller方法,但圖中有處不一樣。
由於有兩處ajax調用,一開始view_taxonomy.ctp 的$product['Product']['taxonomy_id']一開始加載時能獲取,但產品變化後就不能獲取了,所以我在product_list.ctp裏面更換成 $product_tid,這個變量是我在controller裏面$this->set('product_tid',$tid);進行設置。以免流失了taxonomy的id。
注意update的id,每次update都分成一個小區進行處理,化大成小的思想,組合起來就可以解決這個問題了。
當然更復雜的ajax分頁功能還需要自己琢磨。

詳盡的請看源碼。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章