Friday, December 19, 2008

JSF / JBossSeam - Show list of records with scroller & Return to the page from which you edited.




The above content is emplist.xhtml.
Consider the scenario, Requirement 1 : Display a list of employee records as 10 rows per page. Requirement 2: You are editing a record of employee list in n-th page. On clicking on save or cancel button, it has to go the n-th page from which you edited. In JSF/Seam, We have dataTable and dataScroller components from richfaces to achieve these.

Use rich:dataTable to list the employee records as above.

Use rich:dataScroller to have scroller for the table.
Use for attribute to map the table.

Use pageIndexVar attribute of rich:datascroller to capture the current page number and pass it as request parameter while editing the record.

Retrieve the page index value from request and calculate the first row to be displayed in dataTable as below:

//Get the request parameter pageIndex
Integer index = Integer.parseInt((String)FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("pageIndex"));

//Calculate the first row to set into the first attribute of rich:dataTable
index = (index - 1) * 10; // 10 – value of rows attribute in rich:dataTable


Set this value in session scope.

While clicking the save or cancel button, get the index value from session and set the value to member variable bound to the first attribute of rich:dataTable.