2014-04-25 46 views
2

的变化事件AJAX请求之前我有一个primefaces selectOneMenu用于对象和下方,其具有基于所述selectOneMenu用于有条件值呈现多个面板的面板。所以我在SelectOneMenu中包含了primefaces p:ajax请求。 ajax请求正确触发,面板显示正常,没有任何问题。确认对话框呼吁selectOneMenu用于

但现在我要他们改变了selectOneMenu用于继续进行Ajax请求,警告他们,他们在面板中输入的任何值都将丢失之前添加的确认。所以我添加了一个p:ajax的onstart事件,并包含一个javascript函数,它具有javascript确认。问题是,当用户选择“取消”按钮,阿贾克斯在不触发,但我selectOneMenu用于有选择的新的价值。我想在click事件中调用ajax请求。但是PrimeOne 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用于显示其旧的价值?我想甚至使用下面的JavaScript代码在JSF SelectOneMenu中使用f:ajax。它要求确认,但我在确认框中按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; 

} 

回答

6

您可以使用<号码:confirmDialog >组件,并与selectOneMenu用于widgetVar结合起来。只是删除<号码:AJAX >,把你的行动是按钮确认对话框。如果您单击是,则会执行操作。如果没有更改将被恢复。

<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> 
+0

简单和容易 –

3

在阐述@bhdrk答案:

不要忘记调用PF( '<部件名称>')来获取你的小部件: onchange="PF('confirm').show()"onclick="PF('ps').selectValue(PF('ps').preShowValue.val());PF('confirm').hide()"