如何使用 JQuery 在 JSF 2 中打开 Modal 弹出窗口
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9434558/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to open Modal pop up in JSF 2 using JQuery
提问by user1220373
I have a requirement that in my JSF page there will be a commandButton, and on clicking that commandButton it will fire a action method which will perform some logic and based on the outcome of that logic I will have to show a pop up with two commandButton on it. I can can not use any implementation of JSF (like RichFaces, IceFaces), I need to achieve this only using JSF 2 and JQuery. Is it possible? Any ideas or thoughts will be very helpful to me. Oh another thing I need to add the button and the same logic in several pages. I have tried using the SimepleModal plug in but was not successful to come up with a smart approach.
我有一个要求,在我的 JSF 页面中会有一个 commandButton,点击那个 commandButton 它将触发一个 action 方法,该方法将执行一些逻辑,并根据该逻辑的结果,我将不得不显示一个带有两个 commandButton 的弹出窗口在上面。我不能使用 JSF 的任何实现(如 RichFaces、IceFaces),我只需要使用 JSF 2 和 JQuery 来实现这一点。是否可以?任何想法或想法都会对我很有帮助。哦另一件事我需要在几个页面中添加按钮和相同的逻辑。我曾尝试使用 SimepleModal 插件,但没有成功想出一个聪明的方法。
回答by Daniel
Here's a complete working example
这是一个完整的工作示例
I used display:none
to hide the h:panelGroup
dialog container cause the .dialog function will make it visible when its needed to be
我曾经display:none
隐藏h:panelGroup
对话框容器,因为 .dialog 函数将使其在需要时可见
You also can hide the real jsf command buttons and access it through dialog button with jquery # selector and invoke the .click on it like I did in the js file.
您还可以隐藏真正的 jsf 命令按钮并通过带有 jquery # 选择器的对话框按钮访问它,并像我在 js 文件中所做的那样调用它的 .click。
One last thing , used onclick="initDialog(); return false;"
to call the dialog js function and added return false so the commandbutton wont try to navigate away...
最后一件事,用于onclick="initDialog(); return false;"
调用对话框 js 函数并添加 return false 以便命令按钮不会尝试导航...
By using the jQuery UI Dialog you will get maximum flexibility/control over your dialogs.
通过使用 jQuery UI 对话框,您将获得对对话框的最大灵活性/控制。
The example consists from 2 file (one .xhtml and one .js) ,
该示例由 2 个文件(一个 .xhtml 和一个 .js)组成,
I used jQuery and jQueryUI , no need in any other plugins at all
我使用了 jQuery 和 jQueryUI ,根本不需要任何其他插件
index.xhtml
索引.xhtml
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core">
<f:view>
<h:head>
<script language="javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script language="javascript" src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.16/jquery-ui.min.js"></script>
<script language="javascript" src="scripts.js"></script>
<link rel="stylesheet" href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.7.2/themes/ui-lightness/jquery-ui.css" type="text/css" media="all" />
</h:head>
<h:body>
<h:panelGroup id="idOfDialogPlaceHolder" style="display:none">
<h:form prependId="false">
<h:outputText value="Some Text"></h:outputText>
<h:commandButton id="justAButton" onclick="alert('wow')" style="display:none"></h:commandButton>
</h:form>
</h:panelGroup>
<h:form prependId="false">
<h:commandButton id="dialogClickBtn" value="Click to display dialog" onclick="initDialog(); return false;"></h:commandButton>
</h:form>
</h:body>
</f:view>
</html>
and scripts.js
和scripts.js
function initDialog() {
$("#idOfDialogPlaceHolder").dialog({
modal: true,
buttons: {
SomeButton: function () {
$("#justAButton").click();
},
Close: function () {
$(this).dialog("close");
}
},
});
}
That's it
就是这样