简体   繁体   English

将javascript函数添加到Vaadin

[英]Adding javascript function to Vaadin

I want to add a javascript function to vaadin so it would be performed when the button is pressed. 我想为vaadin添加一个javascript函数,以便在按下按钮时执行。 So far I have tried it these two ways. 到目前为止,我已经尝试过这两种方式。

String string = "function myFunction(p) {"
    +"alert(p);"
+"}"
JavaScript.getCurrent().execute(string);
JavaScript.getCurrent().execute("myFunction('Hello');");

And

String string = "function myFunction(p1, p2) {"
    +"alert(p);"
+"}"
+"myFunction('Hello');"
JavaScript.getCurrent().execute(string);

Buth both ways ended with no successful result. 但两种方式都没有成功结果。

Update : I changed the codes so they will print something. 更新 :我更改了代码,以便打印一些东西。 Those two codes still doesn't work. 这两个代码仍然不起作用。

Your code doesn't work because your myFunction is not registered anywhere when you try to call it. 您的代码不起作用,因为当您尝试调用myFunction未在任何位置注册。 What I mean by this your code: 我的意思是你的代码:

String string = "function myFunction(p) {"
    +"alert(p);"
+"}"
JavaScript.getCurrent().execute(string);

have no effect. 没有效果。 To properly define a function in JavaScript using Vaadin you need to assign it to a some variable. 要使用Vaadin在JavaScript中正确定义函数,您需要将其分配给某个变量。 This simple change will enable your code to run: 这个简单的更改将使您的代码能够运行:

String string = "soReadyToHelp = function myFunction(p) {"
        +"alert(p);"
    +"}";
JavaScript.getCurrent().execute(string);
JavaScript.getCurrent().execute("soReadyToHelp('Hello');");

Note that, when integrating Vaadin and JavaScript you may find AbstractJavaScriptComponent useful. 请注意,在集成Vaadin和JavaScript时,您可能会发现AbstractJavaScriptComponent很有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM