on 03-18-2011 6:19 PM
Hi all
I have 2 scenarios which require sorting functionality:
1. I have a table and wanted to know if we can allow user to sort by all the column of the table rather than the first column alone for ex say col 1 col2 and col3 are there in a table then presently I am able to only sort by col 1 can this functionality be extended to allowing user to sort the data by whichever column he/she selects.
2. I am populating user id in a drop down from ume so can i have that data sorted in alphabetical order.
Kindly provide me some help on how I can go about.
Thank you
Regards,
Preet
Hi Preet,
Post the code you have written for sorting so that i can check it
Regards,
Amol
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi,
This link may help u .........
Regards,
Jaya
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi,
For adding sorting and filtering functionality to the table follow the article
[WDJ - A Generic Java Class for Filtering Web Dynpro Tables|http://www.sdn.sap.com/irj/scn/index?rid=/library/uuid/60d5b593-ba83-2910-28a9-a7a7c7f5996f]
For sorting a node that is used for displaying a dropdown, create a class implementing Comparator interface and use this class to sort the elements of the node so that the entries in the dropdown will be displayed in sorted order
import java.util.Comparator;
import com.sap.tc.webdynpro.progmodel.api.IWDNodeElement;
public class CustomComparator implements Comparator {
String attribute;
/* (non-Javadoc)
* @see java.util.Comparator#compare(java.lang.Object, java.lang.Object)
*/
public CustomComparator(String attrib) {
this.attribute = attrib;
}
public int compare(Object o1, Object o2) {
try
{
IWDNodeElement units1 = (IWDNodeElement) o1;
IWDNodeElement units2 = (IWDNodeElement) o2;
return units1.getAttributeAsText(attribute).compareTo(units2.getAttributeAsText(attribute));
}
catch (Exception e)
{}
//arbitrary order
return 1;
}
}
wdContext.nodeAllLookupValues().sortElements(new CustomComparator("Code"));
Once you execute sortElements method on the node, elements of the node will be sorted based on the 'Code' attribute of the node.
Regards,
Amol
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi Amol,
I refered to the link you sent, but it discusses about filtering. I need to sort the columns, I did find another link and wrote this code:
import java.util.Comparator;
import com.sap.tc.webdynpro.clientserver.uielib.standard.api.WDTableColumnSortDirection;
import com.sap.tc.webdynpro.progmodel.api.IWDNode;
import com.sap.tc.webdynpro.progmodel.api.IWDNodeElement;
/**
The class allows to sort context node elements by the specified attributes in the specified order.
The sorter supports sorting by single attribute or by several attributes. In the last case two node
elements are compared by the first attribute, then, if the elements are equal, the elements are
compared by the second attribute and so on.
*
@author Siarhei_Pisarenka
*/
public class MultiAttributesNodeComparator implements Comparator {
public static interface IAttributeComparator {
public int compareAttributes(Object o1, Object o2, String attrName);
}
/**
The class compares node attribute values interpreting them as a text.
*/
public static class TextAttributeComparator implements IAttributeComparator {
public int compareAttributes(Object o1, Object o2, String attrName) {
if (o1 == null || o2 == null) {
return (o1 == o2) ? 0 : ((o1 == null) ? 1 : -1);
}
String attrValue1 = ((IWDNodeElement) o1).getAttributeAsText(attrName);
String attrValue2 = ((IWDNodeElement) o2).getAttributeAsText(attrName);
if (attrValue1 == null || attrValue2 == null) {
return (attrValue1 == attrValue2) ? 0 : ((attrValue1 == null) ? 1 : -1);
} else {
return attrValue1.compareToIgnoreCase(attrValue2);
}
}
}
private WDTableColumnSortDirection direction;
private String[] attrNames;
private IAttributeComparator attributeComparator;
public MultiAttributesNodeComparator() {
this(new TextAttributeComparator());
}
public MultiAttributesNodeComparator(IAttributeComparator attributeComparator) {
this.attributeComparator = attributeComparator;
}
/**
@see java.util.Comparator#compare(java.lang.Object, java.lang.Object)
*/
public int compare(Object o1, Object o2) {
int result = 0;
for (int iAttr = 0; result == 0 && iAttr < attrNames.length; ++iAttr) {
result = attributeComparator.compareAttributes(o1, o2, attrNames[iAttr]);
}
return (direction == WDTableColumnSortDirection.UP) ? result : -result;
}
public void sortElements(String attrName, WDTableColumnSortDirection direction, IWDNode node) {
sortElements(new String[] { attrName }, direction, node);
}
public void sortElements(
String[] attrNames,
WDTableColumnSortDirection direction,
IWDNode node) {
this.attrNames = attrNames;
this.direction = direction;
node.sortElements(this);
}
}
Usage 1
final MultiAttributesNodeComparator nodeComparator = new MultiAttributesNodeComparator();
...
nodeComparator.sortElements(
new String[] {"attr1", "attr2"},
WDTableColumnSortDirection.UP,
node);
Usage 2
final MultiAttributesNodeComparator nodeComparator = new MultiAttributesNodeComparator();
...
nodeComparator.sortElements("attrName", WDTableColumnSortDirection.UP, node);
when I was using the above usage 1 I was getting an error stating MultiAttributesNodeComparator cannot be resolved.
The steps I followed was in the following path : src->packages->com->sap->app-> createad a simple file and named it Table_Sort.java and copied the above code in it .
Then in my onAction Sort wrote the usage 1 code providing the column names and the node name as the argument.
but before even building it it say it cant resolve
final MultiAttributesNodeComparator nodeComparator = new MultiAttributesNodeComparator();
the above line.
I also added imports but still it didnt resolve my issue.
Kindly provide some inputs...
Thank you
Regards,
Preet
Hi Amol,
The table sorting I was able to complete and work out fine. But the drop down sorting there is an issue.
I created a sample application and in which I have a drop down by index, which contain the following values "B", "A", "N", "C", "F","D".
After populating the node I executed the sort element and as a result it displays "D", "F", "C", "N", "A","B". Kindly let me know if I am doin anything wrong.
Thank you
Regards,
Preet
User | Count |
---|---|
81 | |
9 | |
9 | |
7 | |
7 | |
6 | |
6 | |
6 | |
5 | |
4 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.