cakephp分页功能十分方便,而且ajax分页都是简单易用。
从controller处理完了分页的基本功能后,就要从view进行处理。但先讲讲url的问题
该图片一onclick便会触发$prev这个函数,这个函数远程访问
这个方法详细地说明了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)),//都是参数
$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分页功能还需要自己琢磨。
详尽的请看源码。