cancel
Showing results for 
Search instead for 
Did you mean: 

Table Sorter : table not getting sorted

Former Member
0 Kudos

I have written TableSorter.java class in util package in src. then i m binding onSort event property to sort even written in the view controller. Following is the code for my sort function , but still my table's coloumns are not getting sorted. Help.

/******Sort Event********/

wdContext.currentContextElement().getTableSorter().sort(wdEvent, wdContext.nodeEmployeeDetails());

EmployeeDetails is node binded to table datasource.

Accepted Solutions (1)

Accepted Solutions (1)

Former Member
0 Kudos

Hi kavita,

I try to help you,

I use the following code

public static void wdDoModifyView(IPrivateContactView wdThis, IPrivateContactView.IContextNode wdContext, com.sap.tc.webdynpro.progmodel.api.IWDView view, boolean firstTime)

{

//@@begin wdDoModifyView

if(firstTime){

IWDTable tTable = (IWDTable) view.getElement("Table_Name"); //Table_Name is bound to TableSorterNode

wdContext.currentContextElement().setSortAttribute(new TableSorter(tTable, wdThis.wdGetSortTableAction(), null))

}

//@@end

}

public void onActionsortTable(com.sap.tc.webdynpro.progmodel.api.IWDCustomEvent wdEvent )

{

//@@begin onActionsortTable(ServerEvent)

wdContext.currentContextElement().getSortAttribute().sort(wdEvent, wdContext.nodeTableSorterNode());

//@@end

}

And this work for me.

You can visit these post:

You must have something in mind, if you use a view.resetView () in wdDoModifyView() this not allow to TableSorter.java work properly.

Regards,

Pedro

sorry for my english...

Former Member
0 Kudos

I have already added following code in my wdmodify method

if (firstTime) {

IWDTable table = (IWDTable) view.getElement("Table_0"); // Table_0 table name

wdContext.currentContextElement().setTableSorter(new TableSorter(table, wdThis.wdGetSortAction(), null));

// TableSorter value attribute of type TableSorter

// sort() event

}

public void onActionSort(com.sap.tc.webdynpro.progmodel.api.IWDCustomEvent wdEvent )

{

//@@begin onActionSort(ServerEvent)

wdContext.currentContextElement().getTableSorter().sort(wdEvent, wdContext.nodeEmployeeDetails());

//@@end

}

but still its not getting sorted. I have to bind anything to my Table_0 ???

Answers (4)

Answers (4)

Former Member
0 Kudos

Thnx All ,

My problem has been solved. I was using older version of TableSorter.java class. I have downloaded new class from link provided by u. Thanks again.

Regards

Kavita

Former Member
0 Kudos

Hi Kavita,

Imagine that you have this:

an Orders node in View's Context

Orders [Node]
     Date [Attribute]
     Product [Attribute]
     Price [Attribute]

a Table in View's Layout

OrderTable [Table]
     [Caption]
     OrderTable_Date[TableColumn]
          [Caption]
          OrderTable_Date_editor[TextView - TableCellEditor]
     OrderTable_Product[TableColumn]
          [Caption]
          OrderTable_Product_editor[TextView - TableCellEditor]
     OrderTable_Price[TableColumn]
          [Caption]
          OrderTable_Price_editor[TextView - TableCellEditor]

and

OrderTable bound to Orders node

OrderTable_Date_editor bound to Date attribute

OrderTable_Product_editor bound to Product attribute

OrderTable_Price_editor bound to Price attribute

Put this code in your tab of implementation

inside of wdDoModifyView:

public static void wdDoModifyView(IPrivateWork wdThis, IPrivateWork.IContextNode wdContext, com.sap.tc.webdynpro.progmodel.api.IWDView view, boolean firstTime)
{
     //@@begin wdDoModifyView
     if (firstTime) {
          table = (IWDTable) view.getElement("OrderTable");
          wdContext.currentContextElement().setOrderTableSorter(
          new TableSorter(table, wdThis.wdGetSortOrdersAction(), null)); // sort all columns
     }
     //@@end
}

