如何更改Yii中的默认分页属性?

时间:2013-12-17 15:36:02

标签: yii pagination

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

1 个答案:

答案 0 :(得分: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;
                }
            },
        ),
        // ...
    ),
);

请注意以上配置代码的默认pageSize。我认为它会解决你的问题。