Creating and populating a DataTable dynamically in JSF2.0

南笙酒味 提交于 2019-12-28 06:35:17

问题


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.

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. I don't know also how to returned data from webservice - it is still to determine.


EDIT

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);
}  

回答1:


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>


来源:https://stackoverflow.com/questions/13970285/creating-and-populating-a-datatable-dynamically-in-jsf2-0

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!