【问题标题】:Group radio buttons inside a datatable - Primefaces在数据表中分组单选按钮 - Primefaces
【发布时间】:2017-03-01 01:04:03
【问题描述】:

我想使用如下数据表组件创建网页,该网页在primefaces中带有单选按钮。

我找到了一种解决方案来实现 http://www.javaworld.com/article/2077687/enterprise-java/group-radio-buttons-inside-a-jsf-datatable-component.html for JSF 中提到的自定义组件。

但这需要时间和大量代码。

【问题讨论】:

    标签: jsf primefaces


    【解决方案1】:

    Primefaces 为 selectOneRadio 提供了自定义布局。我使用 selectOneRadio 的自定义布局示例实现了表格,如下所示。在这第一列中,宽度为零,其中包含单选按钮。

    <p:dataTable id ="employeeTable" rowIndexVar="rowId" var ="emp" value ="#{employeeList.employeeData}" widgetvar ="employeeTable" resizableColumns="true">
    
       <p:column headerText="" style="width:0px;">
         <p:selectOneRadio id="action" value="#{emp.status}" layout ="custom">
            <f:selectItem itemLabel="Yes" itemValue="Yes" />
            <f:selectItem itemLabel="No" itemValue="No" />
            <f:selectItem itemLabel="Amendment" itemValue="Amendment" />
            <f:selectItem itemLabel="KIV" itemValue="KIV" />
        </p:selectOneRadio>
        </p:column>
    
        <p:column headerText="Personnel No">
                <h:outputText value="#{emp.perNum}" />
        </p:column>
        <p:column headerText="Empl Name">
            <h:outputText value="#{emp.name}" />
        </p:column>                             
        <p:column headerText="Yes">
            <p:radioButton  for="action" itemIndex="0" />
        </p:column>
        <p:column headerText="No">
            <p:radioButton  for="action" itemIndex="1" />
        </p:column>
        <p:column headerText="Amendment">
            <p:radioButton for="action" itemIndex="2" />
        </p:column> 
        <p:column headerText="KIV">
            <p:radioButton  for="action" itemIndex="3" />
        </p:column> 
    </datatable>
    

    【讨论】:

    • 这种方法不适用于我使用相同的语法
    【解决方案2】:

    在数据表之外使用p:selectOneRadio,并将layout 属性设置为“自定义”。用f:selectItems 填充这个收音机,这与通过“value”参数提供给dataTable 的数据相同。

    <p:selectOneRadio id="customRadio" 
                      value="#{beanMB.selectedItem}"
                      layout="custom">
        <f:selectItems value="#{beanMB.tableItems}"
                       var="item"
                       itemValue="#{item.radioValue}"
                       itemLabel="#{item.radioLabel}"/>
    </p:selectOneRadio>
    

    在dataTable列中使用p:radioButtonitemIndex匹配当前rowIndex

    <p:dataTable var="item"
                 rowIndexVar="rowIndex"
                 value="#{beanMB.tableItems}">
    
        <p:column headerText="Choice">
            <p:radioButton for="@form:customRadio" 
                           itemIndex="#{rowIndex}"
                           rendered="#{item.radioRendered}"/>
        </p:column>
    </p:dataTable>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-05-19
      • 1970-01-01
      • 2019-04-20
      • 1970-01-01
      • 2018-11-13
      • 2012-07-31
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多