【问题标题】:How do I change default pagination attributes in Yii?如何更改 Yii 中的默认分页属性?
【发布时间】:2014-01-05 11:00:15
【问题描述】:

在我的 Yii 项目中,我希望自动获取分页的默认 pageSize,因此我不必在所有使用分页的小部件中指定它。但我似乎无法找到一种方法来全局更改分页类,而无需编辑 Yii 源文件。这可能吗?

【问题讨论】:

标签: yii pagination


【解决方案1】:

请使用以下代码在 /components/WidgetFactory.php 上创建文件。

<?php

/**
 * Custom WidgetFactory class
 * Provides two new events:
 *  - onBeforeCreateWidget
 *  - onAfterCreateWidget
 *
 * Allows for advanced global widget alteration, going a step further than CWidgetFactory's
 * typical process which allows you to define default values for widgets.
 *
 */
class WidgetFactory extends CWidgetFactory
{

    /**
     * Raised right BEFORE a widget is created.
     * @param CEvent $event the event parameter
     */
    public function onBeforeCreateWidget(CEvent $event)
    {
        $this->raiseEvent('onBeforeCreateWidget',$event);
    }

    /**
     * Raised right AFTER a widget is created.
     * @param CEvent $event the event parameter
     */
    public function onAfterCreateWidget(CEvent $event)
    {
        $this->raiseEvent('onAfterCreateWidget',$event);
    }

    /**
     * Creates a new widget based on the given class name and initial properties.
     * @param CBaseController $owner the owner of the new widget
     * @param string $className the class name of the widget. This can also be a path alias (e.g. system.web.widgets.COutputCache)
     * @param array $properties the initial property values (name=>value) of the widget.
     * @return CWidget the newly created widget whose properties have been initialized with the given values.
     */
    public function createWidget($owner,$className,$properties=array())
    {
        if (! ($this->hasEventHandler('onBeforeCreateWidget') || $this->hasEventHandler('onAfterCreateWidget')))
            return parent::createWidget($owner, $className, $properties);

        $event=new WidgetEvent($this, $owner, $className, $properties);
        if ($this->hasEventHandler('onBeforeCreateWidget'))
            $this->raiseEvent('onBeforeCreateWidget', $event);
        $event->widget=parent::createWidget($owner, $className, $properties);
        if ($this->hasEventHandler('onAfterCreateWidget'))
            $this->raiseEvent('onAfterCreateWidget', $event);
        return $event->widget;
    }

}

class WidgetEvent extends CEvent
{
    /**
     * @var CBaseController Owner of the new widget
     */
    public $owner;

    /**
     * @var string Widget class name
     */
    public $className;

    /**
     * @var CWidget The newly created widget
     */
    public $widget;

    /**
     * Constructor.
     * @param WidgetFactory $sender The WidgetFactory instance
     * @param CBaseController $owner The owner of the new widget
     * @param string $className The class name of the widget. This can also be a path alias.
     * @param array $params The initial property values (name=>value) of the widget.
     */
    public function __construct(WidgetFactory $sender, CBaseController $owner, $className, array $params=array())
    {
        parent::__construct($sender, $params);
        $this->owner=$owner;
        $this->className=$className;
    }
}

并正确 config/main.php 如下所示。

return array(
    // ...
    'components'=>array(
        // ...
        'widgetFactory'=>array(
            'class'=>'WidgetFactory',
            'onAfterCreateWidget'=>function(WidgetEvent $event){
                static $defaultPageSize=50; // YOUR_DEFAULT_PAGESIZE_HERE
                $widget=$event->widget;
                if ($widget instanceof CBaseListView) {
                    /** @var CBaseListView $widget */
                    if ($widget->dataProvider!==null && $widget->dataProvider->pagination!==false)
                        $widget->dataProvider->pagination->pageSize=$defaultPageSize;
                }
            },
        ),
        // ...
    ),
);

请注意上面配置代码中的默认页面大小。我想它会解决你的问题。

【讨论】:

  • 请将您的要点复制到答案中,而不仅仅是链接到要点。如果您删除该要点,则答案毫无意义
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-01-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多