简体   繁体   English

p:datatable中的单选按钮行选择事件

[英]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. 基于RadioButton / Checkbox的行选择是一个常见的用例,DataTable通过列选择模式功能为此提供了解决方案。

<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. 我想知道是否选择第一列的radioButton, 如何获得此事件。

  • Because I want to let a button disabled when choose the first column or the last column's radioButton . 因为我想在选择第一列或最后一列的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. 而且我也想在选择列时获取列的索引 ,现在我使用selectedCar比较列表,并获取列的索引。 It looks ugly.Anyone can help me ? 看起来很丑。有人可以帮我吗?

This is a solution for PrimeFaces <= 2.x only. 这仅适用于PrimeFaces <=2.x。

There is a good example at Primeface's showcase . Primeface的陈列柜上有一个很好的例子。 There is an attribute rowSelectListener that can be used like this: 有一个属性rowSelectListener可以像这样使用:

rowSelectListener="#{tableBean.onRowSelect}"

and in the backing bean: 并在支持bean中:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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