如何在zendojo表单中添加javascript函数


How to add javascript function in a zend dojo form?

我正在使用Zend_dojo_form。

这是(部分)代码:

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 Event调用它。

由于这个表单是由几个控制器调用的,我不会每次都手动添加这个功能

new RegistrationForm();

被调用。

好吧,我想出了这个解决方案。。

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中,我用调用我需要的toggle函数(函数会变得更复杂,我需要这个变通方法来使它可读)

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

希望这能帮助其他人:)