Valeriy Fedosov
Valeriy Fedosov

Reputation: 65

How to detect row selection in Primefaces Datatable?

I use Prime Faces 6.2 to construct checkboxed column. Have this sample:

<p:dataTable id="List"
                 value="#{tickets}"
                 lazy="true"
                 paginator="true"
                 paginatorTemplate="{FirstPageLink} {PreviousPageLink} {CurrentPageReport} {NextPageLink} {LastPageLink}"
                 currentPageReportTemplate="{startRecord}-{endRecord} из {totalRecords}"
                 rows="20"
                 rowKey="#{ticket.id}"
                 selection="#{ticketForm.abstractMTSBUExportTickets}"
                 var="ticket"
                 emptyMessage="Записи в данной категории отсутствуют">

<p:column selectionMode="multiple" style="width:40px; text-align:center">
    <!-- <p:ajax event="click" listener="#{form.abstractMTSBUExportTickets}" /> --> // first try
    <!-- <p:commandLink id="select" action="doSome" /> --> // second try
</p:column>

The purpose to use ajax is for making some button on page visible when at least one checkbox selected. Firstly, I tried to insert p:ajax tag into column tag but got exception:

 <p:ajax> Unable to attach behavior to non-ClientBehaviorHolder parent

More obviouse way was to insert command link into but it simply did nothing. So, is there any practice to do this? Thanks for answers in advance.

Upvotes: 1

Views: 5599

Answers (1)

Melloware
Melloware

Reputation: 12019

Yes you are close but what you what is Ajax on the Datatable and not on the checkboxes. PF provides these two Ajax events on the datatable for being notified or boxes being checked or unchecked...

 <p:datatable>
       <p:ajax event="rowSelect" listener="#{dtSelectionView.onRowSelect}" update=":form:mybutton" />
       <p:ajax event="rowUnselect" listener="#{dtSelectionView.onRowUnselect}" update=":form:mybutton" />
       ...
</p:datatable>

See the Select Events example from the Showcase for more info: https://www.primefaces.org/showcase/ui/data/datatable/selection.xhtml

You can simply do something like in the Select and Unselect methods will trigger updating your selection and you can enable the button using EL expression like...

<p:commandButton id="mybutton" disabled="#{ticketForm.abstractMTSBUExportTickets.size > 0}" />

Upvotes: 2

Related Questions