Wednesday, November 13, 2013

Implementing Select All in adf table getting the data from a Pojo datacontrol

There is a af table where one of the column has checkbox that enables user to select or deselect. There is no row selection in that table and hence selection of checkbox is  treated as selection of row.

There is checkbox in the column header of that column and on checking that checkbox, I want all the checkboxes in the respective column to be selected and viceversa. This can be achieved by following solution approach.

The table is getting populated by bindings corresponding to a collection in a POJO data control. Below is the code snippet for Pojo DC. The Pojo is chosen here instead of ADF ViewObject because the list of employees is obtained from Session here.

public class myPojoDc{
       private List<Employee> employeeList;

       public List<Employee> getEmployeeList(){
             if(employeeList == null) {
                 prepareEmployeeList();
             }
             return employeeList;
       }

       private void prepareEmployeeList(){
          //Access the list from Session and set it to employeeList.
       }
}

This data control is looked up and the employeeList is dragged and dropped on a ADF jsff form. This creates bindings in pagedef and af:table in the jsff.

Below is the code snippet for value change event handler of selectAll checkbox in the af:table.


    public void handleAccountsSelectAll(ValueChangeEvent ve) {
     
        BindingContainer bindings =
            BindingContext.getCurrent().getCurrentBindingsEntry();
        DCIteratorBinding it =
            (DCIteratorBinding)bindings.get("employeeListIterator");

        RowSetIterator rit = it.getRowSetIterator();
        rit.reset();
        int count = 0;
        if (selectAll != null) {
            if (rit.first() != null) {
                Row r = rit.first();
                r.setAttribute("selected", selectAll);
                if (selectAll) {
                    count++;
                }
            }
            while (rit.hasNext()) {
                Row r = rit.next();
                if (r != null) {
                    r.setAttribute("selected", selectAll);
                    if (selectAll) {
                        count++;
                    }
                }
            }
        }
        //The count here represents the total count selected rows which is nothing but all rows. The count is kept in pageFlowScope so that is used by outputtext located in below to af:table in the jsff.               
        getPageFlowScope().put(respectiveCount, count);
        it.executeQuery();
        //table instance below is obtained by component binding to af:table.
        addPartialTarget(table);
    }

No comments:

Post a Comment