问题描述
我在那里遇到一点问题。当我想在JSF中填充DataTable时,我必须先创建一个模型,然后在我的视图中使用它。与Primefaces样本一样。
I've a little problem right there. When I want to populate DataTable in JSF I have to create a model first, then use it in my view. Like on Primefaces sample here.
现在,我必须创建将显示来自webservice的数据的DataTable。我不知道会有多少列,而且我不知道他们的名字......你能推荐一些明智的解决方案吗?
And now, I have to create DataTable that will display data, that came from webservice. I don't know how many columns there will be, and I don't know their names... Could you recommend some wise solution ?
PS。我不知道如何从webservice返回数据 - 它仍然需要确定。
PS. I don't know also how to returned data from webservice - it is still to determine.
编辑
public Bean() {
columns = new ArrayList<String>();
rows = new ArrayList<Map<String, Object>>();
populateColumns(columns,4);
for(int i = 0 ; i < 6 ; i++)
{
Map<String,Object> m = new HashMap<String,Object>();
m.clear();
for(int j = 0 ; j < 4 ; j++)
{
m.put("Column" + j, "sth" + j + i);
}
rows.add(m);
}
}
private void populateColumns(List<String> list, int size) {
for(int i = 0 ; i < size ; i++)
list.add("Column" + i);
}
推荐答案
收集数据列表< Map< String,Object>>
表示行
属性。 Map
表示按列名称键入的列(如果需要,只需自动生成,例如 column1
, column2
, column3
等由column+ i
)。将这些列名称收集在单独的列表< String>
中,该列表示列
属性。最后通过< p:columns>
显示如下:
Collect the data in a List<Map<String, Object>>
which represents the rows
property. The Map
represents the columns, keyed by a column name (if necessary, just autogenerated such as column1
, column2
, column3
, etc by "column" + i
). 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>
这篇关于在JSF2.0中动态创建和填充DataTable的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!