0
votes

I am using JSF 2.0 and PrimeFaces 2.2. I have a data table whose columns need to be updated according to the selection from drop down menu. I do not want to create multiple data tables as there are many values in drop down according to which column values as well as number of columns in datatable will be shown.

Is it possible to do so?

Ok let me very specific, suppose i have ten items in drop down menu and on every selection there is a different type of datatables that i have to show on selection from drop down menu, i dont want to make 10 different types of datatable, i want a single datatable in which i can select the columns according to my selection(the datatables have lots of same entries, so there will be lot of repetitive entries in every datatable).

2
You want to have different columns in a dataTable already implemented and show/hide them based on your selection? Or do you want to change the list(dataTable value) based on your selection(basically this means for every option dataTable will show different records)? - spauny
yes i want to have different columns in a dataTable already implemented and show/hide them based on my selection. - curious
You weren't very specific; I've updated my answer and added the BalusC solution! - spauny

2 Answers

1
votes

YES! Let's find together a way (supposing that you have provided a default list to the dataTable component):

<p:dataTable ...with various attributes... id="myTable" value="#{myBean.recordList}">
   ...and various columns...
</p:dataTable>
<h:selectOneMenu value="...">
   <f:selectItems ...your items...>
   <p:ajax event="change" listener="#{myBean.myMethod}" update="myTable" />
</h:selectOneMenu> 

And myMethod would be a method where you change the recordList and then refresh(update) the table component. Of course you have to put an outer form component.

UPDATE:If you want to have different columns in a dataTable already implemented and show/hide them based on your selection(BalusC answer):

<p:dataTable ...with various attributes... id="myTable" value="#{myBean.recordList}">
   <p:column rendered="#{myBean.myValue == 1}" >
      first column
   </p:column>
   <p:column rendered="#{myBean.myValue == 2}" >
      second column
   </p:column>
   <p:column rendered="#{myBean.myValue == 3}" >
      third column
   </p:column>
</p:dataTable>
<h:selectOneMenu value="#{myBean.myValue}">
   <f:selectItem itemLabel="First Column" itemValue="1" />
   <f:selectItem itemLabel="Second Column" itemValue="2" />
   <f:selectItem itemLabel="Third Column" itemValue="3" />
   <p:ajax event="change" update="myTable" />
</h:selectOneMenu>