Tuesday, March 18, 2014

How to override the default sort behavior of the af:table in Oracle ADF?

When ever you set the sortable="true" for any column in the af:table, we get the two options to sort the column (ascending or descending) as shown below








In order to override the default behavior, we have to configure the sort listener as below in the table properties














Add the following code in the sort listener and plugin the bahavior as required

import java.util.ArrayList;
import java.util.List;
import oracle.adf.view.rich.component.rich.data.RichTable;
import oracle.adf.view.rich.context.AdfFacesContext;
import org.apache.myfaces.trinidad.event.SortEvent;
import org.apache.myfaces.trinidad.model.SortCriterion;

public void onSortingTableColumn(SortEvent sortEvent) {
    List sortList = sortEvent.getSortCriteria();
    SortCriterion sc = (SortCriterion)sortList.get(0);
    boolean order = sc.isAscending();

    System.out.println(sc.getProperty());    
    System.out.println(order);
    
    sortList = new ArrayList();    

    // In the sort criteria parameters set the column by 
    // which you would like to sort and the order (asc, desc)
    // pass true if you want to sort in the ascending order 
    // and false otherwise
    String sortCol  = sc.getProperty().toString();
    
    // Create the desired search criteria here
    SortCriterion sc2 = new SortCriterion(sortCol, order);
    sortList.add(sc2);
    getTestDepTable().setSortCriteria(sortList);

    // Refresh the table after applying the sort criteria
    AdfFacesContext.getCurrentInstance().addPartialTarget(getTestDepTable());
}

2 comments: