5

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);
}  
Marshall
  • 1,195
  • 6
  • 30
  • 47

1 Answers1

7

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>
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • great.. Thanks, but how to get column header? Here is my bean in Question's EDIT. Only headers are missing. – Marshall Dec 20 '12 at 14:08
  • 2
    Use ``. Or if you have header names in a separate `Map`, then something like `` – BalusC Dec 20 '12 at 14:12
  • Thanks again. First option works fine. But second... I declare headers like that: List> headers; Create getter and setter with public, but headerText="#{browserBean.headers[column]}" doesn't work :/ – Marshall Dec 20 '12 at 14:50
  • When I write: headerText="#{browserBean.headers[2]}" I get in column: {c2=Column name 2}, where c2 is my column id.. – Marshall Dec 20 '12 at 14:51
  • Why do the headers need to be like that? Use `Map` or `List` instead. Otherwise you need an odd `#{browserBean.headers[2][column]}`. – BalusC Dec 20 '12 at 14:52
  • @BalusC I am trying to create similar datatable from bean, this the code I have so far `table = new DataTable(); table.setVar("row"); table.setValue("#{bean.rows}"); Columns cols = new Columns(); cols.setValue("#{bean.columns}"); cols.setVar("column");` But I am not sure how to set the inner text `#{row[column]}`. Can you please help – vishnu viswanath Mar 22 '16 at 01:43
  • Well I did it by following this link :) http://gerardnico.com/wiki/language/java/dynamic_data_table – vishnu viswanath Mar 22 '16 at 02:11
  • @vishnuviswanath: bad article. Carefully read this: http://stackoverflow.com/questions/14911158/how-does-the-binding-attribute-work-in-jsf-when-and-how-should-it-be-used – BalusC Mar 22 '16 at 07:59
  • @BalusC Thank you. I will check the link. – vishnu viswanath Mar 22 '16 at 18:21