如何在zend dojo表单中添加javascript函数?

时间:2012-08-22 09:19:26

标签: php zend-framework dojo

我正在与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
    }
}

正如您所看到的,当user_role发生更改时,我会使用一些内联JavaScript来显示/隐藏一些选项。

现在情况有点复杂。我可以继续编写内联javascript,但我想在开头声明一个js函数,并从onchange事件中调用它。

由于这个表单由多个控制器调用,因此每次

时都不会手动添加此函数
new RegistrationForm();

被召唤。

1 个答案:

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

希望这有助于其他人:)