jinnguyen/puja-paginator

Puja-Paginator is a flexible component for paginating collections of data and presenting that data to users.

v1.0.0 2017-01-18 02:07 UTC

This package is not auto-updated.

Last update: 2024-04-22 13:47:33 UTC


README

Puja-Paginator is a flexible component for paginating collections of data and presenting that data to users.

Installation

Just run this on the command line:

composer require jinnguyen/puja-paginator

Usage

include '/path/to/vendor/autoload.php';
use Puja\Paginator\Paginator;

Examples:

Simple

$paginator = new Paginator(
    '/news/', // url
    100, // total of records
    10 // number of records per page
);

The rest of the documentation will assume you have a $paginator instance on which you are making calls.

Adding renderer

$paginator->addRenderer('simple', 'Puja\Paginator\Renderer\Simple'); // Puja\Paginator\Renderer\Simple must be extended of Puja\Paginator\Renderer\RendererAbstract

Set labels

$paginator->setLabels($labes); // default $labels is [First, Prev, Next, Last]

First,Last and Current CSS classes

    
  • Home // First element
  • Page
  • Subpage // Current Element
  • Subpage 2 // Last Element

The first/last css classes are the class of first/last Breadcrumb element

$paginator->setFirstCssClassName($className);
$paginator->setLastCssClassName($className);
$paginator->setCurrentCssClassName($className);

The Element

The default paging element is <li class="{CssClassName}">%s{Divider}</li>. To change it, use the setElement method like so:

$paginator->setElement('<span class="{FirstLastCss}">%s{Divider}</span>');

Note:

"%s" is required for Paginator::$element
{CssClassName}: will be replaced by Paginator::$firstCssClassName/Paginator::$currentCssClassName/Paginator::$lastCssClassName if this element is first/current/last element.
{Divider}: will be replaced by Paginator::$divider

The List Element

The default list element used to wrap the paging, is <ul>%s</ul>. To change it, use the setListElement method like so:

$paginator->setListElement('<ol class="ol-paging">%s</ol>');

Note:

"%s" is required for Paginator::$listElement

Divider

The default divider is `` (empty). This will be replace to placeholder {Divider} in property Paginator::$element. If you'd like to change it to, for example, /, you can just do:

$paginator->setDivider('/');

Output

Finally, when you actually want to display your breadcrumbs, all you need to do is call the render() method on the instance:

echo $paginator->render('simple');
echo $paginator->render('basic');
echo $paginator->render(); // default is `basic`

Note

You can write custom Renderer by yourself. You can check Puja\Paginator\Renderer\Simple as a sample

Example

class CustomRenderer extends \Puja\Paginator\Renderer\RendererAbstract
{
    public function parse()
    {
        $p = '';
        for ($i = 0; $i < $this->paginator->getTotalPage(); $i++) {
            $p .= $this->paginator->getPageElement($i, true);
        }

        return $p;
    }
}

$paginator->addRenderer('custom', 'CustomRenderer');
$paginator->render('custom');

Note that by default First/Prev/Next/Last titles are rendered with escaping HTML characters, if you'd like to ignore it just do like so:

$paginator->setSafeHtml(false);