【问题标题】:Values repeated in jsf <ui:repeat>jsf <ui:repeat> 中重复的值
【发布时间】:2017-06-01 14:28:18
【问题描述】:

我的表单上有重复的值。我正在使用&lt;ui:repeat&gt; 标签。

我的代码:

 <h:panelGroup id="alvs">
                                    <ui:repeat value="#{encuestaController.newEncuesta.preguntas}" var="preg">

                                            <h:outputLabel for="preg" value="add question:" style="font-weight:blue" />
                                            <p:inputText id="preg" value="#{preg.pregunta}" />
                                            <h:outputLabel for="value2" value="The question is option:" style="font-weight:bold" />
                                             <p:selectBooleanButton id="value2" value="#{preg.opcional}" onLabel="Si" offLabel="No" onIcon="ui-icon-check" offIcon="ui-icon-close" style="width:60px">
                                                <p:ajax update="f1:growl2"/>
                                            </p:selectBooleanButton>

                                                <h3 style="margin-top:2px">Question Type</h3>
                                          <p:selectOneMenu value="#{preg.tipo}">
                                            <f:selectItem itemValue="1" itemLabel="one option" />
                                            <f:selectItem itemValue="2" itemLabel="many option" />          
                                         </p:selectOneMenu>


                                        <h:panelGroup id="aux">
                                            <ui:repeat value="#{preguntaController.newPregunta.opciones}" var="opc">
                                                    <h:panelGroup id="pn11" rendered="#{preg.tipo eq 1}">  
                                                        uno<p:inputText value="#{opc.descripcion}"/>
                                                    </h:panelGroup>
                                                    <h:panelGroup id="pn12" rendered="#{preg.tipo eq 2}" >
                                                        dos<p:inputText value="#{opc.descripcion}"/>
                                                    </h:panelGroup>
                                                </ui:repeat>
                                        </h:panelGroup>

                                    </ui:repeat>

   </h:panelGroup>
   <p:commandButton class="btn-floating btn-large waves-effect waves-light red" actionListener="#{preguntaController.addOcion}" update="f1:growl2" value="Add" >
                <f:ajax execute="pn11 pn12" render="alvs" />  
   </p:commandButton>   

问题是当我想用&lt;p:commandButton/&gt;在问题(pregunta)中添加一个新选项(opciones)时

这为上一个问题添加了与新问题相同的选项。

也许,错误是ajax? &lt;f:ajax execute="pn11 pn12" render="alvs" /&gt;

帮帮我!!!

Image Description

【问题讨论】:

  • Veo que estás usando Materialize, así que te advierto que tengas cuidado con el funcionamiento de los components puesto que al hacer un , el elemento pierde sus características de Javascript 数据来自 Materialize。 Una vez hagas los "render" tienes que llamar a las funciones de Materialize para reactivar components。 Es un consejo empírico.

标签: ajax jsf uirepeat


【解决方案1】:

在我看来,问题在于您正在使用 ui:repeat 生成多个选项,但是您为内部的每个 panelGroups 声明了相同的 id。 根据来自 var="opt" 的一些唯一值(如 id)为每个 id 添加一些自定义前缀或后缀:

<ui:repeat value="#{preguntaController.newPregunta.opciones}" var="opc">
      <h:panelGroup id="pn11#{opc.id}" rendered="#{preg.tipo eq 1}">  
          uno<p:inputText value="#{opc.descripcion}"/>
      </h:panelGroup>
      <h:panelGroup id="pn12#{opc.id}" rendered="#{preg.tipo eq 2}" >
          dos<p:inputText value="#{opc.descripcion}"/>
      </h:panelGroup>
</ui:repeat>

否则,您将继续覆盖之前的 panelGroup。

您还需要为您的 ajax 请求使用更广泛的执行范围。 我会在 ui:repeat 之外使用第一个 panelGroup:

<f:ajax execute="aux" render="alvs" />

【讨论】:

  • 嗨,@Maciej 我了解个性化前缀,但是,我如何在 id 中实现,变成不同的 id? &lt;h:panelGroup id="?????" rendered="#{preg.tipo eq 1}"&gt;
  • 我将其包含在帖子中。 。同样,这只是我的想法.. 你必须在你的'opc'对象中找到一些独特的字段才能使其工作。否则,您最终可能会重复后缀和整个想法。
  • 只是一个小改动..应该有一个哈希添加:id="pn12#{opc.id}"
  • 谢谢@Maciej!!!解决办法是&lt;ui:repeat var="preg" value="#{encuestaController.newEncuesta.preguntas}"&gt; &lt;ui:repeat var="opc" value="#{preg.opciones}" varStatus="loop"&gt;谢谢你的帮助!!!
猜你喜欢
  • 2014-02-04
  • 2011-04-12
  • 2013-03-19
  • 1970-01-01
  • 2018-07-18
  • 2023-04-07
  • 2013-01-27
  • 2011-04-30
  • 2011-04-17
相关资源
最近更新 更多