Dynamic columns with richfaces 4

孤者浪人 提交于 2019-12-01 05:56:01
BalusC

The <c:forEach> is indeed best what you can get. The <ui/a4j:repeat> won't work as that runs during view render time while the UIData component really needs UIColumn children, not UIRepeat children.

In order to get the <c:forEach> to work, you need to supply it a list/map of all property names (and in case of a map maybe also header labels). Here's a concrete kickoff example assuming that Item has properties id, name and value and that #{bean.itemPropertyNames} returns a List<String> with exactly those property names.

<rich:dataTable value="#{bean.items}" var="item">
    <c:forEach items="#{bean.itemPropertyNames}" var="itemPropertyName">
        <rich:column>
            #{item[itemPropertyName]}
        </rich:column> 
    </c:forEach>
</rich:dataTable>

If you need to show column headers as well, then best is to have a Map<String, String> where the key represents the property name and the value represents the header value.

<rich:dataTable value="#{bean.items}" var="item">
    <c:forEach items="#{bean.itemProperties}" var="itemProperty">
        <rich:column>
            <f:facet name="header">#{itemProperty.value}</f:facet>
            #{item[itemProperty.key]}
        </rich:column> 
    </c:forEach>
</rich:dataTable>

Either way, the only disadvantage is that the #{bean} of <c:forEach items> can in this construct not be a view scoped one. It would be recreated on every request, unless you turn off partial state saving. It needs to be a request scoped one (or session or application). Note that it does not necessarily need to be the same bean as the one in <rich:dataTable value>.

See also:

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!