I use Prime Faces 6.2 to make a data table with checked column:
<p:dataTable id="#{prefix}List"
value="#{tickets}"
lazy="true"
paginator="true"
paginatorTemplate="{FirstPageLink} {PreviousPageLink} {CurrentPageReport} {NextPageLink} {LastPageLink}"
currentPageReportTemplate="{startRecord}-{endRecord} из {totalRecords}"
rows="20"
rowKey="#{ticket.id}"
var="ticket"
emptyMessage="Записи в данной категории отсутствуют">
<p:ajax event="toggleSelect" listener="#{ticketForm.onAllSelect}" process="@this" partialSubmit="true" />
<p:ajax event="rowSelectCheckbox" listener="#{ticketForm.onSelect}" update=":protocolForm" />
<p:ajax event="rowUnselectCheckbox" listener="#{ticketForm.onUnselect}" update=":protocolForm" />
<p:ajax event="rowSelect" listener="#{ticketForm.onSelect}" update=":protocolForm" />
<p:ajax event="rowUnselect" listener="#{ticketForm.onUnselect}" update=":protocolForm" />
<p:column selectionMode="multiple" style="width:40px; text-align:center" />
More specifically, when a header checkbox is selected I want to receive all selected rows data on server-side and use IDs of each row. Also, there's logic that helps to resolve a goal of hidding/rendering a page button when at least one checkbox is selected.
For this purpose I need manualy intercept a row/checkbox selection event and record IDs from it when a button pushed, so I can't use selection
attribute of dataTable using such logic.
On server-side I have several event-listeners:
private Set<AbstractMTSBUExportTicket> abstractMTSBUExportTickets = new HashSet<>();
public Set<AbstractMTSBUExportTicket> getAbstractMTSBUExportTickets() {
return abstractMTSBUExportTickets;
}
public void onSelect(SelectEvent event) {
abstractMTSBUExportTickets.add((AbstractMTSBUExportTicket) event.getObject());
}
public void onUnselect(UnselectEvent event) {
abstractMTSBUExportTickets.remove(event.getObject());
}
public void onAllSelect(ToggleSelectEvent event) {
// do smth
}
Unfortunately, ToggleSelectEvent has only information about data-table itself within.
None information about rows I couldn't find. Also I tried to add process="@this" partialSubmit="true"
attributes but seems like they do different actions.
So, could you give me an example of how it's possible to retrieve such data from an event object? Should I use another way to solve it? Thanks for your answers in advance.
I think you are missing some things on the datatable.
Showcase: https://www.primefaces.org/showcase/ui/data/datatable/selection.xhtml
You need to add selectionMode=multiple and a selection=collection to gather the selected rows like...
Whenever you select a row or row(s) the #{dtSelectionView.selectedCars} collection will be filled with the selected rows automatically.