4

I have the following ArrayList

class Report{
private String manufacturer;
private String color;
private List<Double> revenue;
}

How can I display it on primefaces datatable. I tried using p:columns it is not working. I have following code on XHTML page

<p:dataTable value="#{tableBean.reportList}" var="report" id="table">

    <p:column headerText="Manufacturer">#{report.manufacturer}</p:column>

     <p:column headerText="Color">#{report.color}</p:column>
 </p:dataTable >

I also tried p:columns and ui:repeat. But I am not able to achieve desired output. result.

<p:columns var="amount" value="#{report.revenue}">#{amount}</p:columns>

<ui:repeat var="amount" value="#{report.revenue}">
<p:column headerText="Revenue">#{amount}</p:column>
</ui:repeat>

I need output as a table with manufacturer name, color and all revenues

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Ravi Shekhar
  • 2,633
  • 4
  • 19
  • 19

2 Answers2

3

Your mistake is that you referenced the currently iterated row (the object behind <p:dataTable var> as <p:columns value>. This is not possible. The amount of columns cannot vary based on the currently iterated row. It can only be set table-wide. The <p:columns value> should reference a property in a backing bean. For a real world example, see also this answer: Creating and populating a DataTable dynamically in JSF2.0.

In your particular case, you just basically want to use <ui:repeat> inside <p:column>:

<p:dataTable value="#{tableBean.reportList}" var="report">
    <p:column>
        #{report.manufacturer}
    </p:column>
    <p:column>
        #{report.color}
    </p:column>
    <p:column>
        <ui:repeat value="#{report.revenue}" var="revenue">#{revenue}</ui:repeat>
    </p:column>
</p:dataTable>

(if you want to print them in separate lines, print a <br/> after #{revenue}; or if you want to print them commaseparated, make use of varStatus)

or maybe a nested table:

    <p:column>
        <h:dataTable value="#{report.revenue}" var="revenue">#{revenue}</h:dataTable>
    </p:column>
Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
2

Try with

<p:column headerText="Manufacturer">#{report.manufacturer}</p:column>

<p:column headerText="Color">#{report.color}</p:column>

<p:columns value="#{tableBean.columns}" var="column">
    <f:facet name="header">
        #{column.header}
    </f:facet>
    #{report.revenue.get(column.property)}
</p:columns>

Where in your bean columns is:

List<ColumnModel> columns;

static public class ColumnModel implements Serializable {

    private String header;
    private int property;

    public ColumnModel(String header, int property) {
        this.header = header;
        this.property = property;
    }

    public String getHeader() {
        return header;
    }

    public int getProperty() {
        return property;
    }
}

Filled as follow:

for(int i = 0; i < TOTAL_NUMBER_OF_REVENUES; i++){
    ColumnModel col = new ColumnModel("Revenue "+i, i);
    columns.add(col);
}

Source : http://www.primefaces.org/showcase/ui/datatableDynamicColumns.jsf

Pl4yeR
  • 195
  • 1
  • 6
  • You're still making the same mistake as the OP: referencing the currently itereated item in ``. This is never going to work. – BalusC Sep 18 '13 at 11:43
  • Ouch! I wrote report.columns but I was meaning tableBean.columns (columns is a list inside the bean). I will edit it, but anyway your answer is the right one since this one does not work if each Report object has a variable number of revenues... (you has to choose which one are shown...) – Pl4yeR Sep 19 '13 at 07:01