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

1
  • Check the Primefaces Showcase. Commented Sep 18, 2013 at 8:38

2 Answers 2

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>
Sign up to request clarification or add additional context in comments.

Comments

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

2 Comments

You're still making the same mistake as the OP: referencing the currently itereated item in <p:columns value>. This is never going to work.
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...)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.