How to display variable number of columns in df

I have the following ArrayList

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

How can I map it to datatable. I tried using p: columns, it does not work. I have code on the 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 can’t achieve the desired result. 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 the output in the form of a table with the name of the manufacturer, color and all revenues

+4
source share
2 answers

Your mistake is that you referenced the current iterated row (an object beyond <p:dataTable var> as <p:columns value> . This is not possible. The number of columns cannot change depending on the current iteration of the row. Widescreen <p:columns value> should refer to the property in the backup bean. For an example in the real world, see also this answer: Dynamically create and populate a DataTable in JSF2.0 .

In your specific case, you just 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 on separate lines, print <br/> after #{revenue} or if you want to print them using comparison, use varStatus )

or possibly a nested table:

  <p:column> <h:dataTable value="#{report.revenue}" var="revenue">#{revenue}</h:dataTable> </p:column> 
+3
source

Try

 <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> 

The bean columns indicate:

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

It is filled as follows:

 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

+1
source

Source: https://habr.com/ru/post/1502747/


All Articles