简体   繁体   English

JSF,AJAX,使用selectOneMenu更新动态表

[英]JSF, AJAX, updating dynamic table with selectOneMenu

I'm quite new to jsf, and I stuck in updatig table with ajax. 我对jsf相当陌生,并且我使用ajax停留在updatig表中。

I need to have a table working toghther with select menu, the table needs to be refreshed onchange. 我需要一个与选择菜单一起使用的表,该表需要在更改时刷新。 The table's columns depend on the menu value, columns number can be different for each value. 该表的列取决于菜单值,每个值的列号可以不同。

My code: 我的代码:

<h:form>
        <h:selectOneMenu id="selectOneMenu"  value="#{reservationGuestBean.currentPremiseId}" >
            <f:selectItems value="#{reservationGuestBean.premiseNames}" var="p" itemLabel="#{p.label}" itemValue="#{p.value}" />
            <a4j:ajax event="change" listener="#{reservationGuestBean.premiseChanged}" ajaxSingle="true" render=":currentPremiseId, :reservationsHeading, :reservationsBody" />
        </h:selectOneMenu> 
    </h:form>
    <div><h:outputText id="currentPremiseId" value="PremiseId: #{reservationGuestBean.currentPremiseId}" /></div>
    <table id="reservationsTable">
        <tr>
            <ui:repeat id="reservationsHeading" var="col" value="#{reservationGuestBean.reservationTableHeading}">
                <th>#{col}</th>
            </ui:repeat>
        </tr>
        <ui:repeat id="reservationsBody" var="row" value="#{reservationGuestBean.reservationTableRows}">
            <tr>
                <ui:repeat id="reservationsRowContent" var="cell" value="#{row}">
                    <td>#{cell.value}</td>
                </ui:repeat>
            </tr>
        </ui:repeat>
    </table>

With the code I'm able to reload div's content, but not the table, neither heading nor content. 使用代码,我可以重新加载div的内容,但不能重新加载表,既不加载标题也不包含内容。 In firebug I can see update requests with desired response (xml with ), but selected table elements are not updated. 在firebug中,我可以看到具有所需响应的更新请求(带有的xml),但是所选表元素未更新。

The quick and dirty answer is just to wrap your table in an h:panelGroup , and use its id on the render tag of a4j:ajax : 快速而肮脏的答案是将表包装在h:panelGroup ,并在a4j:ajax的render标签上使用其ID:

<h:panelGroup id="myTable">
    <table>
    ...
    </table>
</h:panelGroup>

Having said that, you probably want to learn to use the component h:datatable instead of redering the table yourself using ui:repeat . 话虽如此,您可能想学习使用h:datatable组件,而不是使用ui:repeat自己ui:repeat创建表。

It's easy to use, and it will be a meaningful component in the JSF View, which plain HTML table is not (and that's why you can't just rerender it in the ajax tag). 它易于使用,并且在JSF View中将是一个有意义的组件,而普通的HTML表则不是(这就是为什么您不能只在ajax标记中重新呈现它)。 You can start by doing the examples in this article . 您可以从本文中的示例开始。

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

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