简体   繁体   中英

RadioButton row select event in p:datatable

RadioButton/Checkbox based row selection is a common use case and DataTable provides a solution for this with column selection mode feature.

<p:dataTable var="car" value="#{tableBean.cars}" paginator="true" rows="10"
                 selection="#{tableBean.selectedCar}">`
     <f:facet name="header">
            RadioButton Based Selection
        </f:facet>

        <p:column selectionMode="single" />

        <p:column headerText="Model">
            <h:outputText value="#{car.model}" />
        </p:column>

        <p:column headerText="Year">
            <h:outputText value="#{car.year}" />
        </p:column>

        <p:column headerText="Manufacturer">
            <h:outputText value="#{car.manufacturer}" />
        </p:column>


        <f:facet name="footer">
            <p:commandButton value="View" image="ui-icon ui-icon-search"
                             update="displaySingle" oncomplete="singleCarDialog.show()"/>
        </f:facet>
    </p:dataTable>

I want to know if I choose the first column's radioButton , how I get a event for this.

  • Because I want to let a button disabled when choose the first column or the last column's radioButton .
  • And I also want to get the index of the column when selected the column ,Now I use the selectedCar to compare the list,and get the index of the column. It looks ugly.Anyone can help me ?

This is a solution for PrimeFaces <= 2.x only.

There is a good example at Primeface's showcase . There is an attribute rowSelectListener that can be used like this:

rowSelectListener="#{tableBean.onRowSelect}"

and in the backing bean:

public void onRowSelect(SelectEvent event) {  
        FacesMessage msg = new FacesMessage("Car Selected", 
                           ((Car) event.getObject()).getModel());  
        FacesContext.getCurrentInstance().addMessage(null, msg);  
}  

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM