简体   繁体   中英

Edit hashmap in JSF or Primefaces datatable

I have an object that represents a service provider. In that object I have this hashmap

private Map<MetaDataKeys, String> metaData;

The "MetaDatatKeys" is a enum that looks like this,

public enum MetaDataKeys {
    PROVIDER_NAME, PROVIDER_CONTACT_NAME, PROVIDER_SERVICE_RADIUS;
}

I would like to display the hashmap key/value pairs in a datatable or similar for editing, something along the lines of,

            <p:dataTable id="providerDatatable" var="infoMap" value="#{editUserBean.editUser.metaData}">
                <p:column><h:outputText value="#{infoMap.key.metaData}"/></p:column>
                <p:column><h:inputText value="#{infoMap.key.value}"/></p:column>
            </p:dataTable>

In my backing bean "editUser" is the object that contains the map.

what is the best way to go about this? I have not been successful in even getting the table to render and populate with values. In searching most examples use a string or a primitive for the key.

You should make iteration in the datatable by Map.entrySet() it's better way. If you try to do itaration by Map.keySet() and than take value by key you can take O(n^2).

For your task realisation looks like:

<p:dataTable id="providerDatatable" var="infoMap" value="#{editUserBean.editUser.metaData.entrySet()}">
    <p:column><h:outputText value="#{infoMap.key}"/></p:column>
    <p:column><h:inputText value="#{infoMap.value}"/></p:column>
</p:dataTable>

To edit table row you can you use p:cellEditor more info about this component you can find at the Primefaces show case

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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