问题描述
我有一个小问题.当我想在 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.
现在,我必须创建 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 ?
附注.我也不知道如何从 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);
}
推荐答案
在一个List>
中收集数据,代表rows
财产.Map
代表列,以列名称为键(如有必要,只需自动生成,例如 column1
、column2
、column3 等通过
"column" + i
).将这些列名称收集在单独的 List
中,代表 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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!