cancel
Showing results for 
Search instead for 
Did you mean: 

Table Sorting

Former Member
0 Kudos

Hi all,

I am using TableSorter.java to sort my table colums.

But the problem here is all colums are getting that option of sort.But i want only for 2 colums.

IS there any way to restrict the number of colums that wants sorting option.

Regards

Padma N

Accepted Solutions (1)

Accepted Solutions (1)

Former Member
0 Kudos

Hi Padma,

In the wdDoModifyView() method pass the column names hat you want sort to a method <setCustomerTableSorter>(context node that you have declared). Marked bold in the below code.

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

}

If you want to sort all the columns then pass null instead of column names.

For more information refer to this blog which will definetly help you.

/people/bertram.ganz/blog/2006/03/07/enhanced-web-dynpro-java-tablesorter-for-sap-netweaver-04s

Thanks n Regards,

Jhansi Miryala

Former Member
0 Kudos

HI Jhansi,

if i pass all the column names which i want to sort as new String[]{Column 1,column 2}

then wat i have to pass as parameters for the mrthod sort()

Regards

Padma N

Former Member
0 Kudos

Hi Padama,

You just need to pass your node to method sort.See the 7th point in the Blog.

7. 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 onActionSort<Customers>(com.sap.tc.webdynpro.progmodel.api.IWDCustomEvent wdEvent )

{

//@@begin onActionSort<Customers>(ServerEvent)

wdContext.currentContextElement().get<CustomerTableSorter()>.sort(wdEvent, wdContext.node<Customers()>);

//@@end

}

Replace the context element and node name in the above code with your context element of type TableSorter and your table node.

Thanks n Regards,

Jhansi Miryala

Former Member
0 Kudos

Hi Jhansi,

My requirement here is i ahve a table with column names as column1,column2,column3,column4

I want the sorting option only for column2,column3

The code i wrote in wdModify is

if(firstTime)

* {*

* IWDTable table=(IWDTable)view.getElement("Table")*

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

* }*

the code in sort is

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

{

//@@begin onActionsort(ServerEvent)

* wdContext.currentContextElement().getTableSorter().sort(wdEvent, wdContext.nodeTableData());*

//@@end

}

the ouput is the sorting option is comming for all the colums of the table

But i want only for column2,column3

can u plz help me out

Regards

Padma N

Former Member
0 Kudos

Padma,

Modify your code in the wdModify method, here pass your column names Column2","Column3" as the parameters.

IWDTable table = (IWDTable) view.getElement("Table");

wdContext.currentContextElement().setTableSorter(

new TableSorter(table, wdThis.wdGetSortAction(), null, new String[] { "Column2","Column3" }));

Replace the code in wdModify method with the above specified code.

Thanks n Regards,

Jhansi Miryala

Former Member
0 Kudos

Hi Jhansi,

But there is no TableSorter constructor with 4 parameteres and new String[] menas string array as an argumnet in the constructor

It is giving error as new TableSorter(table, wdThis.wdGetSortAction(), null, new String[] { "Column2","Column3" })); is undefind

Regards

Padma N

former_member201361
Active Contributor
0 Kudos

hi,

U might have created a TableNode and Attributes for the Table . now for sorting only the two columns

create another Context Attribute and attribute same as the Attribute for the two column(even the type should be same).

in the domodify method :

if (firstTime ) {

IWDTable table = (IWDTable) view.getElement("Id of the table");

wdContext.currentContextElement().setTableSorter(

new TableSorter(table, wdThis.wdGetOnSortAction(),new Hashtable()));

}

and in the actionsort method

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

Hope this will solve ur problem .

Thanks and regards

Fistae

Former Member
0 Kudos

Hi Fistae,

Could you please tell me where we have to pass the names of the columns to be sorted in the code given by you.

Is it that i have to create a separate node with the colums to be sorted with type and everything same as the table columns

if (firstTime ) {

IWDTable table = (IWDTable) view.getElement("Id of the table");

wdContext.currentContextElement().setTableSorter(

new TableSorter(table, wdThis.wdGetOnSortAction(),new Hashtable()));

}

and in the actionsort method

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

Regards

Padma N

former_member201361
Active Contributor
0 Kudos

hi,

No need to give any column names in the sort action , as per requirements u need only two columns to be sorted and create a new node and the copy the attrinbutes which u have binded to the two column.

in the sort method u have to give the new node which has the column attribute which has to be Sorted in runtime.

Thanks and regards

Fistae

Former Member
0 Kudos

Hi ,

i am getting the table data from the function module.the output of the function module is Table which is mapped to the table UIElement at the design time.how is it possible to crate anothe node outside with the value attributes same as columns which i want to sort.

Now which node i have to map to the table

Even though i created a seperate node with colums how can it contain the data for the table

Regards

PadmaN

former_member201361
Active Contributor
0 Kudos

hi,

Ya , u r right it cant be done but u can send the column names to the Constructor of the table sorter .

please check in the table sorter class u have got the constructor with 4 parameters .

public TableSorter(IWDTable table, IWDAction sortAction, Map comparators, String[] sortableColumns) {

init(table, sortAction, comparators, sortableColumns);

}

thanks and regards

Fistae

former_member201361
Active Contributor
0 Kudos

hi,

As Jhansi said there should be another constructor in the Table sorter where we can feed the column names for the sort action.

thanks and regards

Fistae

Former Member
0 Kudos

Hi,

I am getting the same error.

