surda / items-per-page

用于 Nette 框架的每页项目数量控制

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

This package is auto-updated.

Last update: 2024-09-04 17:05:32 UTC


README

Build Status Licence Latest stable PHPStan

安装

推荐的方式是通过 Composer

composer require surda/items-per-page

之后您需要在 config.neon 中注册扩展

extensions:
    itemsPerPage: Surda\ItemsPerPage\ItemsPerPageExtension

配置

默认

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

用法

展示器

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();
    }
}

模板

{control ipp}

自定义

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;
    }
}

自定义选项

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;
    }
}