2013-12-17 5 views
1

내 Yii 프로젝트에서 페이지 매김의 기본 pageSize를 자동으로 가져 오려고하므로 페이지 매김을 사용하는 모든 위젯에서 지정할 필요가 없습니다. 하지만 Yii 소스 파일을 편집하지 않고도 페이지 매김 클래스를 전역 적으로 변경할 수있는 방법을 찾을 수 없습니다. 이것이 가능한가?Yii에서 기본 페이지 매김 속성을 변경하려면 어떻게해야합니까?

+2

http://www.yiiframework.com/forum/index.php/topic/39265-widget-pagination-global-solution/ 필요한 것이 있습니다! – secretlm

답변

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; 
       } 
      }, 
     ), 
     // ... 
    ), 
); 

설정 코드에서 위의 크기 페이지를 참조하십시오. 나는 그것이 당신의 문제를 해결할 것이라고 생각합니다.

+0

요점을 답안에 복사하고 요지로 연결하지 마십시오. 그 요점을 지우면 대답은 의미가 없습니다. – dstarh

관련 문제