pnixx/pagination

Foundation Zurb 分页组件,适用于 PHP 5.4

1.0.0 2015-07-10 06:51 UTC

This package is not auto-updated.

Last update: 2024-09-14 18:04:09 UTC


README

Foundation 框架的分页是一个简单易用的类,可以为你提供分页链接。你可以通过编写自定义渲染器或使用可用的渲染器(如 Twitter 的 Bootstrap)来自定义页面链接的外观和感觉,或者扩展它们。

安装

如果你使用的是 composer,请在壳中输入,否则你必须下载 Pagination 和 Exception 类

composer require pnixx/pagination

基本用法

分页类本身没有渲染,它的主要目标是提供一个项目(链接)数组。这可以很容易地完成

<?php

use PNixx\Pagination\Pagination;
$pagination = new Pagination();

// Set the total number of items
$pagination->setTotalItems(45);

// Render html
echo $pagination->render(); 

基本分页渲染 HTML,如下所示

<ul class="pagination">
  <li class="arrow unavailable"><a href="">&laquo;</a></li>
  <li class="current"><a href="">1</a></li>
  <li><a href="">2</a></li>
  <li><a href="">3</a></li>
  <li><a href="">4</a></li>
  <li class="unavailable"><a href="">&hellip;</a></li>
  <li><a href="">12</a></li>
  <li><a href="">13</a></li>
  <li class="arrow"><a href="">&raquo;</a></li>
</ul>

设置器

<?php

// Total number of items. This one MUST be set.
$pagination->setTotalItems($total_items);

// setTotalItems() alias
$pagination->setItems($total_items);

// Sets the number of items (lines) in a single page.
$pagination->setItemsPerPage($per_page);

// setItemsPerPage() alias
$pagination->setLimit($per_page);

// Sets the page number manually.
$pagination->setPage($page);

/*
 * Sets the URI pattern for creating links for pages.
 * Default pattern is "page={page}"  (URLs like /posts/show?page=5)
 * Can be set for example to "p={page}" or anything else for $_GET parameter
 * Can be set also to "page/{page}" for friendly URLs. In this case Pagination
 * will build URLs like: /posts/show/page/5
 */
$pagination->setPattern($pattern);

/*
 * Sets the current URI. Default is $_SERVER["REQUEST_URI"]
 * Handy for unit tests.
 */
$pagination->setUri($uri);

// Sets the proximity. See getProximity() above for more explanations.
$pagination->setProximity($proximity);

// Prev and Next labels
$pagination->setLabelPrev($label_prev);
$pagination->setLabelNext($label_next);

// Show arrows always or only not on the first or last page. Default: true (always)
$pagination->setShowArrowIfNeed($show_arrow_if_need);

每个设置都可以在分页构造函数中完成。

<?php

$pagination = new Pagination([
    'items' => 100, // or 'total'
    'per_page' => 10,
    'proximity' => 3,
    'uri' => 'http://example.com/show/page:6',
    'pattern' => 'page:{page}',
    'page' => 6,
])