Reputation: 317
Sorting is not working in Primefaces datatable.My jsf code is shown.We need to do anything in backing bean to make it working?
<p:dataTable id="employees" value="#{employeeList.employees}"
var="employee" emptyMessage="No Employees found" rows="10"
paginator="true"
paginatorTemplate="{CurrentPageReport} {FirstPageLink} {PreviousPageLink} {PageLinks} {NextPageLink} {LastPageLink} {RowsPerPageDropdown}"
rowsPerPageTemplate="5,10,15" rowIndexVar="rowIndex">
<p:column sortBy="#{employee.firstName}">
<f:facet name="header">
<h:outputText value="First Name" />
</f:facet>
<h:outputText value="#{employee.firstName}">
</h:outputText>
</p:column>
<p:column sortBy="#{employee.lastName}">
<f:facet name="header">
<h:outputText value="Last Name" />
</f:facet>
<h:outputText value="#{employee.lastName}" />
</p:column>
</p:dataTable>
My Primefaces version is 3.0.M3 with JSF2 and Google Cloud SQL
EmployeeList.java:
public List<Employee> getEmployees() throws HibernateException, SQLException {
List<Employee> employees = new ArrayList<Employee>();
employees.addAll(empList());
}
Where in empList() I wrote query for retrieving all employees and which returns all employees.
EmployeeList.java
@Component("employeeList")
@SessionScoped
@Repository
public class newb implements Serializable {
private static final long serialVersionUID = -2417394435764260084L;
public static HibernateTemplate hibernateTemplate;
@Autowired
public void setSessionFactory(SessionFactory sessionFactory)
{
this.hibernateTemplate = new HibernateTemplate(sessionFactory);
}
public List<Employee> getEmployees() throws HibernateException, SQLException {
List<Employee> employees = new ArrayList<Employee>();
employees.addAll(empList());
return employees;
}
@SuppressWarnings("unchecked")
public List<Employee> empList() {
try
{
List <Employee> result = hibernateTemplate.find("from Employee");
return result;
}
finally {
//close the session and user-supplied JDBC connection
}
}
}
Upvotes: 0
Views: 5849
Reputation: 41
It will not work if you have the list populated in your get. Sort it prime faces would not work if the list is populated each time in get. Instead try making the bean viewscoped and populate the list in the constructor, it should work then.
Upvotes: 4
Reputation: 604
try this:
private List<Employee> employees = new ArrayList<Employee>();
public List<Employee> getEmployees() {
if(employees.isEmpty()){
employees.addAll(empList());
}
return employees;
}
Upvotes: 1