CakePHP从1.2开始内置了分页功能,控制器的paginate()方法和视图的paginator助手配合使用,控制器里是paginate,视图里是paginator!
忘了是谁说的CakePHP没有合适的文档说明分页,我感觉的确是这样的,那是非常痛苦啊。摘抄了同学网上找的一份示例,很简单,如下:
建立一个控制器(相应的模型,视图,数据库等等自己随便建就行):
class ThreadsController extends AppController
{
// ...
var $paginate = array(
'Thread' => array(
'conditions' => '',
'fields' => '',
'order' => 'Thread.id DESC',
'limit' => 10,
'page' => 1,
'recursive' => 0
)
);
function index()
{
$this->Thread->recursive = 0;
if(isset($this->params['requested']))
{
return $this->paginate();
}
$this->set('threads', $this->paginate());
}
......
通过控制器属性(var $paginate)来设置分页相关的缺省值。然后在控制器方法(index)里通过调用paginate()方法就可以完成分页了。
有了paginate方法后,你基本不用再写findAll方法了,自然也不用记忆findAll那冗长的参数列表了。
控制器编码完成后,再让我们看看视图部分的编写:
首先我们没有必要手动加载分页助手(paginator),因为当我们在控制器里调用paginate()方法时,paginator助手会被自动加载。
<div class="pagination">
<?php if(! $paginator->hasPrev()): ?>
<span class="disabled"><</span>
<?php endif; ?>
<?php echo $paginator->prev('<'); ?>
<?php echo $paginator->numbers(array('separator' => '')); ?>
<?php echo $paginator->next('>'); ?>
<?php if(! $paginator->hasNext()): ?>
<span class="disabled">></span>
<?php endif; ?>
</div>
至于各种属性用法,可以自己查看助手文件的源代码即可,摘录如下:
/**
* Holds the default options for pagination links
*
* The values that may be specified are:
* - <i>$options['sort']</i> the key that the recordset is sorted.
* - <i>$options['direction']</i> Direction of the sorting (default: 'asc').
* - <i>$options['format']</i> Format of the counter. Supported formats are 'range' and 'pages'
* and custom (default). In the default mode the supplied string is
* parsed and constants are replaced by their actual values.
* Constants: %page%, %pages%, %current%, %count%, %start%, %end% .
* - <i>$options['separator']</i> The separator of the actual page and number of pages (default: ' of ').
* - <i>$options['url']</i> Url of the action. See Router::url()
* - <i>$options['model']</i> The name of the model.
* - <i>$options['escape']</i> Defines if the title field for the link should be escaped (default: true).
* - <i>$options['update']</i> DOM id of the element updated with the results of the AJAX call.
* If this key isn't specified Paginator will use plain HTML links.
* - <i>$options['indicator']</i> DOM id of the element that will be shown when doing AJAX requests.
*/
类似的方法还有:
<?php echo $paginator->prev('上页'); ?>
<?php echo $paginator->next('下页'); ?>
<?php echo $paginator->counter(); ?>
等等。
我们还可以通过设定CSS让分页更漂亮点,这里给一个例子:
<style>
div.pagination {
margin: 4px;
padding: 4px;
color: #FF0000;
font-size: 12px;
font-weight: bold;
vertical-align: middle;
}
div.pagination a {
margin: 4px;
padding: 2px 4px;
text-decoration: none;
border: 1px solid #AAAADD;
background: #FFFFFF;
color: #000099;
font-weight: normal;
}
div.pagination a:hover, div.pagination a:active {
border: 1px solid #000099;
background: #000099;
color: #FFFFFF;
font-weight: normal;
}
span.disabled {
margin: 4px;
padding: 2px 4px;
border: 1px solid #CCCCCC;
background-color: #FFFFFF;
color: #CCCCCC;
font-weight: normal;
}
</style>
注:本来的效果中,点击链接的时候,虚线框总是有些歪,后来看了flickr的样式表代码,发现设置一下“vertical-align”就可以搞定。
看看效果,挺漂亮吧,是不是有点digg/flickr的味道了。
注:分页助手还提供了排序的功能($paginator->sort()),这个功能实在太方便了,你用用就知道我为啥这么说了。
还有一点,CakePHP分页助手生成的分页URL类似下面的样子:
/threads/index/page:1/sort:created/direction:desc
开始看的时候多少有点怪怪的,习惯了就好了。