简体   繁体   English

在JSF和ui中嵌套ajax:重复

[英]Nested ajax in JSF and ui:repeat

<h:form>
    <fieldset>
        <h:selectManyListbox id="listbox" value="#{form.items}">
            <f:selectItems value="#{form.allItems}">
        </h:selectManyListbox>
    </fieldset>
    <h:commandButton value="Get Table" action="#{form.getTable}">
        <f:ajax render="result_table" execute="listbox" />
    </h:commandButton>
    <h:panelGrid id="result_table">
        <table>
            <thead></thead>
            <tbody>
                <ui:repeat var="table" value="#{form.resultTable}">
                    <tr>
                         <td>#{table.name}</td>
                         <td>
                             <h:selectBooleanCheckbox binding="#{showMore}">
                                 <f:ajax render="inner" />
                             </h:selectBooleanCheckbox>
                         </td>
                    </tr>
                    <tr>
                         <td>
                             <h:panelGrid id="inner" rendered="#{not empty showMore.value and showMore.value}">
                                 <table></table>
                             </h:panelGrid>
                         </td>
                    </tr>
                </ui:repeat>
            </tbody>
        </table>
    </h:panelGrid>
</h:form>     

I followed the checkbox sample code from BalusC here . 我在这里按照BalusC的复选框示例代码。

When I click the command button, based on the list box values, it generates a table result which I display in h:panelGrid id="result_table" . 当我单击命令按钮时,根据列表框值,它会生成一个表格结果,我在h:panelGrid id="result_table"显示h:panelGrid id="result_table" This command button works. 此命令按钮有效。 Inside the generated table, I have some sub-tables I want to display, but only if I check the box to show them. 在生成的表中,我有一些我想要显示的子表,但只有在我选中该框才能显示它们。 For the checkbox if I use render="@form" it collapses my outer parent table. 对于复选框,如果我使用render="@form"它会折叠我的外部父表。 If I use render="inner" checking/unchecking box does nothing. 如果我使用render="inner"检查/取消选中框什么都不做。

How do I get this to work? 我如何让它工作? Is it because the two ajax are in conflict? 是因为两个ajax发生了冲突吗?

FYI, I also tried using a bean property instead but got the same result. 仅供参考,我也尝试使用bean属性,但得到了相同的结果。 When I tried using the javascript method mentioned in the same link, but checking/unchecking the box has no effect. 当我尝试使用同一链接中提到的javascript方法时,但检查/取消选中该框没有任何效果。

Updated to add example snippet and <ui:repeat></ui:repeat> above: 已更新,可在上方添加示例代码段和<ui:repeat></ui:repeat>

<h:selectBooleanCheckbox binding="#{showMore}">
     <f:ajax render="inner" />
 </h:selectBooleanCheckbox>
 <h:panelGrid id="inner">
     <h:outputText value="always" />
     <h:outputText value="hidden" rendered="#{not empty showMore.value and showMore.value}" />
 </h:panelGrid>

The culprit is here: 罪魁祸首在这里:

<h:panelGrid id="inner" rendered="#{not empty showMore.value and showMore.value}">
    <table></table>
</h:panelGrid>

The f:ajax uses JavaScript to locate the element-to-be-rendered in the client side. f:ajax使用JavaScript来定位客户端要呈现的元素。 JavaScript won't be able to locate the HTML representation of the inner panelgrid when it's not rendered to the client side by JSF. 当JSF未将其呈现给客户端时,JavaScript将无法找到inner panelgrid的HTML表示。 Put the inner id in a parent component instead which is always rendered to the client side, so that Ajax/JavaScript can locate it regardless of the rendered condition. inner id放在父组件中,而不是总是呈现给客户端,这样无论rendered条件如何,Ajax / JavaScript都可以找到它。 Eg: 例如:

<h:panelGroup id="inner">
    <h:panelGrid rendered="#{not empty showMore.value and showMore.value}">
        <table></table>
    </h:panelGrid>
</h:panelGroup>

Update : as per the comments, using the ui:repeat should not form a problem here. 更新 :根据评论,使用ui:repeat不应该在这里形成问题。 Even more, to be sure I have copypasted your example to my playground environment and it works fine (using Mojarra 2.0.3 on Tomcat 6.0.29). 更重要的是,为了确保我已经将你的例子复制到我的游乐场环境中并且工作正常(在Tomcat 6.0.29上使用Mojarra 2.0.3)。 The bean is by the way marked @ViewScoped . 这个bean标记为@ViewScoped Maybe yours was @RequestScoped and the loading of the value for the ui:repeat was based on some request scoped condition which wasn't retained in the subsequent ajax calls? 也许你的是@RequestScoped并且ui:repeat的值的加载是基于一些请求范围的条件,这个条件在后续的ajax调用中没有保留?

See also: 也可以看看:

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

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