我有一个inputSwitch,它需要显示一个对话框来确认或不确认操作。如果我打开或关闭它,对话框应该会显示一条变量消息。我做了下一件事,但问题是当我按下no并切换开关让它进入原始状态时,它再次调用ajax事件并再次打开对话框。如何才能仅在单击时启动事件,而不是在调用.toggle函数时启动事件?
<p:outputLabel for="activa"
value="#{bundleBean.getValue('activa')}" />
<p:inputSwitch id="activa"
value="#{gestionBean.domainEntity.activa}" onLabel="Si"
offLabel="No"
widgetVar="switch#{gestionBean.domainEntity.id}">
<p:ajax event="change" update="test" oncomplete="PF('dialog#{gestionBean.domainEntity.id}').show()"></p:ajax>
</p:inputSwitch>
<div>
<p:confirmDialog id="test"
widgetVar="dialog#{gestionBean.domainEntity.id}"
styleClass="content-confirmation-dialog"
message="#{gestionBean.mensajeDialogoActiva(gestionBean.domainEntity.activa)}"
header="#{bundleBean.getValue('confirmacion')}" closable="false">
<p:commandButton value="Si"
styleClass="boton rounded-button ui-button-danger float-right ui-confirmdialog-yes" immediate="true"
onclick="PF('dialog#{gestionBean.domainEntity.id}').hide()" />
<!-- action="#{gestionBean.cambiarEstado(gestionBean.domainEntity)}" -->
<p:commandButton value="No" styleClass="boton rounded-button ui-button ui-confirmdialog-no"
immediate="true"
update="activa"
onclick="PF('switch#{gestionBean.domainEntity.id}').toggle(); PF('dialog#{gestionBean.domainEntity.id}').hide()" />
</p:confirmDialog>发布于 2021-03-18 02:28:16
当您单击No按钮时,您可以更新表单,而不是switch,对话框将不会显示。
<p:commandButton update="@from">发布于 2021-03-18 05:02:05
解决此问题的另一种方法是取消p:confirmDialog,让p:inputSwitch / p:toggleSwitch上的更改事件在隐藏的commandButton上触发单击事件。例如:
<p:inputSwitch id="toggle" value="#{toggleSwitchController.toggleOn}" >
<p:ajax oncomplete="PF('hidnBtn').getJQ().click();"/>
</p:inputSwitch>
<p:commandButton id="hiddenBtn" widgetVar="hidnBtn"
action="#{toggleSwitchController.toggleChanged}"
style="visibility: hidden;">
<p:ajax event="dialogReturn" update=":frm1:growl toggle"
listener="#{toggleSwitchController.handleReturn}" />
</p:commandButton>隐藏的p:commandButton调用的操作可以测试切换的值,如果设置为true/yes,则使用PrimeFace的对话框框架显示确认对话框。该对话框可以返回一个布尔值,可以在handleReturn函数中对其进行测试。
public void toggleChanged() {
if (toggleOn) {
Map<String, Object> options = new HashMap<>();
options.put("resizable", false);
options.put("draggable", false);
options.put("closable", false);
options.put("modal", true);
PrimeFaces.current().dialog().openDynamic("/pages/pleaseConfirm", options, null);
}
}
public void handleReturn(SelectEvent<Boolean> event) {
boolean confirmed = event.getObject();
if (confirmed) {
facesContext.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "confirmed", null));
} else {
facesContext.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "not confirmed", null));
toggleOn = false;
}
}您可以通过openDynamic()调用为文本字符串提供参数,从而提高"pleaseConfirm“对话框的可重用性。
发布于 2021-03-18 18:56:25
这个问题可以通过以下方法解决:向ajax change事件添加侦听器,使侦听器返回切换的当前值,并在显示对话框之前测试onComplete子句中的返回值。
<p:inputSwitch id="activa"
value="#{toggleSwitchController.toggleOn}"
onLabel="Si" offLabel="No"
widgetVar="toggleWidget" >
<p:ajax event="change"
listener="#{toggleSwitchController.testToggle}"
update="test"
oncomplete="if ( args.toggleValue == 1 ) { PF('dlgWidget').show();}" />
</p:inputSwitch>监听器可以非常简单:
public void testToggle() {
PrimeFaces.current().ajax().addCallbackParam("toggleValue", (toggleOn ? 1 : 0));
}https://stackoverflow.com/questions/66672365
复制相似问题