Skip to Content

Within SAP NetWeaver 04s the Web Dynpro Table UI element was significantly  enhanced with additional features like grouped columns, cell variants, single  markable cells, standard cells with adaptable designs or table popins. You can  find more detailed information about the extended Table UI element within theSAP Online Help documentation.

Before we will publish an updated version of theWeb Dynpro table tutorial on SDN we want to provide an enhanced version of  the highly useful TableSorter class which can be reused in all Web Dynpro  applications running in SAP NetWeaver 04s.

Functionality of the NW04s-adapted TableSorter class

Adapted to SAP NetWeaver 04s the enhanced TableSorter class provides the  following functionality:

Adapted Table UI element event binding:  In NW04 the TableSorter  implements the functionality to display the sort icons in the column headers and  to store the sort state for each column. In NW04s this functionality is   generically provided by the Table-UIElement itself. The new TableSorter is now  based on the new onSort-event of the Table UI element instead of the onAction-event of the TableColumn UI element. The sort a and sort order icons are automatically displayed for sortable tables.

 image

Restricted sorting to a subset of columns: In order to restrict  sorting to a subset of table columns  in NW04, the onAction-event of  the corresponding TableColumn UI elements was bound to the action Sort. In NW04s the interaction model between the table and the TableSorter class is  completely based on the table’s onSort-event and the event parameters  ‘selected column’ and ‘sort direction’. Within an additional constructor methodTableSorter(IWDTable table, IWDAction sortAction, Map comparators, String[] sortableColumns)  a subset of sortable table columns can  be specified. The TableSorter class sorts all columns by default.

Definition of custom comparators:  The new TableSorter  uses a java.util.Map to define alternative comparators instead of an  Array because this solution is more flexible for further enhancements. As key  for the map, the column ID is used.

Support of grouped columns: The new TableSorter can deal withGroupedColumns, which were added in NW04s.

Language specific sorting : Strings are compared with the  java.text.Collator class to allow language specific sorting.

Support of non-singleton child node attributes:  The previous  TableSorter could only sort those olumns which were directly bound to an  attribute of the table’s root context node. Attributes of non-singleton child  nodes of this node, which were displayed in the same table, were not sortable.  The NW04s-TableSorter can now sort these columns too.

image

How to apply the TableSorter class

     

  1. Add the TableSorter class TableSorter.java  to the new package    folder src/packages/com/sap/tc/webdynpro/tests/utils
      image
  2.  

  3. Declare a new context attribute TableSorter of type   com.sap.tc.webdynpro.tests.utils.TableSorter within the view controller    context.
  4.  

  5. Declare a new action with name Sort (or SortCustomers in this    example) and with the associated action event handler onActionSort() to    the view controller.
  6.  

  7. Implement the following source code in the wdDoModifyView()-hook-method of    the view controller:  
    public static void wdDoModifyView(
       IPrivateWork wdThis, IPrivateWork.IContextNode wdContext, com.sap.tc.webdynpro.progmodel.api.IWDView view, boolean firstTime)
      {
        //@@begin wdDoModifyView
        if (firstTime) {
          IWDTable table = (IWDTable) view.getElement("CustomerTable");
          wdContext.currentContextElement().setCustomerTableSorter(
            new TableSorter(table, wdThis.wdGetSortCustomersAction(), null, new String[] { "CustomerTable_Name" }))
    ;
        }
        //@@end
      }

     

  8.  

  9. In case you apply the new generic UI service which automatically creates all custom extension fields (only supported for Adaptive RFC models) in the view layout you must additionally invoke the IWDView interface. By default, custom extension fields are created automatically after the first call to wdDoModifyView() that follows the creation of a parent element (e.g. table) that aggregates custom extension fields. Of course, this only relates to custom extension fields that have been explicitly configured by an administrator for the current user role. In case you want to modify these custom extension fields (e.g. making table columns sortable) you must call the method IWDView.nowCreateAllCustomExtensionFields() before:      
    public static void wdDoModifyView(IPrivateWork wdThis, IPrivateWork.IContextNode wdContext, com.sap.tc.webdynpro.progmodel.api.IWDView view, boolean firstTime)
      { 
        //@@begin wdDoModifyView
        if (firstTime) {
          view.nowCreateAllCustomExtensionFields();   
          IWDTable table = (IWDTable) view.getElement("CustomerTable");
          wdContext.currentContextElement().setCustomerTableSorter(
            new TableSorter(table, wdThis.wdGetSortCustomersAction(), null, new String[] { "CustomerTable_Name" }));
        }
        //@@end
      }

     

  10.  

  11. Organize imports within the view controller class so that import    com.sap.tc.webdynpro.tests.utils.TableSorter; is added.  
  12.  

  13. Implement the following source code in the action event handler   onActionSort() (or onActionSortCustomers in this example).  
      //@@begin javadoc:onActionSortCustomers(ServerEvent)
      /** Declared validating event handler. */
      //@@end
      public void onActionSortCustomers(com.sap.tc.webdynpro.progmodel.api.IWDCustomEvent wdEvent )
      {
        //@@begin onActionSortCustomers(ServerEvent)
        wdContext.currentContextElement().getCustomerTableSorter().sort(wdEvent, wdContext.nodeCustomers());
        //@@end
      }

     

