2012-08-22 52 views
1

我正在與Zend_dojo_form戰鬥。如何在zend dojo窗體中添加javascript函數?

下面是代碼(部分):

class Application_Form_RegistrationForm extends Zend_Dojo_Form { 

    private $_user; 
    private $_admin; 
    private $_teamadmin; 
    private $_newuser; 
    private $_redirect; 

    public function __construct($admin = false, $user = null, $redirect = '') { 
     //blablabla 
     parent::__construct(); 
    } 

    public function init() { 

     Zend_Dojo::enableForm($this); 

     $this->setMethod('post'); 
     $this->setAttribs(array(
      'id' => 'formRegistration', 
      'name' => 'formRegistration', 
     )); 

     //some decorators 

     if ($this->_admin) { 
      $this->addElement(
       //blabla + inline javascript 
       'onChange' => "if(this == ".E_UserRole::OPERATOR.") dojo.query(\".perms\").style({ display:\"block\" }); else dojo.query(\".perms\").style({ display:\"none\" }); " 
       ) 
      ); 
     } 

     if (Application_Manager_Login::hasRole(E_UserRole::ADMIN)) { 
       //add some display:none elements 
       $permission_decorators = array(
        "DijitElement", 
        "Errors", 
        array(array("data" => "HtmlTag"), array("tag" => "td", "class" => "perms", "style"=> "display:none",)), 
        array("Label", array("tag" => "td", "class" => "perms", "style"=> "display:none", 'escape' => false, 'requiredSuffix' => ' <span class="red">*</span>')), 
        array(array("row" => "HtmlTag"), array("tag" => "tr")) 
       ); 

       //hidden element 
       $this->addElement(
        'CheckBox', 
        'permission_content', 
        array(
         'decorators' => $permission_decorators, 
         'label'   => 'Gestione contenuti', 
         'checkedValue' => true, 
         'uncheckedValue' => false, 
         'checked'  => $this->_user->permission_content, 
        ) 
       );  
     } 
     //submit button and other stuff 
    } 
} 

正如你可以看到我已經把一些內嵌的JavaScript來顯示/隱藏某些選項時USER_ROLE變化。

現在情況有點複雜。我可以繼續寫內聯的JavaScript,但我想在開頭聲明一個js函數,並從onchange事件中調用它。

由於這種形式是由多個控制器稱爲我不會手動每一個

new RegistrationForm(); 

被調用時添加該功能。

+0

你不能在外部JS文件中不顯眼嗎? –

+0

@TimFountain我找到了解決方案。外化js不是我想要的,因爲我不在視圖中。我要寫解決方案。 –

回答

0

好吧,我想出了這個解決方案..

function addJavascript(){ 

     echo "<script> 
     function toggle(e){ 
      alert(e); 
      if(e == ".E_UserRole::USER." || e == ".E_UserRole::ADMIN."){ 
       dojo.query(\".perms\").style({ display:\"none\" }); 
      } 
      else { 
       dojo.query(\".perms\").style({ display:\"block\" }); 
      } 
     } 
     </script> 
     "; 

} 

我聲明的類裏面這個功能,我只是把它在開始的時候:

$this->addJavascript(); 

然後爲onChange我調用切換功能,我需要(功能,將變得更加複雜,我需要這個解決辦法,使其讀取)與:

$this->addElement(
     //blabla 
     'onChange' => "toggle(this)", 
     ) 
); 

希望這可以幫助別人:)

相關問題