【问题标题】:detectUnsavedChanges on dialogReturn event在 dialogReturn 事件上检测UnsavedChanges
【发布时间】:2017-01-30 14:56:29
【问题描述】:

在我看来,我有一个禁用的p:inputText 显示一些地址信息。 p:inputText 的值由dialogReturn 监听器onAdresseDialogReturn() 设置。除了这个 inputText 我还有其他几个输入字段(p:selectOneMenup:inputText)。

每当我更改其他输入组件的某些值并尝试离开页面时,我都会收到来自detectUnsavedChanges JavaScript 的警告对话框。

我现在的问题是,当dialogReturn 侦听器更改 inputText 字段的值时,我也想要此警告消息 - 但这不会发生!

所以我的问题是,我怎样才能做到这一点?

这里p:inputTextp:commandButton 使用primefaces 对话框框架:

<ui:composition>

    <ui:define name="head">
        <h:outputScript name="js/detectUnsavedChanges.js"/>
    </ui:define>

    <ui:define name="content">
        <h:form id="form">
            <p:panelGrid>
                <p:row>
                    <p:column>
                        <p:commandButton id="save" value="Save" ajax="false" action="#{bean.save}"/>
                    </p:columns>
                </p:row>
            </p:panelGrid>

            <p:panelGrid>
                <p:row>
                    <p:column>
                        <p:inputText id="adresse" disabled="true" size="50"
                                     value="#{bean.adresse}"/>
                        <p:commandButton id="adresseAuswahl" 
                                         title="Adresse bearbeiten"
                                         process="@this"
                                         actionListener="#{bean.showAdresseDialog}">
                            <p:ajax event="dialogReturn" update="adresse"
                                    listener="#{bean.onAdresseDialogReturn}"/>
                        </p:commandButton>  
                    </p:columns>
                </p:row>
            </p:panelGrid>
        </h:form>
    </ui:define>
</ui:composition>

这里是我的 bean 中的 dialogReturn 监听器:

public void onAdresseDialogReturn(SelectEvent e) {
    final Adresse adresse = (Adresse) e.getObject();
    boolean changedAdresse = Optional.ofNullable(adresse).isPresent() 
            && !getAdresse().equals(adresse);
    if (changedBauvorhabenAdresse) {
        // Adresse has been changed
        setAdresse(adresse);
    }
}

这里是 BalusC 从How to detect unsaved data in form when user leaves the page? 提到的 JavaScript 代码:

$(function() {
    // Set the unload message whenever any input element get changed.
    $(':input').on('change', function() {
        setConfirmUnload(true);
    });

    // Turn off the unload message whenever a form get submitted properly.
    $('form').on('submit', function() {
        setConfirmUnload(false);
    });
});

function setConfirmUnload(on) {
    var message = "You have unsaved data. Are you sure to leave the page?";
    window.onbeforeunload = (on) ? function() { return message; } : null;
}

我的环境:Wildfly-10.0.0.Final 上的 Primefaces 6.0

欢迎任何提示 - 谢谢!

【问题讨论】:

    标签: jsf primefaces


    【解决方案1】:

    尝试将oncomplete 属性添加到p:ajax 内的commandButton。 我实际上不知道您是否可以将p:ajax 组件放在p:commandButton 中,但是在 oncomplete 属性中您应该像这样调用 setConfirmUnload(true):

                       <p:commandButton id="adresseAuswahl" 
                                         title="Adresse bearbeiten"
                                         process="@this"
                                         actionListener="#{bean.showAdresseDialog}">
                            <p:ajax event="dialogReturn" update="adresse"
                                    listener="#{bean.onAdresseDialogReturn}" oncomplete="setConfirmUnload(true)"/>
                        </p:commandButton>
    

    【讨论】:

    • p:commandButton 在 PrimeFaces 展示和文档中提到的默认情况下已经 ajaxified
    • 我是这么认为的。我会在 dialog 组件中使用 p:ajax 组件来监听 close 事件。像这样:&lt;p:dialog&gt; &lt;p:ajax event="close" update="adresse" listener="#{bean.onAdresseDialogReturn}" oncomplete="setConfirmUnload(true)"/&gt; &lt;/p:dialog&gt;
    • 但现在确认对话框总是出现——即使changedBauvorhabenAdresse 在我的onAdresseDialogReturn() 监听器中为假。我试过oncomplete="setConfirmUnload(#{bean.changedBauvorhabenAdresse})"changedBauvorhabenAdresse 的getter 在页面创建期间只调用一次,而不是在ajax 事件发生时调用!我该如何解决这个问题?
    • 是的,它只在 JSF 创建 HTML 页面并评估所用组件的客户端属性时调用一次。
    • 将按钮的 id 添加到 ajax 更新 (update="adresse adresseAuswahl") 重新评估 changedBauvorhabenAdresse 并按预期显示确认对话框 - 谢谢!
    猜你喜欢
    • 2014-01-22
    • 2014-03-19
    • 2014-09-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-12
    • 1970-01-01
    相关资源
    最近更新 更多