selecting row(s) via javascript in p:datatable

Hi,

with PrimeFaces things can be so easy that you cannot even imagine. Lastly a requirement came to me to select a row ( or rows ) with javascript for the p:datatable. The thing I did was like this:

<p:dataTable rowIndexVar="rowIndex"  widgetVar="datatableVar"
selectionMode="single"
selection="#{someBean.someData}">

<p:column>
<f:facet name="header">
<h:outputText value="Select"/>
</f:facet>
<h:graphicImage value="/images/selectImage.gif"  onclick="datatableVar.unselectAllRows();datatableVar.selectRow(#{rowIndex})"/>
</p:column>

...

</p:datatable>

with the help of

onclick="datatableVar.unselectAllRows();datatableVar.selectRow(#{rowIndex})"

, I removed the previous selections and added the new selection. As you see it is very easy to do stuff with PrimeFaces. By the way as the datatable will be re-written in the new version, this may not work in the future but currently it works nicely…

Advertisements