我有一个primefaces SelectOneMenu对象和一个面板,它下面有多个面板,这些面板根据SelectOneMenu值有条件地呈现。因此,我在SelectOneMenu中包含了一个primefaces p:ajax请求。ajax请求被正确触发,面板被正确显示,没有任何问题。
但现在我想在他们更改SelectOneMenu以继续ajax请求之前添加一个确认,以警告他们在面板中输入的任何值都将丢失。因此,我添加了一个p:ajax的onstart事件,并包含了一个javascript函数,该函数具有javascript confirm。问题是,当用户选择"Cancel“按钮时,ajax不会触发,但我的SelectOneMenu选择了新值。我想过在click事件中调用ajax请求。但是primefaces SelectOneMenu没有点击事件。
JSF代码:
<p:selectOneMenu id="methodOfId" value="#{cc.attrs.comboBoxValue}">
<f:selectItem itemValue="-1" itemLabel=""/>
<f:selectItems value="#{cc.attrs.comboBoxOptions}" var="methodOfId" itemValue="#{methodOfId.id}" itemLabel="#{methodOfId.name}"/>
<f:param name="cid" value="#{cc.attrs.beanConversationID}" />
<p:ajax update="dynamicPanels" process="@this" onstart="return confirmMethodOfIdChange()"/>
</p:selectOneMenu>
<p:panel id="dynamicPanels">
<ui:include src="#{cc.attrs.panelSrc}" />
</p:panel>
Javascript代码:
function confirmMethodOfIdChange() {
var userResponse = confirm("Are you sure you want to change your selection?");
return userResponse;
}
现在,如何更改SelectOneMenu以显示其旧值?我甚至想过在jsf SelectOneMenu中使用f:ajax,使用下面的javascript代码。它要求确认,但我在确认框中点击OK,它不执行ajax请求。
function confirmMethodOfIdChange(data) {
alert("inside confirm");
var ajaxStatus = data.status; // Can be "begin", "success" and "complete"
if (ajaxStatus == 'begin'){
var userResponse = confirm("Are you sure you want to change your selection?");
if (userResponse) {
return true;
} else {
return false;
}
}
return true;
}
发布于 2014-04-25 23:27:52
您可以使用组件并将其与selectOneMenu widgetVar结合使用。只需移除并将您的操作置于确认对话框中的是按钮。如果单击是,将执行操作。如果不是,更改将被恢复。
<p:selectOneMenu widgetVar="ps" onchange="confirm.show()">
<f:selectItem itemValue="1" itemLabel="1"/>
<f:selectItem itemValue="2" itemLabel="2"/>
<f:selectItem itemValue="3" itemLabel="3"/>
</p:selectOneMenu>
<p:confirmDialog widgetVar="confirm" message="Are you sure you want to change your selection?" header="WARN!" severity="alert">
<p:commandButton value="Yes" process="@form" update="myform" oncomplete="confirm.hide()" />
<p:commandButton value="No" type="button"
onclick="ps.selectValue(ps.preShowValue.val());confirm.hide()" />
</p:confirmDialog>
发布于 2015-10-19 15:47:26
详细说明@bhdrk的答案:
别忘了调用PF('')来获取你的小部件:onchange="PF('confirm').show()"
,onclick="PF('ps').selectValue(PF('ps').preShowValue.val());PF('confirm').hide()"
等。
https://stackoverflow.com/questions/23300406
复制相似问题