surda/items-per-page

Items per page control for Nette Framework

v2.1.0 2022-11-04 12:19 UTC

This package is auto-updated.

Last update: 2024-04-04 16:18:48 UTC


README

Build Status Licence Latest stable PHPStan

Installation

The recommended way to is via Composer:

composer require surda/items-per-page

After that you have to register extension in config.neon:

extensions:
    itemsPerPage: Surda\ItemsPerPage\ItemsPerPageExtension

Configuration

Default

itemsPerPage:
    listOfValues: [20, 50, 100]
    defaultValue: 20
    storageKeyName: ipp
    useAjax: TRUE
    storage: Surda\KeyValueStorage\Session
    templates:
        default: bootstrap4.dropdown.latte
        nav-item: bootstrap4.nav-item.latte

Usage

Presenter

use Surda\ItemsPerPage\TItemsPerPage;
use Surda\ItemsPerPage\ItemsPerPageControl;

class ProductPresenter extends Nette\Application\UI\Presenter
{
    use TItemsPerPage;

    public function actionDefault(): void
    {
        /** @var ItemsPerPageControl $ipp */
        $ipp = $this->getComponent('ipp');

        $itemsPerPage = $ipp->getValue();
    }
}

Template

{control ipp}

Custom

use Surda\ItemsPerPage\ItemsPerPageControl;
use Surda\ItemsPerPage\ItemsPerPageFactory;

class ProductPresenter extends Nette\Application\UI\Presenter
{
    /** @var ItemsPerPageFactory */
    private $itemsPerPageFactory;

    /**
     * @param ItemsPerPageFactory $itemsPerPageFactory
     */
    public function injectItemsPerPageFactory(ItemsPerPageFactory $itemsPerPageFactory): void
    {
        $this->itemsPerPageFactory = $itemsPerPageFactory;
    }

    public function actionDefault(): void
    {
        /** @var ItemsPerPageControl $ipp */
        $ipp = $this->getComponent('ipp');

        $itemsPerPage = $ipp->getValue();
    }

    /**
     * @return ItemsPerPageControl
     */
    protected function createComponentIpp(): ItemsPerPageControl
    {
        // Init items per page component
        $control = $this->itemsPerPageFactory->create();
        
        // Define event
        $control->onChange[] = function (ItemsPerPageControl $control, int $value): void {
            // ...
        };

        return $control;
    }
}

Custom options

class ProductPresenter extends Nette\Application\UI\Presenter
{
    /**
     * @return ItemsPerPageControl
     */
    protected function createComponentIpp(): ItemsPerPageControl
    {
        // Init items per page component
        $control = $this->itemsPerPageFactory->create();
        
        // list of allowed values 
        $control->setListOfValues([20, 50, 100]);

        // Default items per page
        $control->setDefaultValue(20);

        // Value of items per page
        $control->setValue(20);

        // To use your own template
        $control->setTemplateFile('path/to/your/latte/file.latte');

        // Enable ajax (defult is enabled)
        $control->enableAjax();
        
        // Disable ajax
        $control->disableAjax();
        
        return $control;
    }
}