Primefaces DataTable with SubTable and multiple selection

jsfjsf-2primefaces

I would like to have a table of bookings grouped by clients. From this table the user shall be able to select multiple booking for billing. So I am trying to use a SubTable for the grouping, however, I am unsure how to realize to selection functionality. Apparently the subtable doesn't allow the selection attribute and if I set the selection attribute on the parent DataTable I don't know how to choose the rowKey.

That is my try:

<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 results in the following error when submitting the 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 DataTables with SubTables? If so, how to do it right? If not, which way would you suggest to achieve a similar result?

I'm using: Primefaces 3.1.1 – Mojarra JSF 2.1 – Tomcat 6.0.14

Best Answer

Have you analyzed this solution described in Primefaces' showcase?

It basically boils down to this:

<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 with the ajax event listener, bound 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);
}

It's not very elegant, but it brought my logic to work after getting this rather obscure NullPointerException.

Related Topic