cancel
Showing results for 
Search instead for 
Did you mean: 

Table sort is not working for columns.

Former Member
0 Kudos

Hi,

I am using TableSort.java class. Followed https://www.sdn.sap.com/irj/servlet/prt/portal/prtroot/com.sapportals.km.docs/library/user-interface...

to create the action and assigned that to onSort event for the table. When I run, I see the ascending-descending icons besides the columns, but nothing happens when I click them. Here is the context.

Context

l

l

l ---User_Table

> Email

> Name

|

> Office

Here Name is a custom string (last name, first name). Also office is a custom string (office1, office2, ...etc).

Edited by: srinivas M on Feb 8, 2009 6:03 AM

Edited by: srinivas M on Feb 8, 2009 6:03 AM

Accepted Solutions (1)

Accepted Solutions (1)

Former Member
0 Kudos

HI

For TableSorting you have to provide TableSort.java class in the src->pkg , this java class file you can

find in the examples of the IDE or in the SDN.

then you follow the procedure provided in the SDN link , but make the changes w.r.t requirement

that is if you want on Table Sorting

write the code same as in wdModify..() and in the TableSorter onAction() ,

Thanks

Former Member
0 Kudos

I followed the procedure. But nothing seems to work. I can see the ascending-descending arrows besides the column headers. When I click them, nothing happens. Here is the code in wdDoModify()


if (firstTime) {

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

		  wdContext.currentContextElement().setTableSorter(

			new TableSorter(table, wdThis.wdGetSortAction(), null));

	} 

And here is the code in the action that I linked with the onSort Event of the table


 g_message_manager.reportSuccess("Sort is in progress: ");
	wdContext.currentContextElement().getTableSorter().sort(wdEvent, wdContext.nodeUser_Search_Results());

The message gets printed whenever I click on the ascending-descending icons (on all column headers).

My original requirement is to sort the results by default (the first time the rows are returned) on a column. Does it make sense to associate the action with the table or associate with the column I am interested in?

sanyev
Active Participant
0 Kudos

Hi Srinivas,

