2
votes

I am having a two level map Map<String,HashMap<String,String>> which i need to display using a <p:dataTable>. The code of managed bean is as follows:

@ManagedBean(name="MyBean")
public class MyBean{
    private Map<String,HashMap<String,String>> twoDimentionalMap;
    public void getMapData(){
       twoDimentionalMap=getDataFromDataStore();
    }

}

Now I am using this map in my view.xhtml file as follows:

<p:dataTable var="entrySet1" value="#{MyBean.twoDimentionalMap.entrySet()}">
    <p:columns var="entrySet2" value="#{entrySet1.getValue()}">
         #{entrySet2.getKey()} - #{entrySet2.getValue()}
    </p:columns>
</p:dataTable>

I also tried using

<p:dataTable var="entrySet1" value="#{MyBean.twoDimentionalMap.entrySet()}">
    <p:columns var="entrySet2" value="#{MyBean.twoDimentionalMap.get(entrySet1.getKey()).getValue()}">
         #{entrySet2.getKey()} - #{entrySet2.getValue()}
    </p:columns>
</p:dataTable>

I even tried converting the outer map to a list:

List<HashMap<String,String>> twoDimentionalMap;

However nothing is displayed on datatable. The execution shows no error but there is nothing displayed on the page.

Kindly suggest if I am doing something wrong or if <p:columns> is having any issue handling maps.

Thanks

1
You only need to display the key/value for entrySet2? - Seitaridis
Yes the actual data is stored in the inner map so I need to display only key/value for entrySet2 - Rajeev Singh
Do you need dynamic columns or you just used them as a way to iterate over the map? - Seitaridis
I need dynamic columns. As column names are also defined in database so I don't know column names before hand and thus I can't use static columns. Column names and respective data will be fetched from database at runtime and displayed in datatable. - Rajeev Singh
In the maps you use what would act as column names? The key of the outer map? - Seitaridis

1 Answers

4
votes

since the keys of outer map do not have a meaning, converting the outer map to a list is correct.

but your approach to retrieve column names from xhtml does not seem valid. you need to get them independently from the current iteration variable entrySet1, otherwise you add a third dimension to the operation, which data table cannot handle.

we need to assume that all keys are same across the listed maps.

here is the code for xhtml:

<p:dataTable var="entrySet1" value="#{testMB.twoDimensionalMap}">
    <p:columns var="keySet2" value="#{testMB.columnNames}">
         #{keySet2} - #{entrySet1[keySet2]}
    </p:columns>
</p:dataTable>

and for the bean:

@Named
@ViewScoped
public class TestMB implements Serializable {

    private List<HashMap<String,String>> twoDimensionalMap;

    public TestMB()
    {
        getMapData();
    }

    private void getMapData(){
        //twoDimentionalMap=getDataFromDataStore();
        twoDimensionalMap = new ArrayList<HashMap<String,String>>();
        twoDimensionalMap.add(new HashMap<String,String>());
        twoDimensionalMap.get(0).put("key0", "value00");
        twoDimensionalMap.get(0).put("key1", "value01");
        twoDimensionalMap.add(new HashMap<String,String>());
        twoDimensionalMap.get(1).put("key0", "value10");
        twoDimensionalMap.get(1).put("key1", "value11");
    }

    public Set<String> getColumnNames()
    {
        return twoDimensionalMap.size() > 0 ? twoDimensionalMap.get(0).keySet() : new HashSet<String>();
    }

    public List<HashMap<String, String>> getTwoDimensionalMap() {
        return twoDimensionalMap;
    }
}