I'm trying to iterate through a List of Map items, i.e. an ArrayList of HashMaps or something similar, and I'm trying to do this in primefaces datatable. This is basically what I'm trying to do:
<body>
<h:form>
<p:dataTable value="#{customerBean.list}" var="map">
<c:forEach items="#{map}" var="entry">
<p:column headerText="#{entry.key}">
#{entry.value}
</p:column>
</c:forEach>
</p:dataTable>
</h:form>
</body>
In this case, customerBean.list
is a List<Map<String, String>>
and entry is a Map<String, String>
.
What I want to do, is create a dynamic amount of columns, based on the amount of entries in a Map<String, String>
while using the map entry's key as a header name, and the value as the output. The c:forEach
thing seems to work fine when I'm using a hardcoded Map<String, String>
, but apparently it can't loop through the var of the p:dataTable
. I assume that the program takes precaution to avoid having to loop through Maps of different sizes. So how can I make this work anyway? How can I create an arbitrary amount of columns based on the amount of entries in a Map? Note that I'm a 100% certain that every Map<String, String>
is of equal size in my List<Map<String, String>>
EDIT:
Here's my bean source. The code works fine and everything, the problem is just with the loop not willing to go through my map:
@ManagedBean
@SessionScoped
public class CustomerBean {
private List<Map<String, String>> list = new ArrayList<Map<String, String>>();
private Mapper mapper = new Mapper();
public CustomerBean() {
list = mapper.all(); //gets data from database
}
public List<Map<String, String>> getList() {
return list;
}
public void setList(List<Map<String, String>> list) {
this.list = list;
}
}