首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用confirmDialog的JSF inputSwitch

使用confirmDialog的JSF inputSwitch
EN

Stack Overflow用户
提问于 2021-03-17 19:41:11
回答 3查看 135关注 0票数 1

我有一个inputSwitch,它需要显示一个对话框来确认或不确认操作。如果我打开或关闭它,对话框应该会显示一条变量消息。我做了下一件事,但问题是当我按下no并切换开关让它进入原始状态时,它再次调用ajax事件并再次打开对话框。如何才能仅在单击时启动事件,而不是在调用.toggle函数时启动事件?

代码语言:javascript
复制
                <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>
EN

回答 3

Stack Overflow用户

发布于 2021-03-18 02:28:16

当您单击No按钮时,您可以更新表单,而不是switch,对话框将不会显示。

代码语言:javascript
复制
<p:commandButton update="@from">
票数 0
EN

Stack Overflow用户

发布于 2021-03-18 05:02:05

解决此问题的另一种方法是取消p:confirmDialog,让p:inputSwitch / p:toggleSwitch上的更改事件在隐藏的commandButton上触发单击事件。例如:

代码语言:javascript
复制
            <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函数中对其进行测试。

代码语言:javascript
复制
    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“对话框的可重用性。

票数 0
EN

Stack Overflow用户

发布于 2021-03-18 18:56:25

这个问题可以通过以下方法解决:向ajax change事件添加侦听器,使侦听器返回切换的当前值,并在显示对话框之前测试onComplete子句中的返回值。

代码语言:javascript
复制
            <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>

监听器可以非常简单:

代码语言:javascript
复制
    public void testToggle() {
        PrimeFaces.current().ajax().addCallbackParam("toggleValue", (toggleOn ? 1 : 0));
    }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66672365

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档