简体   繁体   English

对话框上另一个组件中的Primefaces Ajax更新组件

[英]Primefaces ajax update component in another component on dialog

When using ajax inside selectOneMenu to update another selectOneMenu, the ajax feature isn't working. 在selectOneMenu内部使用ajax更新另一个selectOneMenu时,ajax功能不起作用。 What's wrong with the code? 代码有什么问题?

This code works but I don't want to update all form. 这段代码有效,但是我不想更新所有表格。

<p:dialog header="Create New Campaign" height="auto" appendTo="@(body)" modal="true" widgetVar="createCampaign" resizable="false">
        <h:form id="createCampaign">
            <h:panelGrid columns="2" cellpadding="3" cellspacing="3" style="width: 400px; max-height: 500px;" >
                <h:outputLabel value="Name:" for="Name" />
                <p:inputText  style="width:90%" id="Name" value="#{testBean.name}" title=" Name" required="true">
                </p:inputText>

                <h:outputLabel value="Assign to:" for="assignTo" />
                <p:selectOneMenu  style="width:90%" id="assignTo" value="#{testBean.city}">
                    <f:selectItem itemLabel="Choose One" itemValue="" />
                    <f:selectItems value="#{testStaticSelectBean.cities}" />
                    <p:ajax event="change" listener="#{testBean.control}" update="createCampaign"/>
                </p:selectOneMenu>

                <h:outputLabel value="Entities:" rendered="#{testBean.entityControl}"  id="entitiesLabel"/>
                <p:selectOneMenu  style="width:90%" id="entities" value="#{testBean.entity}" rendered="#{testBean.entityControl}">
                    <f:selectItem itemLabel="Choose One" itemValue="" />
                    <f:selectItems value="#{testSubSelectBean.entities}" />
                </p:selectOneMenu>                    
            </h:panelGrid>
            <br />
            <p:commandButton update="create" value="Save" icon="ui-icon-check" actionListener="#{testBean.Test}" oncomplete="window.location.reload();"/>
        </h:form>
    </p:dialog>

when city choosed only update its entities, show entities and entitiesLabel. 当城市选择仅更新其实体时,显示实体和entitysLabel。 I tried like this but does not work. 我尝试过这样,但不起作用。

<p:dialog header="Create New Campaign" height="auto" appendTo="@(body)" modal="true" widgetVar="createCampaign" resizable="false">
        <h:form id="createCampaign">
            <h:panelGrid columns="2" cellpadding="3" cellspacing="3" style="width: 400px; max-height: 500px;" >
                <h:outputLabel value="Name:" for="Name" />
                <p:inputText  style="width:90%" id="Name" value="#{testBean.name}" title=" Name" required="true">
                </p:inputText>

                <h:outputLabel value="Assign to:" for="assignTo" />
                <p:selectOneMenu  style="width:90%" id="assignTo" value="#{testBean.city}">
                    <f:selectItem itemLabel="Choose One" itemValue="" />
                    <f:selectItems value="#{testStaticSelectBean.cities}" />
                    <p:ajax event="change" listener="#{testBean.control}" update=":createCampaign:entities :createCampaign:entities"/>
                </p:selectOneMenu>

                <h:outputLabel value="Entities:" rendered="#{testBean.entityControl}"  id="entitiesLabel"/>
                <p:selectOneMenu  style="width:90%" id="entities" value="#{testBean.entity}" rendered="#{testBean.entityControl}">
                    <f:selectItem itemLabel="Choose One" itemValue="" />
                    <f:selectItems value="#{testSubSelectBean.entities}" />
                </p:selectOneMenu>                    
            </h:panelGrid>
            <br />
            <p:commandButton update="create" value="Save" icon="ui-icon-check" actionListener="#{testBean.Test}" oncomplete="window.location.reload();"/>
        </h:form>
    </p:dialog>

I assume that you already checked that testBean.control() is actually being fired. 我假设您已经检查了testBean.control()是否确实被触发了。

Since your p:selectOneMenu with id="entities" is not rendered at the moment your DOM tree is built, it is not available to be targeted by the update of the ajax, simply because it isn't there. 由于在构建DOM树时不会渲染具有id =“ entities”的p:selectOneMenu,因此更新ajax不能将其作为目标,只是因为它不存在。 You need to surround it with a container, for example an p:outputPanel and update the container instead. 您需要将其用容器包围,例如p:outputPanel并更新容器。

Something like this: 像这样:

<p:ajax event="change" listener="#{testBean.control}" update="createCampaign:panel1"/>

<p:outputPanel id ="panel1">
      <h:outputLabel value="Entities:" rendered="#{testBean.entityControl}"  id="entitiesLabel"/>
      <p:selectOneMenu  style="width:90%" id="entities" value="#{testBean.entity}" rendered="#{testBean.entityControl}">
             <f:selectItem itemLabel="Choose One" itemValue="" />
             <f:selectItems value="#{testSubSelectBean.entities}" />
       </p:selectOneMenu>   
</p:outputPanel>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM