在Primefaces中,所有单选按钮都会自动选中

时间:2013-09-08 07:32:20

标签: jsf jsf-2 primefaces

我正在关注给定here的示例。我必须显示CheckBox和RadioButton以获取Employees列表,其中用户可以选择许多CheckBox,但只能选择一个RadioButton。只是正常的行为。我首先开始使用Radiobutton,然后在运行后自动选择所有的radiobutton。

我有以下index.xhtml页面

<!DOCTYPE html>
<html lang="en"
    xmlns="http://www.w3.org/1999/xhtml"
    xmlns:f="http://java.sun.com/jsf/core"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:ui="http://java.sun.com/jsf/facelets"
    xmlns:p="http://primefaces.org/ui">

    <f:event listener="#{userPreferenceBean.preRender}" type="preRenderView" />
    <h:head>
        <title>Datatable with Checkbox and RadioButton Example</title>
    </h:head>
    <h:body>
        <h:form>
            <p:dataTable id="employeeDataTable" var="employee" value="#{userPreferenceBean.employeeList}"
                rowKey="#{userPreferenceBean.employeeDataModel}" paginator="true" rows="10" 
                selection="#{userPreferenceBean.selectedEmployeeList}">

                <f:facet name="header">
                    Showing employee List
                </f:facet>

                <p:column selectionMode="single" style="width:2%"></p:column>

                <p:column headerText="Name" style="width:48%">
                    #{employee.name}
                </p:column>

                <p:column headerText="Department" style="width:48%">
                    #{employee.department}
                </p:column>
            </p:dataTable>
        </h:form>

    </h:body>
</html>

我的支持豆是:

@ManagedBean
@SessionScoped
public class UserPreferenceBean implements Serializable{

    private static final long serialVersionUID = 1L;

    private List<Employee> employeeList;
    private List<Employee> selectedEmployeeList;
    private EmployeeDataModel employeeDataModel;

    public void preRender(ComponentSystemEvent ebent){
        System.out.println("Inside prerender");
    }

    @PostConstruct
    public void initializeEmployeeList(){
        createEmployeeList();
        employeeDataModel = new EmployeeDataModel(employeeList);
    }

    private void createEmployeeList(){
        employeeList = new ArrayList<>();
        employeeList.add(new Employee("Sudipta",29,"Computer"));
        employeeList.add(new Employee("Bunty", 29, "Electrical"));
        employeeList.add(new Employee("Pradipta", 24, "Computer"));
    }

        //Other Getter and Setters

以下是POJO员工类:

public class Employee implements Serializable{

    private static final long serialVersionUID = 1L;

    private String name;
    private int age;
    private String department;
        //Constructor and Getters+Setters

这是我的DataModel类:

public class EmployeeDataModel extends ListDataModel<Employee> implements SelectableDataModel<Employee>{

    public EmployeeDataModel(){

    }

    public EmployeeDataModel(List<Employee> employees){
        super(employees);
    }

    @Override
    public Employee getRowData(String rowKey) {
        @SuppressWarnings("unchecked")
        List<Employee> employees = (List<Employee>) getWrappedData();
        for(Employee employee : employees){
            if(employee.getName().equals(rowKey))
                return employee;
        }

        return null;
    }

    @Override
    public Object getRowKey(Employee employee) {
        return employee.getName();
    }

}

您是否知道为什么所有单选按钮都会被自动选中以及我需要做哪些更改?谢谢。附件是屏幕截图enter image description here

3 个答案:

答案 0 :(得分:0)

我认为不可能有一个radiobutton列表,其中只能选择1个,然后选择1个复选框来选择所有的radiobuttons。无线电按钮的正常行为是只能选择1。如果您只使用复选框,则可以使用,但即使这样,也只会选择显示页面。你可以在展示中测试这个。

如果我是你,我会只用复选框来实现解决方案。

答案 1 :(得分:0)

您没有正确使用employeeDataModel,表格的value属性应按以下方式初始化

value="#{userPreferenceBean.employeeDataModel}"

我认为你可以删除rowKey属性

请查看以下示例DataTable - Instant Row Selection

答案 2 :(得分:0)

最后我能够解决问题。问题如下: 由于我的selectionMode像

一样单一
<p:column selectionMode="single" style="width:2%"></p:column> 

所以我需要selection="#{userPreferenceBean.selectedEmployee}"而不是员工名单。

现在index.xhtml看起来像:

<p:dataTable id="employeeDataTable" var="employee" value="#{userPreferenceBean.employeeDataModel}"
            paginator="true" rows="10" selection="#{userPreferenceBean.selectedEmployee}">

            <f:facet name="header">
                    Showing employee List
                </f:facet>

            <p:column selectionMode="single" style="width:2%" />

            <p:column headerText="Name" style="width:48%">
                    #{employee.name}
                </p:column>

            <p:column headerText="Department" style="width:48%">
                    #{employee.department}
                </p:column>
        </p:dataTable>

我在我的支持bean中添加了以下成员。

private Employee selectedEmployee; with getters and setters. Now it is working fine.

完整代码为@ myGitHubRepo

感谢。