发新话题
打印

学习CakePHP1.2的缺省内置分页功能

学习CakePHP1.2的缺省内置分页功能

作者:老王

CakePHP从1.2开始内置了分页功能,分别对应控制器的paginate()方法和视图的paginator助手。

由于CakePHP1.2没有合适的文档,所以如何使用新功能都要自己看源代码来摸索,还好不是很复杂,现在总结出来,如下:

建立一个例子控制器(相应的模型,视图,数据库等等就自己建立吧,我就不罗嗦了):
复制内容到剪贴板
代码:
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">&lt;</span>
<?php endif; ?>
<?php echo $paginator->prev('<'); ?>
<?php echo $paginator->numbers(array('separator' => '')); ?>
<?php echo $paginator->next('>'); ?>
<?php if(! $paginator->hasNext()): ?>
<span class="disabled">&gt;</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

开始看的时候多少有点怪怪的,习惯了就好了。

TOP

谢谢老王

TOP

希望继续看到您的关于cakephp的帖子

TOP

发新话题