Your code looks fine. You can also go thorough this [Weblog|https://www.sdn.sap.com/irj/scn/weblogs?blog=/pub/wlg/3287] [original link is broken] [original link is broken] [original link is broken]; for more info about TableSorter class.

If you go through the comment in the TableSorter.java class there are some conditions given for sorting of a perticular column.

-


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

  • following rules.

  • If a comparator is given for a column's ID 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.

  • Additionally it is possible to define the sortable columns by their

  • TableColumn UI element ids.

  • @see sort()

  • @see NodeElementByAttributeComparator

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

-


If you are using some special data types then you need to pass the appropriate comparators to the TableSorter class. Go through the TableSorter.java class, details are given there. If it is plain string then it should work properly. There is a possibility that it is a problem with the comparator. Regarding the User_Search_Results node, I believe that this node is bound to the table. But in the context diagram you are talking about a node called User_Table. What the table sorter class does is when you call tableSorter.sort(wdEvent, node) it will extract the columnID and sortDirection from the wdEvent and then does a sort on the node provided based in the columnID. You have to make sure that the node you provide to the TableSorter classes sort method should be the node that is bound to the table. Else you might be sorting a different node and the changes will not be reflected in the table.

If you want to initially sort the table you still can use the same TableSorter class but you need to modify the code in the table sorter class. The sort(event, node) basically does the sorting. From the event it extracts the columnID and sortDirection. What you need to do is to create a InitialSort method and copy the content of the sort method. Instead of extracting the information from the wdEvent you can hardcode the columnID and the sortDirection. You can use [WDTableColumnSortDirection|http://help.sap.com/javadocs/NW04S/SPS09/wd/com/sap/tc/webdynpro/clientserver/uielib/standard/api/WDTableColumnSortDirection.html] class for the sort direction. Once this new method is created you can call this method from the wdDoModyfyView first time just after initializing the instance of table sorter class.

Hope this helps

Sanyev

Former Member
0 Kudos

Hi Sanyev,

Thanks for your reply. But it is not very clear to me. Can you provide code if possible. I somehow cannot make the sort work for any column. I would like to implement sort on only one column. Does this mean, I shouldn't use the onSort event of the table and rather use onAction of the column?

Thanks

Srinivas

sanyev
Active Participant
0 Kudos

Hi Srinivas,

If you want to do an initial sort. You have to add the following method to the TableSorter class.

public void initialSort(String columnId, IWDNode dataSource) {
		// find the things we need
		String direction = WDTableColumnSortDirection.UP;
		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);
	}

In your wdDoModifyView() after initializing the tablesorter class you have to call the above method.

if (firstTime) {
		  IWDTable table = (IWDTable) view.getElement("Table");
		  wdContext.currentContextElement().setTableSorter(
			new TableSorter(table, wdThis.wdGetSortAction(), null));
                  wdContext.currentContextElement().getTableSorter().initialSort("Your Column ID", wdContext.nodeUser_Search_Results());
	}

Can you double check in your code if the table is bound to the node "User_Search_Results" and not "User_Table". If the table is bound to the "User_Table" then the sort will not work since in the code you are sorting the node "User_Search_Results".

If you want to implement sort on only one column you can use the alternate constructor for the TableSorter class.

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

You have to give a String array of columns that need to be sort enabled.

Regards,

Sanyev

Former Member
0 Kudos

Hi Sanyev,

I added the code to TableSorter class. Now I get 2 errors.

"Type Mismatch: cannot convert from WDTableColumnSortDirection to String"

at line

String direction = WDTableColumnSortDirection.UP;

"The method setSortDirection(WDTableColumnSortDirection ) is undefined for the type TableSorter.NodeElementByAttributeComparator."

at line

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

I organized imports and tried typecasting. Still the above 2 errors persist.

The table is bound to the node "User_Search_Results".

sanyev
Active Participant
0 Kudos

Hi Srinivas,

I did not have access to an IDE yesterday to write the code. I have corrected the errors.

public void initialSort(String columnId, IWDNode dataSource) {
		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
                // the direction could be UP or DOWN depending on your requirement.
		elementComparator.setSortDirection(WDTableColumnSortDirection.UP);
		dataSource.sortElements(elementComparator);
	}

Let me know if this works.

Regards,

Sanyev

Former Member
0 Kudos

Hi Sanyev,

I am not sure if this is right in my case in wdDoModify():

if (firstTime) {
		  IWDTable table = (IWDTable) view.getElement("Table");
		  wdContext.currentContextElement().setTableSorter(
			new TableSorter(table, wdThis.wdGetSortAction(), null));
                  wdContext.currentContextElement().getTableSorter().initialSort("Name", wdContext.nodeUser_Search_Results());
	}

When I run with above code, I don't see the sort direction icon beside the column that I am interested in (Name).

The other columns have the sort direction but nothing happens when I click them.

I had to write the following code in the NodeElementByAttributeComparator class in TableSorter.java, along with initialSort that you told.


public void setSortDirection(WDTableColumnSortDirection direction){
	if(direction.equals(WDTableColumnSortDirection.UP)){
		sortDirection = true;
	}else if(direction.equals(WDTableColumnSortDirection.DOWN)){
		sortDirection = false;
	}
	}
	
	private boolean sortDirection;

sanyev
Active Participant
0 Kudos

Srinivas,

Can you confirm if "Name" is the columnID of the TableColumn inside the table where you need sorting? You can verify it from the list of UI elements. Inside your table you can find the table columns. You have to give the table column ID in the initialSort method.

If the TableColumn ID is correct then you need to debug the TableSorter class initialSort method and see if the element comparator is coming as null?

Sanyev

Former Member
0 Kudos

Hi Sanyev,

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

I used the TableSorter.java class from the project mentioned in the blog. It worked.

Srinivas

Edited by: srinivas M on Feb 9, 2009 9:02 PM

sanyev
Active Participant
0 Kudos

Hi Srinivas,

For sorting to work with the TableSorter class you only need to follow the document you mentioned. You do not need any additional code in the Table Sorter class. The code that I mentioned earlier is for enabling initial sorting.

Since you have implemented what ever was there in the document there is obviously some problem in the node. I would recommend that you debug the table sorter class and see the working of the elementComparator.

Basically the TableSorter class does a sort on the context node provided. The api used is

node.sortElements(elementComparator);

Where elementComparator is the implementation of the Comparator interface which compare two IWDNodeElements.

If your Webdynpro context node that you are trying to sort is a value node and the attributes are all type strings then there should not be any problem. TableSorter class should be able to sort the collection with out any problem.

If your Webdynpro context node that you are trying to sort is a model node. There it might happen that the result of the model execution is a readonly immutable collection. Then the sorting will fail. You will have to check if your model is readonly in the backend.

Or there could be a problem with the comparator of your column. TableSorter is not able to extract the comparator properly.

De mention in your reply if you are using model node or value node.

Sanyev

sanyev
Active Participant
0 Kudos

Ok.... You were using a different version of the TableSorter class. I didn't think about that.

Good to know that your problem is solved.

Sanyev

Answers (1)

Answers (1)

Former Member
0 Kudos

Plz have a look at the below thread