p:commandButton 不显示 p:dialog

p:commandButton doesn't dislpay p:dialog

我在单击时显示对话框时遇到问题。这是一个明显的问题,但我无法发现错误。我已经坚持了好几天了,这太疯狂了。你能帮帮我吗

<h:form id="form">

<p:commandButton
    rendered="#{characterBean.characterSession.characterName ne null}"
    value="#{characterBean.characterSession.title.titleName}"
    icon="fa fa-fw fa-edit" onclick="PF('dlg').show();"
    update="@form"/>

<p:dialog id="titleDetail" header="#{i18n['title.yourTitles']}"
    widgetVar="dlg" dynamic="true" closable="false" resizable="false"
    showEffect="fade" hideEffect="fade">
    <h:panelGroup>
        <p:messages autoUpdate="true" />
        <h:selectOneMenu id="titleSelect" converter="#{titleConverter}"
            value="#{characterBean.characterSession.title}">
            <f:selectItems value="#{characterBean.titleUnlocked}" var="t"
                itemValue="#{t}" itemLabel="#{t.titleName}" />
        </h:selectOneMenu>
        <hr />
        <h:panelGrid columns="2" style="width: 100%; text-align:center">
            <p:commandButton value="#{i18n['general.submit']}"
                icon="fa fa-check"
                actionListener="#{characterBean.updateCharacterTitle}"
                oncomplete="PF('dlg').hide();" update="@form" />

            <p:commandButton value="#{i18n['general.cancel']}"
                icon="fa fa-close" action="#{characterBean.submitCancel}"
                oncomplete="PF('dlg').hide();" update="@form" process="@this" />
        </h:panelGrid>
        <p:remoteCommand name="updateForm()" process="@this" update="@form" />
    </h:panelGroup>
</p:dialog>

</h:form>

核心问题本质上是这样的:

<h:form>
    <p:commandButton onclick="PF('dlg').show();" update="@form"/>

    <p:dialog widgetVar="dlg">
        ...
    </p:dialog>
</h:form>
  • <p:dialog> 的默认状态是隐藏的。
  • onclick 显示对话框。
  • update更新了<h:form>的全部内容。
  • <p:dialog> 也包含在更新中。
  • 所以,<p:dialog> 又被隐藏了。

有几种解决方法:

  1. 不要让 update 包含 <p:dialog>

    <h:form>
        <h:panelGroup id="outsideDialog">
            <p:commandButton onclick="PF('dlg').show();" update="outsideDialog"/>
        </h:panelGroup>
    
        <p:dialog widgetVar="dlg">
            ...
        </p:dialog>
    </h:form>
    
  2. update.

    运行 之后将 onclick 替换为 oncomplete
    <h:form>
        <p:commandButton update="@form" oncomplete="PF('dlg').show();" />
    
        <p:dialog widgetVar="dlg">
            ...
        </p:dialog>
    </h:form>
    
  3. <p:dialog> 移到 <h:form> 之外并赋予它自己的 <h:form>.

    <h:form>
        <p:commandButton update="@form :dlg" oncomplete="PF('dlg').show();" />
    </h:form>
    
    <p:dialog id="dlg" widgetVar="dlg">
        <h:form>
            ...
        </h:form>
    </p:dialog>
    

    或者,取决于您是否确实需要更新对话框的内容

    <h:form>
        <p:commandButton onclick="PF('dlg').show();" update="@form" />
    </h:form>
    
    <p:dialog widgetVar="dlg">
        <h:form>
            ...
        </h:form>
    </p:dialog>
    

推荐的解决方案是3。

另请参阅:

  • Execution order of events when pressing PrimeFaces p:commandButton
  • How to use <h:form> in JSF page? Single form? Multiple forms? Nested forms?
  • p:commandbutton action doesn't work inside p:dialog