0

I am using Primefaces DataTable, it is using the value from a bean that provide a list. and we display a row for each Item from the list.

As well i have another list which is used to display the values in each row for each of its columns.

The example below works very well.

USING VALUES FROM BEAN

<p:dataTable var="item" value="#{bean.items}">

  <p:columns var="value" value="#{bean.values}">

     <p:outputLabel value="#{value}"/> <!--[WORKS FINE !!!]-->

   </p:columns>

</p:dataTable>

If instead of using the values from the bean, i try to use the values from the item itself it does not display anything !

USING VALUES FROM ITEM ITSELF

<p:dataTable var="item" value="#{bean.items}">

  <p:columns var="value" value="#{item.values}">

     <p:outputLabel value="#{value}"/> <!--[DOES NOT DISPLAY ANYTHING !!!]-->

   </p:columns>

</p:dataTable>

The bean is the following and it display rows in datatable when using the values from there.

@Named("bean")
@ViewScoped
public class Bean implements Serializable 
{
    private static final long serialVersionUID = 1L;

    private List<Item> _items = new LinkedList<>();
    private List<String> _values = new LinkedList<>();

    public List<Item> getItems() {return _items;}
    public void setItems(List<Item> items) {_items = items;}

    public List<String> getValues() {return _values;}
    public void setValues(List<String> values) {_values = values;}

    //[BUILD]
    @PostConstruct public void init()
    {
       _items.add(new Item());
       _items.add(new Item());
       _items.add(new Item());
    }
}

The Item object is the following and it does not display rows when using values from there.

public class Item
{
  private List<String> _values = new LinkedList<>();

  public List<String> getValues() {return _values;}
  public void setValues(List<String> values) {_values= values;}

  //[BUILD]
  public Item()
  {
    _values.add("value0");
    _values.add("value1");
    _values.add("value2");
  }
}

Why the primeface datatable does not display any row in the case i use the list of values from an external object like below ???

WORKS FINE !

<p:dataTable var="item" value="#{bean.items}">
  <p:columns var="value" value="#{bean.values}"><!--[USING BEAN LIST<STRING> -->
     <p:outputLabel value="#{value}"/>

DOES NOT WORKS !

<p:dataTable var="item" value="#{bean.items}">
  <p:columns var="value" value="#{item.values}"><!--[USING ITEM LIST<STRING> -->
     <p:outputLabel value="#{value}"/>
fra
  • 83
  • 1
  • 11
  • Possible duplicate of [Primefaces static and dynamic columns in datatable](https://stackoverflow.com/questions/25658034/primefaces-static-and-dynamic-columns-in-datatable) – Kukeltje Jun 03 '19 at 11:47

1 Answers1

0

As you can see from the showcase https://www.primefaces.org/showcase/ui/data/treetable/columns.xhtml the columns (and so the number of columns) must not depend from the current item.

Alexcat
  • 87
  • 4
  • For which there is duplicate: https://stackoverflow.com/questions/25658034/primefaces-static-and-dynamic-columns-in-datatable – Kukeltje Jun 03 '19 at 11:47
  • @Alexcat yeah it seems like they are using that way. But it just doesnt make sense. The object you pass to the datatable will be most likely a list of entity and for each entity you want to generate the row data columns. What makes sense is to pass the datatable var to the columns var. – fra Jun 05 '19 at 06:33
  • @Alexcat as well it does not say anywhere in the documentation we cannot do it that way. So nothing is official here. I still have no answer to my question :( – fra Jun 05 '19 at 06:34
  • @fra I agree with you that documentation does not state what is allowed and what is forbidden. Yet, I agree with the logic that is behind p:columns. Indeed the list of entities you pass to the **datatable** must affect only the number of rows, and not the number of columns. – Alexcat Jun 06 '19 at 07:40