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的問題
<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這個函數,這個函數遠程訪問
$product['Product']['taxonomy_id'],'page:'.($this->Paginator->current()-1)),//都是參數