ajax page
cakephp分页功能十分方便,而且ajax分页都是简单易用。
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的问题
<divclass="leftbutton"><!--Ajax上一页 -->
<scripttype="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)),//都是参数
由于ajax返回内容update某个区的时候要一模一样,我使用多了两个方法,就是上图第二第三个controller方法,但图中有处不一样。