Can u plz check my Tablesorter.java file once.

In that there is no method which you said

public TableSorter(IWDTable table, IWDAction sortAction, Map comparators, String[] sortableColumns) {

init(table, sortAction, comparators, sortableColumns);

}

MY TableSorter.java file is given below.

package com.sap.tut.webdynpro.tab.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.IWDTextEdit;

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

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;

public final class TableSorter {

* /***

* * Creates a table sorter for the given table using the given sort action.*

* * This constructor must be called from <code>wdDoModifyView()</code>, but*

* * usually only when that hook is called for the first time. Store the newly*

* * created instance in a context attribute with Java native type*

* * <code>com.sap.tc.webdynpro.tests.utils.TableSorter</code>.*

* * The given sort action's event handler will be bound to the <code>onAction</code>*

* * event of sortable columns and must at least call this table sorter's*

* * <code>sort(wdEvent)</code> method.*

* **

* * Every column of the table is made sortable if possible according to the*

* * following rules.*

* * If a comparator is given for a column's index and it is a*

* * <code>NodeElementByAttributeComparator</code>, then that comparator defines*

* * both the attribute and the ordering used to sort that column.*

* * If any other comparator is given and an attribute can be determined from*

* * that column's table cell editor, then that attribute is used to sort that*

* * column according to the ordering imposed by the given comparator.*

* * If no comparator is given but an attribute can be determined from*

* * that column's table cell editor, then that attribute is used to sort that*

* * column according to the natural ordering of that attribute's type.*

* * Else that column is left untouched.*

* **

* * When a column is made sortable its <code>onAction</code> event (incl.*

* * parameter mapping) is changed. Furthermore if a header is present for that*

* * column then the header's <code>imageSource</code> property is also changed*

* * to indicate the currently sorted column and its sort direction.*

* **

* * @see sort()*

* * @see NodeElementByAttributeComparator*

* * @see com.sap.tc.webdynpro.clientserver.uielib.standard.api.IWDTable*

* /

* 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.iterateColumns(); it.hasNext();++index)*

* { // for every column: try to make it bindable*

* IWDTableColumn column = (IWDTableColumn) 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());*

* 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) {*

* 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;*

* if (o1 instanceof Boolean && o2 instanceof Boolean)*

* return o1.toString().compareTo(o2.toString()); // false < true*

* 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;*

* }*

}

Regards

Padma N

former_member201361
Active Contributor
0 Kudos

hi,

Please refer think for table sorting where the table column names are fed to enable sorting for those columns only .

[https://www.sdn.sap.com/irj/sdn/weblogs?blog=/pub/wlg/3287] [original link is broken] [original link is broken] [original link is broken];

Thanks and regards

Fistae

Former Member
0 Kudos

Hi ,

I have document on table filter / sorting , which can help u on to slove u r issue.

give mail i will send you.

Thanks,

Beat regards.

EP

Former Member
0 Kudos

Hi Fistae,

Is it that this Enhanced Web Dynpro Java TableSorter will work for the NWDS version starting 4.0

In the WDModifyView my code is

if (firstTime) {

IWDTable table = (IWDTable) view.getElement("CustomerTable");

wdContext.currentContextElement().setCustomerTableSorter(

new TableSorter(table, wdThis.wdGetSortCustomersAction(), null, new String[] { "Column1" }));

Column1 is in table name "TableData"

i didnt change any sort method code..

IS it that i have to map the sort action to the onAction event of that table

Regards

Padma N

former_member201361
Active Contributor
0 Kudos

hi,

I think it will work ,are u not able to follow the blog for the table sorting? . and NOTE : U have replace the prevoius TableSorter class with the new One from the web blog.

Thanks and Regards

Fistae

Former Member
0 Kudos

Hi Fistae,

I replaced the TableSorter.java with the version from the bolg.I am getting error at

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

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

public void setSortDirection(WDTableColumnSortDirection direction){

if(direction.equals(WDTableColumnSortDirection.UP)){

sortDirection = true;

}else if(direction.equals(WDTableColumnSortDirection.DOWN)){

sortDirection = false;

}

}

The NWDS version which i am using is 2.0.7

IS it the problem???

former_member201361
Active Contributor
0 Kudos

hi,

For the NWDS 2.0.7 the Table sorter will not work.

I am not sure for the version we can sort the columns(Required) for the table.(The table sorter will work NWDS 04)

Thanks and regards

Fistae

Answers (2)

Answers (2)

Former Member
0 Kudos

hi,

u can do this way

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

{

//@@begin onActionsort(ServerEvent)

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

//@@end

}

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

{

//@@begin wdDoModifyView

if(firstTime)

{

IWDTable table = (IWDTable) view.getElement("Table");

wdContext.currentContextElement().setTableSorter(

new TableSorter(table, wdThis.wdGetSortAction(), null, new String[] { "Column2","Column3" }));

}

//@@end

}

Regards

Trilochan

Edited by: Trilochan Bagauli on Apr 30, 2008 8:46 AM

Former Member
0 Kudos

Hi Jhansi,

First of all it is giving error as there is no constructor of new Tablesorter() with 4 paramneters.

and by the way how to refer to the colum 1 of a particular table

see i have all the colums in node TableData.

If i say directly new String{column1,column2}

how it knows that i am referring to those colums of a particular table

Regards

Padma N

0 Kudos

hi,

u can try this

wdContext.currentContextElement().getattribute<>().sort(wdEvent,wdContext.<node>());