Primary DataTable with SubTable and Multiple Selection

I would like to have a table of orders grouped by customers. From this table, the user can select several booking options for billing. Therefore, I am trying to use SubTable to group, however, I am not sure how to implement the selection functionality. Apparently, the subtable does not allow the selection attribute, and if I set the selection attribute in the parent DataTable, I don’t know how to select rowKey.

This is my attempt:

<p:dataTable style="border: 0px;" value='#{clientController.allClients}' var='client' rowKey="#{item.id}" selectionMode="multiple" selection="#{bookingController.bookingsToBill}"> <p:subTable value='#{client.billableBookings}' var='item'> <f:facet name="header"> <h:outputText style="font-weight:bold;" value="#{client.name}" /> </f:facet> <p:column> <f:facet name="header"> <h:outputText value="Booking"/> </f:facet> <h:outputText value="#{item.title}"/> </p:column> </p:subTable> </p:dataTable> 

Well, this leads to the following error when submitting a selection:

 java.lang.NullPointerException java.lang.reflect.Array.newArray(Native Method) java.lang.reflect.Array.newInstance(Array.java:52) org.primefaces.component.datatable.DataHelper.decodeMultipleSelection(DataHelper.java:238) org.primefaces.component.datatable.DataHelper.decodeSelection(DataHelper.java:224) org.primefaces.component.datatable.DataTableRenderer.decode(DataTableRenderer.java:64) javax.faces.component.UIComponentBase.decode(UIComponentBase.java:787) javax.faces.component.UIData.processDecodes(UIData.java:1162) org.primefaces.component.datatable.DataTable.processDecodes(DataTable.java:531) javax.faces.component.UIForm.processDecodes(UIForm.java:225) javax.faces.component.UIComponentBase.processDecodes(UIComponentBase.java:1176) javax.faces.component.UIComponentBase.processDecodes(UIComponentBase.java:1176) javax.faces.component.UIViewRoot.processDecodes(UIViewRoot.java:933) com.sun.faces.lifecycle.ApplyRequestValuesPhase.execute(ApplyRequestValuesPhase.java:78) com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101) com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:118) javax.faces.webapp.FacesServlet.service(FacesServlet.java:409) 

Is multiple selection supported for data tables with subtests? If so, how to do it right? If not, in which case would you suggest achieving a similar result?

I use: Primefaces 3.1.1 - Mojarra JSF 2.1 - Tomcat 6.0.14

+4
source share
1 answer

Have you analyzed this solution described in the Primefaces showcase?

It basically boils down to the following:

 <p:dataTable style="border: 0px;" value='#{clientController.allClients}' var='client' rowKey="#{item.id}" selection="#{bookingController.bookingsToBill}" > <p:subTable value='#{client.billableBookings}' var='item'> <f:facet name="header"> <h:outputText style="font-weight:bold;" value="#{client.name}" /> </f:facet> <p:column selectionMode="multiple" /> <p:column> <f:facet name="header"> <h:outputText value="Booking"/> </f:facet> <h:outputText value="#{item.title}"/> </p:column> </p:subTable> 

Or try the ajax event listener attached to your BookingController:

 <p:ajax event="rowSelect" listener="#{bookingController.rowSelected}" /> <p:ajax event="rowUnselect" listener="#{bookingController.rowUnselected}" /> 

And you update your own list of selected items in these two functions:

 List<Booking> selectedBookings = new ArrayList<>(); ... public void rowSelected(SelectEvent event) { Booking book = (Booking) event.getObject(); selectedBookings.add(book); } public void rowUnselected(UnselectEvent event) { Booking book = (Booking) event.getObject(); selectedBookings.remove(book); } 

This is not very elegant, but it caused my logic to work after getting this rather obscure NullPointerException.

+1
source

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


All Articles