P:dataTable multiple selection not working

faceletsjsfjsf-2mojarraprimefaces

I don't seem to get multiple selection in PrimeFaces dataTables working.

I'm trying to implement a list of clients (dataList) and show their respective bookings in nested dataTables with a possibility to select multiple bookings for billing:

<p:dataList value="#{clientController.allClients}" var="client">
<p:column>
    <p:dataTable value='#{client.bookingsDataModel}' var='item' selection="#{client.bookingsToBill}">
        <p:column selectionMode="multiple" />
    </p:dataTable>
</p:column>
</p:dataList>

My controller and backing bean classes:

public class ClientController {
    public List<Client> getAllClients() {
        return clients;
    }
}

public class Client {

    private List<Booking> bookings;
    private Booking[] bookingsToBill;

    public LeistungDataModel getBookingsDataModel() { 
        return new BookingsDataModel(bookings); 
    }

    public Booking[] getBookingsToBill() { 
        return bookingsToBill; 
    }

    public void setBookingsToBill(Booking[] bookingsToBill) { 
        this.bookingsToBill = bookingsToBill;
    }

}

The data model class:

public class BookingsDataModel extends ListDataModel<Booking> implements SelectableDataModel<Booking> {    

    public BookingsDataModel(List<Booking> data) {  
        super(data);  
    }  

    @Override  
    public Booking getRowData(String rowKey) { 
        List<Booking> bookings = (List<Booking>) getWrappedData();
        for(Booking booking : bookings) {  
            if(("booking_"+booking.getId().toString()).equals(rowKey)) {
                return booking;
            } 
        }
        return null;  
    }  

    @Override  
    public Object getRowKey(Booking booking) {  
        return "booking_"+booking.getId().toString();  
    }

}

The browser posts the following data to the server, when I submit the form with my selections:

j_idt9%3Aj_idt13%3A0%3Aj_idt15_selection:booking_300,booking_301,booking_302
j_idt9%3Aj_idt13%3A1%3Aj_idt15_selection:booking_566,booking_567
j_idt9%3Aj_idt13%3A2%3Aj_idt15_selection:

Also, I found during debugging that the getRowData method of the BookingsDataModel returns the correct Booking objects (the selected ones).

However, always empty arrays are passed to the setBookingsToBill of my Client objects. What could be going wrong here?


Update:

An empty array is only passed the first Client objects – it doesn't matter if a booking has been selected or not. All other Client objects' setBookingsToBill methods are called with a parameter value of null.

Best Answer

Not really, if you want multiple selection with check box you have to do as jfs did:

In the showcase there is one example showing just that. It will create a column containing the boxes for the user to select. You can also do as you've said, using an attribute of p:dataTable, however this will not create the boxes and the user will have to control+click to do multiple select.

Related Topic