如何迭代Map< String,Collection>在素数表中 [英] how to iterate Map<String,Collection> in datatable in primefaces
问题描述
将JSF 2.1与Primefaces配合使用
Using JSF 2.1 with primefaces
class FOO{
String name;
String value;
public void setName(String name){
this.name=name;
}
public String getName(){
return this.name;
}
public void setValue(String value){
this.value=value;
}
public String getValue(){
return this.value;
}
}
我有一个Map<String, List<FOO>>
标题名称应为地图的关键字.我需要创建多个列(即Map的大小),并且每个列都应具有FOO列表以在行中显示FOO.Name.
Header name should be Key of the Map. I need to create multiple columns (i.e. size of Map) and each column should have the list of FOO to display FOO.Name in rows.
例如: 如果地图的大小是4
For Example : if size of map is 4
库姆----- Key1
Coulmns-----Key1
第一列的行-针对Key1的List<FOO>
ROWs of 1st column - List<FOO>
against Key1
Coulmns ----- Key2
Coulmns-----Key2
第一列的行-针对Key2的List<FOO>
ROWs of 1st column - List<FOO>
against Key2
Coulmns ----- Key3
Coulmns-----Key3
第一列的行-针对Key3的List<FOO>
ROWs of 1st column - List<FOO>
against Key3
Coulmns ----- Key4
Coulmns-----Key4
第一列的行-针对Key4的List<FOO>
ROWs of 1st column - List<FOO>
against Key4
有人可以告诉我使用什么组件在xhtml页面中显示这种类型的输出吗?我尝试使用动态数据表创建,但无法显示出来.
Can someone tell me what component to use for displaying this type of output in xhtml page ? I have tried using dynamic datatable creation but not able to show this.
推荐答案
您的数据结构错误.将其更改为正确的数据结构.最简单的方法是在表示rows
属性的List<Map<String, Object>>
中收集数据. Map
代表以列名作为键的列.将这些列名称收集在一个单独的List<String>
中,该List<String>
表示columns
属性.最后按<p:columns>
所示:
You've there a wrong data structure. Change it to the right data structure. Easiest is to collect the data in a List<Map<String, Object>>
which represents the rows
property. The Map
represents the columns, keyed by a column name. Collect those column names in a separate List<String>
which represents the columns
property. Finally show it as follows by <p:columns>
:
<p:dataTable value="#{bean.rows}" var="row">
<p:columns value="#{bean.columns}" var="column">
#{row[column]}
</p:columns>
</p:dataTable>
如果需要,这里是将奇怪的数据结构转换为正确的数据结构的方式(并假设每个List<FOO>
具有相同的大小;整个数据结构的意义不那么明显):
Here's how you could convert the strange data structure to the right data structure, if necessary (and assuming that each List<FOO>
is of the same size; the whole data structure makes otherwise less sense):
Map<String, List<FOO>> wrongDataStructure = createItSomehow();
List<String> columns = new ArrayList<String>(wrongDataStructure.keySet()); // Note I expect LinkedHashMap ordering here.
List<Map<String, Object>> rows = new ArrayList<Map<String, Object>>();
int size = wrongDataStructure.values().iterator().next().size();
for (int i = 0; i < size; i++) {
Map<String, Object> row = new HashMap<String, Object>();
for (String column : columns) {
row.put(column, wrongDataStructure.get(column).get(i).getName());
}
rows.add(row);
}
// Now use "columns" and "rows".
这篇关于如何迭代Map< String,Collection>在素数表中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!