cancel
Showing results for 
Search instead for 
Did you mean: 

Dynamic Tabel Sorting

Former Member
0 Kudos

Hello,

I have an issue by applying a sorter for a dynamic table. In general i followed the bolg by Bertram (<a href="/people/bertram.ganz/blog/2006/03/07/enhanced-web-dynpro-java-tablesorter-for-sap-netweaver-04s). But when i test this, the Sort buttons are displayed, but when i click on them the table is not getting sorted.

I debugged the tableSorter.java and i found out that the elementComparator is empty so that the method "sort" in tableSorter.java is been canceled.

Does anyone have a solution for this issue? I am working on NW2004s SP 12.

Thanks in advance for any suggestions,

Best regards,

Rene

Accepted Solutions (1)

Accepted Solutions (1)

Former Member
0 Kudos

Hi rene,

Can u provide the code for sorting,so we can find the problem?

regards

Sumit

Former Member
0 Kudos

Hi Sumit,

thanks for the answer, please see blow for the code:

wdDoModifyView:


if (firstTime){
   view.nowCreateAllCustomExtensionFields();
   IWDTable table = (IWDTable) view.getElement("LOPTable");
   wdContext.currentContextElement().setTableSorter(
	  new TableSorter(table, wdThis.wdGetSortTableAction(), null)) ;
}

wdGetSortTableAction():


IWDNode dynNode = wdContext.wdGetAPI().getRootNode().getChildNode("dynamicTable", IWDNode.LEAD_SELECTION);
wdContext.currentContextElement().getTableSorter().sort(wdEvent, dynNode);

The node is a dynamic node, so that i am not able to "browse" over wdContext to the node.

Thanks in advance for any help.

best regards,

Rene

Former Member
0 Kudos

Hi Rene,

When you call the TableSorter constructor, it tries to add comparators for each of the columns based on the node attribute to which the cell editor for each column is bound. All this is done based on the datasource node of the table.

In your case, the question is where are you binding the dynamic node as the datasource of the table? If you are doing it any later than the TableSorter is initialized, then this is not available when the TableSorter is initialized. Hence it cannot set a comparator to any of the columns and it marks all columns as "<i>NOT_SORTABLE</i>".

Regards,

Satyajit.

Former Member
0 Kudos

Hi Satyajit,

thanks for the informative answer. know i understand more of this issue.

I create the dynamic node and add attributes to it in wdDoInit(). The table itself is been created in wdDoModifyView. In my understanding wdDoInit is executed first after wdDoModifyView is executed. in wdDoModifyView i call a method for binding the dynamic node with the table. after this i set and initialize the table sorting.

I think i ordered everything in the right direction. am I missing something?

best regards,

rene

Former Member
0 Kudos

Hi Rene,

Where r u passing column names to table sorter ???

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

}

Code should be like this .isn't it??

regards

Sumit

Former Member
0 Kudos

Hi Sumit,

the new for NW2004s table sorter class sorts all tables if you do not specify any table names. anyway, tried to provide table columns to the sorter without any changes.

I used this method for design time declared tables before and it is working fine.

best regards,

rene

Former Member
0 Kudos

Hi Rene,

That looks alright. Can you just debug this part of TableSorter.java?

String bindingOfPrimaryProperty = bindingOfPrimaryProperty(column.getTableCellEditor());
if (bindingOfPrimaryProperty == null || !bindingOfPrimaryProperty.startsWith(dataSourcePrefix)){
//no attribute found or outside of data source
column.setSortState(WDTableColumnSortDirection.NOT_SORTABLE);
continue;
}

Check if the <i>bindingOfPrimaryProperty</i> and <i>dataSourcePrefix</i> have proper values. dataSourcePrefix should have a value like "<i><Dynamic node name>.</i>" and bindingOfPrimaryProperty should have a value like "<i><Dynamic node name>.<Dynamic attribute name></i>".

Regards,

Satyajit.

Former Member
0 Kudos

Hi Satyajit,

that's looks all right for me, the bindingOfPrimaryProperty= "dynamicTable.Punkt" that's the table and column i tried to sort and the dataSourcePrefix= "dynamicTable." that is ok as well. when continuing debugging the column.setSortState(WDTableColumnSortDirection.NOT_SORTABLE); is not set.

very nice try. The columns are set properly.

any other suggestions?

thank you,

best regards,

Rene

Former Member
0 Kudos

Hi Rene,

Hmmm...

In the sort method of TableSorter, in the line

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

what's the value of <i>column</i> and what is contained in the map <i>comparatorForColumn</i>? Can you debug and see?

Regards,

Satyajit.

Former Member
0 Kudos

Hi Satyajit,

i debugged the code. the elementComparator is null, because the needed column is null


IWDTableColumn column = (IWDTableColumn) table.getView().getElement(columnId);
		NodeElementByAttributeComparator elementComparator = (NodeElementByAttributeComparator) comparatorForColumn.get(column);

The column is null, but the String from the Event is equal to the ColumnID. When create dynamic table is there a way to find dynamic columns via table.getView...?

The second way you suggested is really primitive and i will try this only if i really have to.

Thanks for the help in this issue Satyajit,

best regards,

Rene

Former Member
0 Kudos

Hi Rene,

My last effort Replace the code for the sort method of the TableSorter class with the following code:

