Dynamic columns with List<List> in <p:dataTable><p:columns>

Cristian picture Cristian · Nov 15, 2013 · Viewed 8.1k times · Source

I'm attempting to generate a dataTable with columns dinamycally, so I've a List<List> when a List inside of a List is the content of my column, but when I try to show it I can't display not much.

So, this is the code of my Bean:

@ManagedBean
@javax.faces.bean.ViewScoped
public class Controlador {

   private List<List> estadistico;

   @PostConstruct
   public void inicializar(){
      this.estadistico = new ArrayList<List>();

      this.estadistico.add(  Arrays.asList( new Integer[]{0,1,24})); 
      this.estadistico.add(  Arrays.asList( new Integer[]{5,1,34})); 
      this.estadistico.add(  Arrays.asList( new Integer[]{12,1,4})); 

   }
   //getter's and setter's
}

And this is the view:

<h:form>
    <!-- estadistico is  List<List> -->
    <p:dataTable value="#{controlador.estadistico}" var="lista">
        <!-- lista is List of numbers
             and I suppose that value is each number
         -->
        <p:columns value="#{lista}" var="value" >
               #{value}
        </p:columns>
    </p:dataTable>
</h:form>

I expected some like :

---------------
0     5    12 
---------------
1     1    1
---------------
24    34   4
---------------

what am I doing wrong?

What is the correct way?

Answer

BalusC picture BalusC · Nov 15, 2013

The <p:columns value> cannot refer the <p:dataTable var>. It is technically and logically not possible to control the columns on a per-row basis. They have to be controlled on a per-table basis.

If your model guarantees that every nested list has the same size, then this should do:

<p:dataTable value="#{controlador.estadistico}" var="lista">
    <p:columns value="#{controlador.estadistico[0]}" columnIndexVar="i">
        #{lista[i]}
    </p:columns>
</p:dataTable>

See also the <p:columns> showcase.