Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Display key and value Map in primefaces datatable

In my Java EE 6 / JSF 2 project I have a Map property advantages in the Employee entity:

@ElementCollection
private Map<String, Float> advantages;

The key represents the advantage name and the value represents the cost associated with the advantage name. This is mapped to a table with three columns Employee_Id, Advantages and Advantages_Key. I need to display all map entries in my <p:dataTable> which shows a List<Employee>. How can I achieve this?

like image 383
Reem Avatar asked Dec 02 '13 10:12

Reem


2 Answers

Provided that your environment supports EL 2.2 (Java EE 6 does), and that #{employee} in the below example is coming from <p:dataTable var>, then this should do

<ui:repeat value="#{employee.advantages.entrySet().toArray()}" var="entry">
    Name: #{entry.key}, Cost: #{entry.value}
</ui:repeat>
like image 179
BalusC Avatar answered Nov 18 '22 23:11

BalusC


this works for me, and this is my map in bean Map:

<p:dataTable id="dtbAddedRoles" value="#{controllerUserCreationMain.mapUtil.entrySet().toArray()}"
                                var="roleAdded">
                                <p:column headerText="Role">
                                    <p:outputLabel value="#{roleAdded.key.roleName}" />
                                </p:column>
                            </p:dataTable>
like image 34
Ozwald Tutillo Avatar answered Nov 19 '22 00:11

Ozwald Tutillo