jsfrichdatatable

jsf working with collection/map


Can I bind my h:dataTable/rich:dataTable with some Map? I found that h:dataTable can work only with List object and deleting in List can be very heavy.


Solution

  • Yes, that's right. dataTable, ui:repeat and friends only work with Lists.

    You can just add a managed bean method that puts map.keySet() or map.values() into a list depending on which you want to iterate over.

    Typically when I want to iterate a map from a JSF view, I do something like

    <h:dataTable value="#{bean.mapKeys}" var="key"> 
       <h:outputText value="#{bean.map[key]}"/> 
    </h:dataTable>
    

    with managed bean property

    class Bean {
       public List<T> mapKeys() {
         return new ArrayList<T>(map.keySet());
       }
    } 
    

    or something like that.

    Of course, this makes the most sense if you're using something like TreeMap or LinkedHashMap that preserves ordering.