gpt4 book ai didi

javascript - 如何在 vaadin 对话框中启用 javascript window.close()?

转载 作者:行者123 更新时间:2023-12-03 06:21:53 26 4
gpt4 key购买 nike

我有一个 html 页面,其中有一个“关闭”按钮,单击该按钮应该关闭窗口。此 html 页面正在 Vaadin 对话框上加载。据我所知,Vaadin 负责使用 window.setClosable(true) 关闭对话框。但是,html 页面中的按钮也应该执行相同的操作。我如何启用此功能?
下面是代码:

myHelp.html:

<html>
<head>
</head>
<body>
.
.
<!-- This is at the footer right corner -->
<p class="myClass" align="right"><img src="images/iconclose.gif" width="50" height="10" border="0" onClick="window.close()" title="Close"></p>
.
.
</body>
</html>

Java 代码:

.
.
String link = "test/myHelp.html";
MenuItem menuItem = null;
if (link.contains("/test")) {

menuItem = menuBar.addItem("", new ExternalResource(StringUtil.append("/images/", images.get(i))), new Command() {

@Override
public void menuSelected(MenuItem selectedItem) {

final Window window = new Window(this.caption);
window.setClosable(true);
window.setWindowMode(WindowMode.NORMAL);
window.setModal(true);
window.setDraggable(true);
window.setResizable(false);
window.center();
window.addStyleName("abcdailog");
VerticalLayout layout = new VerticalLayout();
layout.setSizeFull();
layout.setSpacing(true);
layout.setMargin(true);

if (!CommonUtil.isEmpty(this.styleName)) {
window.addStyleName("abcStyle");
layout.setMargin(false);
}
if (!CommonUtil.isEmpty(link)) {
BrowserFrame browser = new BrowserFrame(null, new ExternalResource(this.link));
browser.setSizeFull();
layout.addComponent(browser);
} else {
verticalLayout.setSizeFull();
layout.addComponent(verticalLayout);
}

window.setContent(layout);
UI.getCurrent().addWindow(window);
}
}
});
.
.
.

发生的情况是 myHtml 被加载到新窗口中。作为一个 vaadin 窗口,一切都很好,但由于 html 在图像上有一个 window.close ,它应该工作但不起作用。我希望代码有助于更好地理解。

最佳答案

最简单的方法是在窗口底部添加一个 Vaadin 按钮。但是,如果这不是一个选择,有几种方法可以做到这一点,我想到的第一个方法(可能也是下一个最简单的方法)是添加 a callback JS function关闭窗口。

1) 自定义窗口实现:

import com.vaadin.shared.ui.label.ContentMode;
import com.vaadin.ui.JavaScript;
import com.vaadin.ui.Label;
import com.vaadin.ui.UI;
import com.vaadin.ui.Window;

public class MyHtmlWindow extends Window {
private static final String CLOSE_WINDOW_FUNCTION = "closeWindow";

public MyHtmlWindow() {
// some window configuration
setModal(true);
setClosable(true);
setResizable(false);
}

public void show(UI ui) {
// add some HTML content in a label including the call to the closeWindow() JS function
setContent(new Label("<html>\n" +
"<button type=\"button\" onClick=\"" + CLOSE_WINDOW_FUNCTION + "();\">Close</button>\n" +
"<script>\n", ContentMode.HTML));

// add a JS function to close the window
JavaScript.getCurrent().addFunction(CLOSE_WINDOW_FUNCTION, arguments -> this.close());

// show the window
ui.addWindow(this);
}

@Override
public void close() {
// call super so everything gets cleaned up nicely
super.close();
// remove previously added function
JavaScript.getCurrent().removeFunction(CLOSE_WINDOW_FUNCTION);
}
}

2)用法:

public class MyVaadinUI extends UI {
@Override
protected void init(VaadinRequest request) {
new MyHtmlWindow().show(this);
}
}

3) 结果:

JS Callback window

附注:欢迎提出改进建议

关于javascript - 如何在 vaadin 对话框中启用 javascript window.close()?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38830141/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com