public void sort(IWDCustomEvent wdEvent, IWDNode dataSource) {
// find the things we need
String columnId = wdEvent.getString("selectedColumn");
String direction = wdEvent.getString("sortDirection");
		
IWDTableColumn column = null;
		
int colCount = table.getColumns().length;
		
for(int i = 0; i < colCount; i++){
IWDTableColumn col = (IWDTableColumn)table.getColumn(i);
			
if(columnId.equals(col.getId())){
column = col;
break;
}
}
		
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);
}

After you have done this, please debug and check whether the column is still null.

Regards,

Satyajit.

Former Member
0 Kudos

Hi Satyajit,

the problem is that table.getColumns().length = 0.

so there are no table columns provided. I will see how to get the column to the comparator.

I can't figure out how there can't be columns. I guess the problem is somewhere else.

Satyajit, thank you very much for your help.

best regards,

Rene

Former Member
0 Kudos

Hi,

If you are adding grouped columns to the table, then you can also try,

table.getGroupedColumns().length;

Regards,

Satyajit.

Former Member
0 Kudos

Hi Satyajit,

that's it! i can sort. I still have some weird behavior but i am sure i can solve it now.

Thanks again Satyajit for your effort you out into my issue!

best regards,

Rene

Former Member
0 Kudos

Hi Rene

can you tell me what your weird behaviour looks like?

i had the same problem with sorting but managed it to

sort the tables properly.

but if i change the leadSelection of the table it wont sort

anymore until i change the leadSelection back to the

initial state.

i used 2 different versions of the TableSorter.java but its

always the same

but maybe this matches your weird problem and you

know the answer

best regards,

Norbert

Some additional informations:

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

{

//@@begin onActionSort(ServerEvent)

try{

logger.infoT(CAT,"normal:"+wdEvent.getString("col"));

wdContext.currentCharsElement().

getTableSorter().

sort(wdEvent, wdContext.nodeChars());

}catch (Exception ex){

logger.fatalT(CAT,"EXCEPTION:"wdEvent.getString("col")"::"ex.getMessage()"klasse:"+ex.getClass().toString());

}

//@@end

}

this is the sort action, normaly it works perfect but when i change the leadselection of the table i catch an exception without a message:

com.karmann.table.test.TableView Sep 21, 2007 3:41:44 PM Fatal EXCEPTION:null::nullklasse:class java.lang.NullPointerException

Message was edited by:

Norbert Schlangen

Former Member
0 Kudos

hi,

in your code :

if (firstTime){

view.nowCreateAllCustomExtensionFields();

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

wdContext.currentContextElement().setTableSorter(

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

}

Please break it down to :

if (firstTime){

view.nowCreateAllCustomExtensionFields();

}

if (firstTime){

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

wdContext.currentContextElement().setTableSorter(

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

}

yugn siu wai

Former Member
0 Kudos

Hi Norbert,

sorry for the late reply. And i have to admit that i am not able to help you on your issue. My behavior is more that the sorting is done only sometimes and i have not worked on this issue since.

It looks to me that something in your TableSorter.java class is not correct. I would try to debug this class to analyze this exception.

best regards,

Rene

Former Member
0 Kudos

Hi Rene,

i finally fixed it, and maybe you got the same problem, since my table was also sorting sometimes and sometimes not.

in my first version the table displayed values directly mapped from the model and it had a structure like this:

ModelNode

|__ModelNode

|__ModelAttribute1

|__ModelAttribute1

when i changed it to:

ModelNode

|__ModelAttribute1

|__ModelAttribute1

it works fine, but finally i copied the mapped values from the model to a local context node of the View and use these values in my table (i implemented filtering so i needed it anyways):

ModelNode

|__ModelNode

|__ModelAttribute1

|__ModelAttribute1

ValueNode //Used in table

|__ValueAttribute1

|__ValueAttribute1

so now it works fine although i don't have a clou why it threw (invisible) NullPointerExceptions in the first version....

best regards

Norbert

Answers (2)

Answers (2)

Former Member
0 Kudos

Hi Rene,

Why dont you try to dynamically sort the table?

Write a method where you pass IWDView, Context for sorting and column to be sorted by.

In this method you can loop through the context and sort and refresh the table

I know this is a very primitive way...But if you dont find nething else....u can try this

Regards

Smruti

Former Member
0 Kudos

Hi experts,

does no one have an idea how to sort dynamic tables?

best regards,

Rene

achim_hauck2
Active Contributor
0 Kudos

Rene,

1: check, if your SortAction is called anyway (print a message like at the beginning of the Action)

2: if it is called, i guess, the reference to the table to be sorted is wrong. try to print out the size of the received table & check if its not null and contains entries.

kr, achim

Former Member
0 Kudos

Hi Achim,

thanks for your answer, but the method is called and the table is not null. I checked it via debugging.

do you have another idea? the process is being canceled in the sort method of the tabelSorter class at this code lines:

	if (elementComparator == null){
			//not a sortable column
			column.setSortState(WDTableColumnSortDirection.NOT_SORTABLE);
			return; 
		} 

the elementComparator is null.

thanks for any help,

best regards

Rene

Former Member
0 Kudos

Hi Rene,

While you create a columns dynamically set sort state for the column on which you are sorting to WDTableColumnSortDirection.UP or WDTableColumnSortDirection.DOWN as per your requirement.

Then try executing your application.

Regards,

Murtuza

Former Member
0 Kudos

Hi Murtuza,

thanks for the suggestion. I tried this, but it is not working. setting the sortstate of the column does not solve my issue,

thanks,

best regards,

Ren