define an Action:

public void onActionSortOrders(com.sap.tc.webdynpro.progmodel.api.IWDCustomEvent wdEvent )
{
     //@@begin onActionSortOrders(ServerEvent)
     wdContext.currentContextElement().getOrderTableSorter().sort(wdEvent, wdContext.nodeOrders());
     //@@end
}

Deploy new archive a run, this should work

This example is of <a href="/people/bertram.ganz/blog/2006/03/07/enhanced-web-dynpro-java-tablesorter-for-sap-netweaver-04s blog</a>

Regards,

Pedro

former_member485701
Active Participant
0 Kudos

Hi,

I think you are using the TableSorter.java file available on SDN.

then follow these steps:-

TableSorter sorter=new TableSorter((IWDTable)view.getElement("Table id"),wdThis.wdGetSortAction());

WDTableColumnSortDirection sortDirection=wdContext.currentContextElement().getTablecolumnsorter();

// For sorting pass Context node, sorting direction and column name

sorter.sort(wdContext.nodeEmployeeDetails(),sortDirection,columnName);

Feel free to ask queries regarding this.

Regards,

Praveen

Former Member
0 Kudos

where do i hv to add this sortcoloumn code??? in sort event of view???

TableSorter sorter=new TableSorter((IWDTable)view.getElement("Table id"),wdThis.wdGetSortAction());

WDTableColumnSortDirection sortDirection=wdContext.currentContextElement().getTablecolumnsorter();

// For sorting pass Context node, sorting direction and column name

sorter.sort(wdContext.nodeEmployeeDetails(),sortDirection,columnName);

former_member485701
Active Participant
0 Kudos

Hi,

Yes you have to do this in onActionSort.

Normally what you are doing it should work, but I am giving you the way to test your TableSorter.Add a sort method which i have given to you is with the hardcoded columnName and sorting direction, coding will be same as the existing sort method.

public void sort(IWDCustomEvent wdEvent, IWDNode dataSource,String columnId,String direction) {

IWDTableColumn column = (IWDTableColumn) table.getView().getElement(columnId);

NodeElementByAttributeComparator elementComparator = (NodeElementByAttributeComparator) comparatorForColumn.get(column);

if (elementComparator == null){

//not a sortable column

column.setSortState(WDTableColumnSortDirection.NOT_SORTABLE);

return;

}

// sorting

elementComparator.setSortDirection(WDTableColumnSortDirection.valueOf(direction));

dataSource.sortElements(elementComparator);

}

Regards,

Praveen

Message was edited by:

Praveen Kumar Pandey

Former Member
0 Kudos

Hi Praveen ,

I commented my earlier sort() method in TablewSorter.java class . and added following method given by you.

public void sort(IWDCustomEvent wdEvent, IWDNode dataSource,String columnId,String direction) {

IWDTableColumn column = (IWDTableColumn) table.getView().getElement(columnId);

NodeElementByAttributeComparator elementComparator = (NodeElementByAttributeComparator) comparatorForColumn.get(column);

if (elementComparator == null){

//not a sortable column

column.setSortState(WDTableColumnSortDirection.NOT_SORTABLE);

return;

}

// sorting

elementComparator.setSortDirection(WDTableColumnSortDirection.valueOf(direction));

dataSource.sortElements(elementComparator);

}

its gicing me error at line

elementComparator.setSortDirection(WDTableColumnSortDirection.valueOf(direction));

dataSource.sortElements(elementComparator);

as setSortDirection(........) method unknown.

This is my TableSorter.java class i m using for table sorting

/***************************************************************************************************/

/*

  • Created on Oct 29, 2007

*

  • To change the template for this generated file go to

  • Window&gt;Preferences&gt;Java&gt;Code Generation&gt;Code and Comments

*/

package com.apl.util;

import java.util.Comparator;

import java.util.HashMap;