Download New NW04s-TableSorter class

You can download the Web Dynpro sample project WDTableSorter_NW04s.zip which applies the enhanced NW04s-TableSorter class. This sample is based on thecontext tutorial application.

Enjoy Web Dynpro,

Bertram Ganz, Jens Pflueger, SAP AG

To report this post you need to login first.

15 Comments

You must be Logged on to comment or reply to a post.

  1. Srikanth Patlu
    I’am running NWDS SP15 on my laptop and have issue compiling my WD project using the Table Sorter class. Some of the imports referenced in the table sorter class will not resolve on my machine. Example are as follows:

    import com.sap.tc.webdynpro.clientserver.uielib.standard.api.IWDAbstractTableColumn;

    import com.sap.tc.webdynpro.clientserver.uielib.standard.api.IWDTableColumnGroup;

    How can I resolve this issue ? Please advise.

    Thanks in advance !

    (0) 
      1. Som kolli
        Hi Bertram ,

        Can you pls let me know how to use this TableSorter class with Tree Table Structure?

        Appreciate your help

        Som

        (0) 
        1. Bertram Ganz Post author
          Hallo Kunal,

          the link to the previous “Web Dynpro Java Feature2Sample Matrix” page changed.

          Look at /docs/DOC-8061#46

          Regards, Bertram

          (0) 
  2. Hi Bertram ,
     
    This is a real nice blog.  I was using grouped columns and i found this class very useful for sorting them.  The usage is very simple.   

    Thanks,
    Arun

    (0) 
  3. Navneet Nair
    First of all, thanks a lot for that class. I had used it extensively in almost every table Ui application I had built in SP13. But I have now upgraded to SP16 NWDS … and find that many IWDTable methods in the TableSorter.java class arent found.
    Is there any alternative class that I need to use with SP16 ? The sorting requirement is just a client add-on, and I don’t really want to spend any significant amount of time on creating a complex sorting logic from scratch. Please help !! 🙁

    Navneet Nair.

    (0) 
    1. Bertram Ganz Post author
      Hi Navneet,

      the table sorter you used in NW04 SP13 should also work in SP16; which methods in the TableSorter.java class aren’t found now?

      Regards, Bertram

      (0) 
  4. Sigiswald Madou
    I was just wondering why you should pass the table’s dataSource as a parameter to the sort method. It can be determined in the init method as
      this.dataSource =
        getDataSourceNode(
          sortAction.getController().getContext(),
          table.bindingOfDataSource());
    with
      private IWDNode getDataSourceNode(IWDContext context, String dataSource) {
        String[] nodeNames = dataSource.split(“\\.”);
        IWDNode node = context.getRootNode();

        for (int i = 0; i<nodeNames.length; i++) {
          node = node.getChildNode(nodeNames[i], IWDNode.LEAD_SELECTION);
        }

        return node;
      }

    The tokenize method isn’t necessary either since
      String[] tokens = tokenize(attributeName, “.”);
    can be done as
      String[] tokens = attributeName.split(“\\.”);

    (0) 
  5. Massimiliano Turco
    Hi,
    I imported the project on NW using the linked zip but it doesn’t work. I got 2 error messages as in the following:

    1) “The project was not built due to classpath errors (incomplete or involved in cycle).”

    2) “Missing required source folder: ‘WDTableSorter_NW04s/gen_ddic/datatypes’.”

    any help?

    Thnx in advance
    Max Turco
    Italy

    (0) 
  6. Joren Crauwels
    The TableSorter class is very useful. I’ve adapted it (the nw04 version to one working in nw04s) to my own needs for sorting a web dynpro table on multiple columns. It has the following features:
    – single column sorting
    – multiple column sorting
    – simple display of the sorting order in case the user is sorting on multiple columns.

    It’s only developed for single columns, so i dont know if it fits everyone’s needs.

    I’m still curious though why the sorting isn’t just standard behavior on the web dynpro table element? (without having to implement the sorting behavior ourselves) The same goes for filtering columns…

    Kind regards,
    J.

    (0) 
  7. Yasar Pala
    Hi,

    How can I sort grouped columns. In the blog you mention this :
    “Support of grouped columns: The new TableSorter can deal with GroupedColumns”.

    How can we implement the sort functionality on grouped columns?

    Kind Regards,

    Yasar

    (0) 

Leave a Reply