【问题标题】:Re-render only a part of the panel仅重新渲染面板的一部分
【发布时间】:2012-11-22 10:03:18
【问题描述】:

这是我的问题的简化版本。

<h:form id="form1">
  <a4j:commandButton value="Ok" reRender="panel_1"/>

  <a4j:outputPanel id="panel_1" layout="block" style="height:100px;border:solid 1px;">
    Content here should be reRendered

    <a4j:outputPanel id="panel_2" layout="block" style="height:50px;border:solid green;color:green;">
      Content here should not be reRendered  
    </a4j:outputPanel>

  </a4j:outputPanel>
</h:form>

当用户单击&lt;a4j:commandButton&gt; 时,应重新渲染第一个&lt;a4j:outputPanel&gt; (panel_1)。但是第二个&lt;a4j:outputPanel&gt; 里面的内容不应该被重新渲染。
这可能吗? (至少通过将&lt;a4j:outputPanel&gt; 更改为另一个组件。)

【问题讨论】:

    标签: richfaces jsf-1.2 ajax4jsf


    【解决方案1】:

    如何将您想要重新呈现的内容包装在另一个面板中?像这样:

    <h:form id="form1">
      <a4j:commandButton value="Ok" reRender="panel_1_a"/>
      <a4j:outputPanel id="panel_1" layout="block" style="height:100px;border:solid 1px;">
    
        <a4j:outputPanel id="panel_1_a">
          Content here should be reRendered
        </arj:outputPanel>
    
        <a4j:outputPanel id="panel_2" layout="block" style="height:50px;border:solid green;color:green;">
          Content here should not be reRendered  
        </a4j:outputPanel>
    

    或者,您可以将panel_2 移动到其他地方。

    【讨论】:

    • 实际上在我的情况下这不适用。在我的情况下,当重新渲染父级时,它会将其样式更改为display:none。然后孩子也变得隐藏起来。但是由于孩子也被重新渲染,所以用户在子面板中输入的所有数据都将丢失。我可以在所有输入字段中使用a4j:supports 来防止这种情况。但我正在寻找更清洁的解决方案。
    • @prageeth 根据您对面板的编码方式,panel_2 消失是正常的,因为您正在重新渲染整个 panel_1(包括 panel_2):如果父组件被隐藏,那么它们的孩子们是。我认为panel_1panel_2 都应该是独立的,我不知道任何适合您的代码的解决方案。
    【解决方案2】:

    您可以使用 MyFaces 改进 Makhiel 解决方案,它允许您定义子表单来包装一些内容以进行部分验证和模型更新。

    MyFaces 还允许您通过属性 actionFor=""&lt;t:commandButton&gt; 附加到特定的 &lt;t:subform&gt;

    你需要包含 tomahawk 库才能使用它的标签:

    <%@ taglib prefix="t" uri="http://myfaces.apache.org/tomahawk"%>
    

    然后可以用这种方式重写 Makhiel 的代码(我正在移动 &lt;a4j:outputPanel&gt; 内的按钮,以便将其放入 &lt;t:subform&gt; 标记内。

    <h:form id="form1">
        <a4j:outputPanel id="panel_1" layout="block" style="height:100px;border:solid 1px;">
            <t:subform id="FirstPanelForm">
                <t:commandButton value="Ok" reRender="panel_1_a" actionFor="FirstPanelForm"/>
                <a4j:outputPanel id="panel_1_a">
                    Content here should be reRendered
                </a4j:outputPanel>
            </t:subform>
            <a4j:outputPanel id="panel_2" layout="block" style="height:50px;border:solid green;color:green;">
                Content here should not be reRendered  
            </a4j:outputPanel>
        </a4j:outputPanel>
    </h:form>
    

    我不知道这是否是您正在寻求的行为。如果没有,请坚持 subform 的想法并尝试在您的代码中使用它。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-06-20
      • 2011-05-23
      • 2010-12-18
      • 2020-01-06
      相关资源
      最近更新 更多