import java.util.Iterator;

import java.util.Map;

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

import com.sap.tc.webdynpro.progmodel.api.IWDAction;

import com.sap.tc.webdynpro.progmodel.api.IWDCustomEvent;

import com.sap.tc.webdynpro.progmodel.api.IWDNode;

import com.sap.tc.webdynpro.progmodel.api.IWDNodeElement;

import com.sap.tc.webdynpro.progmodel.api.IWDViewElement;

/**

  • @author Admin

*

  • To change the template for this generated type comment go to

  • Window&gt;Preferences&gt;Java&gt;Code Generation&gt;Code and Comments

*/

public final class TableSorter {

public TableSorter(

IWDTable table,

IWDAction sortAction,

Comparator[] comparators) {

this.table = table;

// sanity checks

if (sortAction == null)

throw new IllegalArgumentException("Sort action must be given");

if (table == null)

throw new IllegalArgumentException("Table must be given");

if (table.bindingOfDataSource() == null)

throw new IllegalArgumentException(

"Data source of table with id '" + table.getId() + "' must be bound");

String dataSourcePrefix = table.bindingOfDataSource() + ".";

int index = 0;

//for (Iterator it = table.iterateGroupedColumns(); it.hasNext(); ++index) {

for (Iterator it = table.iterateGroupedColumns(); it.hasNext(); ++index) {

// for every column: try to make it bindable

IWDTableColumn column = (IWDTableColumn) it.next();

//IWDTableColumnGroup column = (IWDTableColumnGroup) it.next();

Comparator comparator = null;

if (comparators != null && index < comparators.length)

comparator = comparators[index];

ReversableComparator reversable = null;

if (comparator instanceof NodeElementByAttributeComparator) {

// the easy one, attribute and ordering are given

reversable = new ReversableComparator(comparator);

} else { // attribute must be determined

String bindingOfPrimaryProperty =

bindingOfPrimaryProperty(column.getTableCellEditor());

//bindingOfPrimaryProperty(column.getTableColumnGroup());

if (bindingOfPrimaryProperty == null

|| !bindingOfPrimaryProperty.startsWith(dataSourcePrefix))

continue; // no attribute found or outside of data source

String attributeName =

bindingOfPrimaryProperty.substring(dataSourcePrefix.length());

if (attributeName.indexOf('.') >= 0)

continue;

// attribute not immediately below data source (TODO handle this)

reversable = new ReversableComparator(new NodeElementByAttributeComparator(attributeName, comparator));

}

// set up internal data structures

comparatorForColumn.put(column, reversable);

// prepare table column

column.setOnAction(sortAction);

column.mappingOfOnAction().addSourceMapping("col", "col");

if (column.getHeader() != null)

column.getHeader().setImageSource(null);

}

}

/**

  • This method must be called from the event handler of this table sorter's

  • sort action. It performs the actual sort operation.

*/

public void sort(IWDCustomEvent wdEvent, IWDNode dataSource,String columnId,String direction) {

IWDTableColumn column = (IWDTableColumn) table.getView().getElement(columnId);

NodeElementByAttributeComparator elementComparator = (NodeElementByAttributeComparator) comparatorForColumn.get(column);

if (elementComparator == null){

// not a sortable column

column.setSortState(WDTableColumnSortDirection.NOT_SORTABLE);

return;

}

// sorting

elementComparator.setSortDirection(WDTableColumnSortDirection.valueOf(direction));

dataSource.sortElements(elementComparator);

}

/* public void sort(IWDCustomEvent wdEvent, IWDNode dataSource) {

if (dataSource != null) {

// find the things we need

String columnId = wdEvent.getString("col");

IWDTableColumn column =

(IWDTableColumn) table.getView().getElement(columnId);

ReversableComparator reversable =

(ReversableComparator) comparatorForColumn.get(column);

if (reversable == null)

return; // not a sortable column

// remove icon of previously sorted column

if (currentlySortedColumn != null

&& currentlySortedColumn.getHeader() != null)

currentlySortedColumn.getHeader().setImageSource(null);

// bookkeeping

if (column == currentlySortedColumn)

reversable.toggleReversed();

currentlySortedColumn = column;

// set icon in currently sorted column

if (currentlySortedColumn.getHeader() != null)

currentlySortedColumn.getHeader().setImageSource(

reversable.isReversed()

? "~sapicons/s_b_srtd.GIF"

: "~sapicons/s_b_srtu.GIF");

// sorting

dataSource.sortElements(reversable);

}

} */

/**

  • Returns the binding of the given table cell editor's property that is

  • considered "primary" or <code>null</code> if no such binding exists or no

  • such property can be determined.

*/

private static final String bindingOfPrimaryProperty(IWDTableCellEditor editor) {

return editor instanceof IWDViewElement ? bindingOfPrimaryProperty((IWDViewElement) editor) : null;

}

/**

  • Returns the binding of the given view element's property that is

  • considered "primary" or <code>null</code> if no such binding exists or no

  • such property can be determined.

*/

private static final String bindingOfPrimaryProperty(IWDViewElement element) {

if (element instanceof IWDAbstractDropDownByIndex)

return ((IWDAbstractDropDownByIndex) element).bindingOfTexts();

if (element instanceof IWDAbstractDropDownByKey)

return ((IWDAbstractDropDownByKey) element).bindingOfSelectedKey();

if (element instanceof IWDAbstractInputField)

return ((IWDAbstractInputField) element).bindingOfValue();

if (element instanceof IWDCaption)

return ((IWDCaption) element).bindingOfText();

if (element instanceof IWDCheckBox)

return ((IWDCheckBox) element).bindingOfChecked();

if (element instanceof IWDLink)

return ((IWDLink) element).bindingOfText();

if (element instanceof IWDProgressIndicator)

return ((IWDProgressIndicator) element).bindingOfPercentValue();

if (element instanceof IWDRadioButton)

return ((IWDRadioButton) element).bindingOfSelectedKey();

if (element instanceof IWDTextEdit)

return ((IWDTextEdit) element).bindingOfValue();

if (element instanceof IWDTextView)

return ((IWDTextView) element).bindingOfText();

return null;

}

/**

  • Instance of a comparator according to the ordering imposed by the

  • implementation of <code>Comparable</code>.

*/

private static final Comparator DEFAULT = new Comparator() {

/**

  • Compares the given objects according to the ordering imposed by the first

  • ones <code>compareTo(Object)</code> function. Furthermore, <code>null</code>

  • is treated to be less than any object.

  • @see java.lang.Comparable#compareTo(java.lang.Object)

  • @see java.util.Comparator#compare(java.lang.Object, java.lang.Object)

*/

public int compare(Object o1, Object o2) {

if (o1 == null && o2 == null)

return 0;

if (o1 == null)

return -1;

if (o2 == null)

return +1;

return ((Comparable) o1).compareTo((Comparable) o2);

}

};

/**

  • Map of table column to comparator (<code>ReversableComparator</code>)

  • used for sorting that column (sortable columns only).

*/

private final Map comparatorForColumn = new HashMap();

/**

  • Column that is currently sorted.

*/

private IWDTableColumn currentlySortedColumn;

/**

  • The table to be sorted.

*/

private final IWDTable table;

/**

  • Generic comparator that compares node elements by a given attribute with

  • the help of a given comparator.

*/

public final class NodeElementByAttributeComparator implements Comparator {

/**

  • Creates a new comparator for the given attribute name that compares values

  • of that attribute according to the natural ordering of that attribute's

  • type (which must implement <code>java.lang.Comparable</code>).

*/

public NodeElementByAttributeComparator(String attributeName) {

this(attributeName, null, false);

}

/**

  • Creates a new comparator for the given attribute name that compares values

  • of that attribute with the help of the given comparator. If no comparator

  • is given, the natural ordering of that attribute's type is used.

*/

public NodeElementByAttributeComparator(

String attributeName,

Comparator comparator) {

this(attributeName, comparator, false);

}

/**

  • Creates a new comparator for the given attribute name that compares values

  • of that attribute either as objects (i.e. "in internal format") or as text

  • (i.e. "in external format") as indicated. The ordering is the natural

  • ordering of that attribute's type (which must implement

  • <code>java.lang.Comparable</code>) in case objects are compared or the

  • natural ordering of <code>java.lang.String</code> in case texts are compared.

*/

public NodeElementByAttributeComparator(

String attributeName,

boolean compareAsText) {

this(attributeName, null, compareAsText);

}

/**

  • Internal constructor.

*/

private NodeElementByAttributeComparator(

String attributeName,

Comparator comparator,

boolean compareAsText) {

if (attributeName == null)

throw new IllegalArgumentException("Attribute name must not be null");

if (comparator == null)

comparator = DEFAULT;

this.attributeName = attributeName;

this.comparator = comparator;

this.compareAsText = compareAsText;

}

/**

  • Compares the given objects which must be instances of <code>IWDNodeElement</code>

  • according to the values of the attribute given at construction time

  • with the help of the comparator given at construction time.

  • @see java.util.Comparator#compare(java.lang.Object, java.lang.Object)

  • @see com.sap.tc.webdynpro.progmodel.api.IWDNodeElement

*/

public int compare(Object o1, Object o2) {

IWDNodeElement element1 = (IWDNodeElement) o1;

IWDNodeElement element2 = (IWDNodeElement) o2;

Object attributeValue1 =

compareAsText

? element1.getAttributeAsText(attributeName)

: element1.getAttributeValue(attributeName);

Object attributeValue2 =

compareAsText

? element2.getAttributeAsText(attributeName)

: element2.getAttributeValue(attributeName);

return comparator.compare(attributeValue1, attributeValue2);

}

/**

  • Name of the attribute used for comparisons.

*/

private final String attributeName;

/**

  • Comparator used for comparing the attribute's values.

*/

private final Comparator comparator;

/**

  • Indicates whether attribute values are compared as text (as opposed to

  • "as objects").

*/

private final boolean compareAsText;

}

/**

  • Comparator that knows how to reverse another comparator's sort direction.

*/

private final class ReversableComparator implements Comparator {

/**

  • Creates a new reversable comparator for the given other comparator.

*/

public ReversableComparator(Comparator comparator) {

if (comparator == null)

throw new IllegalArgumentException("Comparator must not be null");

this.comparator = comparator;

}

/**

  • @see java.util.Comparator#compare(java.lang.Object, java.lang.Object)

*/

public int compare(Object o1, Object o2) {

return reversed

? -comparator.compare(o1, o2)

: comparator.compare(o1, o2);

}

/**

  • Tells whether the other comparator's sort direction is reversed.

*/

public boolean isReversed() {

return reversed;

}

/**

  • Determines whether the other comparator's sort direction is reversed.

*/

public void setReversed(boolean reversed) {

this.reversed = reversed;

}

/**

  • Toggles the current "sort direction".

*/

public void toggleReversed() {

reversed = !reversed;

}

/**

  • The other comparator.

*/

private final Comparator comparator;

/**

  • Whether the other comparator's sort direction is reversed.

*/

private boolean reversed;

}

}

Former Member
0 Kudos

Hi Kavita,

If u follow the steps available in<a href="/people/bertram.ganz/blog/2006/03/07/enhanced-web-dynpro-java-tablesorter-for-sap-netweaver-04s sorter</a> ,it will surely work.

regards

Sumit

Former Member
0 Kudos

Hi Kavita,

Try looking in the Web Dynro for Java forum - you'll have much more luck searching there as there has been a lot of questions about the TableSorter.

Also, there is a blog on here somewhere that walks through how to use the TableSorter class - I don't have a link right now but yuo should be able to find it.

Gareth.