Sunday, 8 September 2013

In Primefaces, all the radiobutton sare getting selected automatically

In Primefaces, all the radiobutton sare getting selected automatically

I am following the example given here. I have to show CheckBox and
RadioButton for a list of Employees, where user can select many CheckBoxs,
but only one RadioButton. Just the normal behaviour. I started with
Radiobutton first and after running all my radiobuttons are selected
automatically.
I have the below index.xhtml page
<!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>
My Backing bean is:
@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
Below is the POJO Class of Employee:
public class Employee implements Serializable{
private static final long serialVersionUID = 1L;
private String name;
private int age;
private String department;
//Constructor and Getters+Setters
And this is my DataModel class:
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();
}
}
Do you have any idea why all radiobuttons are getting selected
automatically and what changes I need to do? Thanks. Attached is the
screenshot

No comments:

Post a Comment