状态 草稿
Todo content needs to be reorganized, more explanation for config and class properties
官方最后更新时间 2008/10/07 14:08

分页库(Pagination Library)

分页类可以自动显示分页链接以及样式就像是一个页面的导航一样。比如:

« First < 1 2 3 4 5 > Last »

链接涉及到页面数字和没有数据偏移的问题。:?:

原文:The links refer to the Page number and Not the offset of the data.

这个库很容易配置。默认分页设置存放在 system/config/pagination.php。你可以复制一份到 application/config 覆写系统的配置。

请注意这个库任何方式都不会影响你的数据,它只是生成链接。开发者必须编写代码获取数据并链接为链接。sql_offset 属性可以基于当前页面以及配置中 items_per_page 的值自动设置每页的数量。这些值你可以在你查询的 LIMITOFFSET 中使用。

页面链接使用 system/views/pagination 的视图文件生成。共有四种样式。你可以根据需要选择不同的样式或者创建新的样式。你也可以复制一份到 application/views/pagination 目录进行覆写。

更多信息:页面教程(英文)

加载分页库

在控制器中加载分页类

config/pagination.php 中获取配置设置。设置也可以是动态的以数组形式设置到类中。

$this->pagination = new Pagination($config);

通过 $this→pagination 可以激活此库。

分页配置属性

下面的配置属性可以传递给分页构造器:

  • base_url
  • directory
  • style
  • uri_segment
  • query_string
  • items_per_page
  • total_items
  • auto_hide

分页类属性

下面的分页类属性自动生成,并可以在控制器中使用:

  • url
  • current_page
  • total_pages
  • current_first_item
  • current_last_item
  • first_page
  • last_page
  • previous_page
  • next_page
  • sql_offset
  • sql_limit

方法

initialize()

$this→pagination→initialize() 用来动态设置分页配置。它自动被类构造器掉哟个,所以不再需要再调用这个方法,除非你想要覆写分页配置。

// 覆写已存在的配置
$this->pagination->initialize(array('uri_segment' => 'pages'));

render()

$this→pagination→render() 用来显示分页链接。你可以设置分页样式。不过请注意:链接也可以通过简单的语句 echo $this→pagination 输出。

// 使用 'digg' 样式覆写默认的 'classic' 样式
$this->pagination->render('digg'));

范例一

$this->pagination = new Pagination(array(
    // 'base_url'    => 'welcome/pagination_example/page/', // base_url will default to current uri
    'uri_segment'    => 'page', // pass a string as uri_segment to trigger former 'label' functionality
    'total_items'    => 254, // use db count query here of course
    'items_per_page' => 10, // it may be handy to set defaults for stuff like this in config/pagination.php
    'style'          => 'classic' // pick one from: classic (default), digg, extended, punbb, or add your own!
));
 
// 仅仅只是输出链接样(__toString() rocks!)
echo 'Classic style: '.$this->pagination;
 
// 你也可以使用 render() 方法并选择一种样式输出
echo '<hr />Digg style:     '.$this->pagination->render('digg');
 
echo '<hr />Extended style: '.$this->pagination->render('extended');
 
echo '<hr />PunBB style:    '.$this->pagination->render('punbb');
 
echo 'done in {execution_time} seconds';

输出结果:

Classic style: 1 2 3 4 5 6 7 8 9 10 11 13 14 15 16 17 18 19 20 21 22 23 24 25 26 > last ›

Digg style: « previous 1 2 3 4 5 6 7 8 9 10 … 25 26 next »

Extended style: « previous | page 1 of 26 | items 1–10 of 254 | next »

PunBB style: pages: 1 2 3 … 26

如果你看到了“pagination.next”,这是英文分页从你的本地使用 Kohana::lang 来寻找语言包。分页的本地语言包放置在 “system/i18n/[语言]/pagination.php”

范例二

从控制器方法引用

public function page($page_no)
{
    $content = new View('pages/items');
    $items = new Items_Model;
 
    $content->items = $items->get_items($page_no, 10); // 获得分页开始的位移,以及获得的数量
 
    $this->pagination = new Pagination(array(
        'base_url'    => 'items/page/', // 配置 URL 的控制器段为 'items' 和方法段为 'page'
        'uri_segment' => 'page', // 这样的配置 URI 会像这样显示 http://domain/items/page/19
        'total_items' => count($items->get_item_count()) // 共计数量
 
    // 注意其他的配置项是从分页配置文件中获得
    ));
 
    $this->template->set(array(
        'title'   => 'Items',
        'content' => $content
    ));
}

视图中引用来显示链接

<h3>Items</h3>
<?php echo $this->pagination->render() ?>

创建自定义分页样式

默认 Kohana 分页样式存放在 system/views/pagination 目录下面。如果想要自定义以存在的样式或者创建一个新的分页样式,你需要:

  1. 创建一个新的名为 application/views/pagination 的目录
  2. system/views/pagination(比如 classic.php)复制已存在的分页样式到 application/views/pagination 目录
  3. 您有两种选项,重命名现有的分页风格来创建新的样式(例如 custom.php )或覆写已存在的样式。

注意:如果你要创建一个分页样式(通过改名的方式),党你在创建分页链接时,你必须在引用新的自定义文件名(比如:$this→pagination→render('custom')

libraries/pagination.txt · 最后更改: 2008/11/18 19:48 由 icyleaf