on 12-15-2007 11:13 AM
Hi experts
I have a table with 7 columns(Which is the result from R/3 backend)
I want to sort each column.
Plz provide the solution
Thanks
Hazrath.G
Hi Hazrath,
Take a look at the thread/ blog below for table sorting and table filtering.
Enhanced Web Dynpro Java TableSorter for SAP NetWeaver 04s
<u>/people/bertram.ganz/blog/2006/03/07/enhanced-web-dynpro-java-tablesorter-for-sap-netweaver-04s
How to get ready-made filter for your Web Dynpro Table with minimal coding ?
<u>/people/kapil.kamble/blog/2006/12/21/how-to-get-ready-made-filter-for-your-web-dynpro-table-with-minimal-coding
Filtering Table Values using WebDynpro
<u>/people/subramanian.venkateswaran2/blog/2005/05/10/filtering-table-values-using-webdynpro
Enhancing Tables in WebDynpro Java Custom built Table Utilities
<u>/people/sap.user72/blog/2006/05/04/enhancing-tables-in-webdynpro-java-150-custom-built-table-utilities
Ashish
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi Gurram,
Yes you can add the <b>TableSorter.java</b> class under <b>com.nfcl.model,com.nfcl.appl</b> packages.
First keep the file(TableSorter.java) in the desktop or any specific folder.
1> Go to the Webdynpro Navigrator Perspective.Right click on the folder(com.nfcl.model,com.nfcl.appl).
2> Select Import
3> A wizard will appear select <b>File System</b> Click on next.
4> A wizard will come browse the directory under which you have kept file name <b>TableSorter.java</b> Click on Ok.
5> Check the file name <b>TableSorter.java</b>.
6> Click on finish
Instead of doing this steps you can also directly drag and drop the file to the folder com.nfcl.model,com.nfcl.appl
After importing the file TableSorter.java change the package name and save.
package com.nfcl.model,com.nfcl.appl;
Regards,
Mithu
Hi Hazrath,
In your case to get this value do as follows.
1) go to your webdynpro project in webdynpro perspective
2) Locate the folder src
3) under src locate folder package
4) there sould be subfolders under packages, drill down till you find your class Tablesorter.java
5) now for the type of your value attribute you need to select the folder structure below package folder and note its hierarchy and prepare your type value.
in your case it might be
com.nfcl.model.Tablesorter or com.nfcl.appl.Tablesorter
( assuming that you have put your Tablesorter class under folder
com.nfcl.model or com.nfcl.appl.)
Let me know if you still face some problem.
Ashish Shah
Hi Hazrath,
Please follow these simple steps:
1. Click on Navigator Tab.
2. Open <Project name> -> src-> packages-> com -> nfcl
3. Right click on nfcl -> New - > Other -> Wizard Opens
4. In the wizard select Simple-> Folder click Next -> Give <Folder name as utils > click Finish.
5. Select, Right click utils-> New - > Other -> Wizard Opens
6. In the wizard select Java -> Class click Next -> Give
<Name as TableSorter> click Finish.
7. Remove public class TableSorter {
} . Add TableSorter.java code except first line package com.sap.tc.webdynpro.tests.utils
Your package will be com.nfcl.utils.
regards,
Siva
Hi Hazrath,
Can you send your project to me?
my email id is ash.coolshah@yahoo.com
to send your peoject through email.
1) right click on your webdynpro project slect properties
2) go to info
3) copy the path against location
4) go to start run , paste the path there.
5) zip the project and send it to me.
Ashish Shah.
Hi krushna
I copied the following code in my wddo modify metod.but i got error at(wdthis.wdGetSortCustomersAction())
What action can i pass to constructor?
And i have read from Blog
It shown that create a action sort then to what can i bind this?
code
====
view.nowCreateAllCustomExtensionFields();
IWDTable table = (IWDTable) view.getElement("Table_Funds");
wdContext.currentContextElement().setTableSorter(
new TableSorter(table, wdThis.wdGetSortCustomersAction(), null, new String[] { "CustomerTable_Name" }));
Hi Hazrath,
Create Sort<name>Action() ; and bind this to onSort property of the table. instead of SortCustomersAction() use Sort<name>Action(); wherever possible.
Check the TableSorter class if there are any errors like missing ; or } etc.
If the TableSorter class is error free then only you will get sort() . Save it and organize imports
regards,
Siva
Edited by: Siva Rama Krushna on Dec 18, 2007 2:47 PM
Hi krushna
I copied the following code in my wddo modify metod.but i got error at(wdthis.wdGetSortCustomersAction())
What action can i pass to constructor?
And i have read from Blog
It shown that create a action sort then to what can i bind this?
code
====
view.nowCreateAllCustomExtensionFields();
IWDTable table = (IWDTable) view.getElement("Table_Funds");
wdContext.currentContextElement().setTableSorter(
new TableSorter(table, wdThis.wdGetSortAction(), null, new String[] { "Table_Funds" }));
under action sort()
-
wdContext.currentContextElement().getTablesorter().sort(wdEvent,wdContext.nodeIt_Final_out());
It will display error:The method sort(0 is undefined for TableSorter class
Plz provide me sol.
Thanks
Hazrath
Hi Gurram,
I m sending u the complete class .create a java class in src->packages->ur package & name it "TableSorter "& copy this code in that file.change the package name to ur package bcos i m giving it as com.sap.test.
package com.sap.test;
import java.sql.Date;
import java.text.Collator;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.StringTokenizer;
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.IWDAbstractTableColumn;
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;
import com.sap.tc.webdynpro.services.sal.localization.api.WDResourceHandler;
/**
Helper class that makes a Web Dynpro table UI element sortable (column-wise).
*/
public final class TableSorter {
/**
@param table
@param sortAction
@param comparators
*/
/**
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>onSort</code>
event of the table 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 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
*/
public TableSorter(IWDTable table, IWDAction sortAction, Map comparators) {
init(table, sortAction, comparators, null);
}
public TableSorter(IWDTable table, IWDAction sortAction, Map comparators, String[] sortableColumns) {
init(table, sortAction, comparators, sortableColumns);
}
/**
Initialisation stuff
*/
private void init(IWDTable table, IWDAction sortAction, Map comparators, String[] sortableColumns){
this.table = table;
if(sortableColumns == null){
sortableCols = null;
}else{
sortableCols = new HashMap();
for (int i = 0; i < sortableColumns.length; i++) {
sortableCols.put(sortableColumns<i>, sortableColumns<i>);
}
}
// 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");
// make the columns sortable
String dataSourcePrefix = table.bindingOfDataSource() + ".";
//TODO: remove the following line since this method is not longer available in later releases
setComparatorsForColumns(dataSourcePrefix, table.iterateColumns(), comparators);
setComparatorsForColumns(dataSourcePrefix, table.iterateGroupedColumns(), comparators);
//set up the table properties
table.setOnSort(sortAction);
table.mappingOfOnSort().addSourceMapping(IWDTable.IWDOnSort.COL, "selectedColumn");
table.mappingOfOnSort().addSourceMapping(IWDTable.IWDOnSort.DIRECTION, "sortDirection");
}
/**
Try to make the given columns sortable (recusivly, if necessary)
*/
private void setComparatorsForColumns(String dataSourcePrefix, Iterator columnIterator, Map comparators){
int index = 0;
for (Iterator it = columnIterator; it.hasNext(); ++index) { // for every column: try to make it bindable
IWDAbstractTableColumn abstractColumn = (IWDAbstractTableColumn) it.next();
if(abstractColumn instanceof IWDTableColumn){
IWDTableColumn column = (IWDTableColumn)abstractColumn;
if(sortableCols == null || sortableCols.containsKey(column.getId())){
//try to make this column sortable
Comparator comparator = null;
if (comparators != null){
comparator = (Comparator)comparators.get(column.getId());
}
NodeElementByAttributeComparator elementComparator = null;
if (comparator instanceof NodeElementByAttributeComparator) {
// the easy one, attribute and ordering are given
elementComparator = (NodeElementByAttributeComparator)comparator;
} else { // attribute must be determined
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;
}
String attributeName = bindingOfPrimaryProperty.substring(dataSourcePrefix.length());
Collection subnodes = new ArrayList();
if (attributeName.indexOf('.') >= 0){
//attribute not immediately below data source
String[] tokens = tokenize (attributeName, ".");
for(int i=0; i<tokens.length-1; i++){
subnodes.add(tokens<i>);
}
attributeName = tokens[tokens.length-1];
}
if(subnodes.size() == 0){
elementComparator = new NodeElementByAttributeComparator(attributeName, comparator);
}else{
elementComparator = new NodeElementByAttributeComparator(attributeName, comparator, subnodes);
}
}
// set up internal data structures
comparatorForColumn.put(column, elementComparator);
//set sort state
column.setSortState(WDTableColumnSortDirection.NONE);
}else{
//column should not be sortable
column.setSortState(WDTableColumnSortDirection.NOT_SORTABLE);
}
}else if (abstractColumn instanceof IWDTableColumnGroup){
//it's just a column group -> try to bind the columns of the column group
IWDTableColumnGroup columnGroup = (IWDTableColumnGroup)abstractColumn;
setComparatorsForColumns(dataSourcePrefix, columnGroup.iterateColumns(), comparators);
}
}
}
/**
Tokenizes the input string according to the given delimiters. The delimiters will be left out.
Example: tokenize("Hello_World", "_") results ["Hello", "World"]
*/
private String[] tokenize (String input, String delim){
StringTokenizer tokenizer = new StringTokenizer(input, delim);
String[] tokens = new String[tokenizer.countTokens()];
int index = 0;
while(tokenizer.hasMoreTokens()){
tokens[index] = tokenizer.nextToken();
index++;
}
return tokens;
}
/**
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) {
// find the things we need
String columnId = wdEvent.getString("selectedColumn");
String direction = wdEvent.getString("sortDirection");
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);
}
/**
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
if (o1 instanceof String && o2 instanceof String){
//Use a Collator for sorting according to the given Locale
Collator collate = Collator.getInstance(WDResourceHandler.getCurrentSessionLocale());
return collate.compare(o1, o2);
}
return ((Comparable) o1).compareTo((Comparable) o2);
}
};
/**
Map of table column to comparator (<code>ReversableComparator</code>)
used for sorting that column (sortable columns only).
*/
private Map comparatorForColumn = new HashMap();
/**
The table to be sorted.
*/
private IWDTable table = null;
/**
Column-IDs of the columns, which should be sortable
*/
private Map sortableCols = null;
/**
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, new ArrayList());
}
/**
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, new ArrayList());
}
/**
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, new ArrayList());
}
/**
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>). In addition it is possible
to define the path to a child node with the <code>java.util.Collection</code> subnodes.
(List of child node names in the correct order)
*/
public NodeElementByAttributeComparator(String attributeName, Collection subnodes) {
this(attributeName, null, false, subnodes);
}
/**
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. In addition it is possible
to define the path to a child node with the <code>java.util.Collection</code> subnodes.
(List of child node names in the correct order)
*/
public NodeElementByAttributeComparator(String attributeName, Comparator comparator, Collection subnodes) {
this(attributeName, comparator, false, subnodes);
}
/**
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. In addition it is possible
to define the path to a child node with the <code>java.util.Collection</code> subnodes.
(List of child node names in the correct order)
*/
public NodeElementByAttributeComparator(String attributeName, boolean compareAsText, Collection subnodes) {
this(attributeName, null, compareAsText, subnodes);
}
/**
Internal constructor.
*/
private NodeElementByAttributeComparator(
String attributeName,
Comparator comparator,
boolean compareAsText,
Collection subNodes) {
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;
this.sortDirection = true;
this.subNodes = subNodes;
}
/**
Sets the sort direction of this comparator to the given direction. The comparator sort in ascending order by default.
@see com.sap.tc.webdynpro.clientserver.uielib.standard.api.WDTableColumnSortDirection
*/
public void setSortDirection(WDTableColumnSortDirection direction){
if(direction.equals(WDTableColumnSortDirection.UP)){
sortDirection = true;
}else if(direction.equals(WDTableColumnSortDirection.DOWN)){
sortDirection = false;
}
}
/**
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;
if(subNodes.size() > 0){
element1 = getSubNodeElement(element1, 0);
element2 = getSubNodeElement(element2, 0);
}
Object attributeValue1 = null;
Object attributeValue2 = null;
if(element1 != null){
attributeValue1 =
compareAsText
? element1.getAttributeAsText(attributeName)
: element1.getAttributeValue(attributeName);
}
if(element2 != null){
attributeValue2 =
compareAsText
? element2.getAttributeAsText(attributeName)
: element2.getAttributeValue(attributeName);
}
if(sortDirection){
return comparator.compare(attributeValue1, attributeValue2);
}else{
return comparator.compare(attributeValue2, attributeValue1);
}
}
/**
Determines recursivly the child node, which have an attribute with the given name.
The path to this child node must be specified in the subnodes property of this comparator.
Start this method with index = 0.
*/
private IWDNodeElement getSubNodeElement(IWDNodeElement currentElement, int index){
if(currentElement == null || index >= subNodes.size()){
//end of recursion
return currentElement;
}else{
return getSubNodeElement(currentElement.node().getChildNode((String)subNodes.toArray()[index], currentElement.index()).getCurrentElement(), index+1);
//return getSubNodeElement(currentElement.node().getChildNode((String)subNodes.toArray()[index], currentElement.index()).getElementAt(0), index+1);
}
}
/**
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;
/**
Sort direction (true = ascending order, false = descending order)
*/
private boolean sortDirection;
/**
List of child node names
(Description of the path from the given context node to the specified attribute)
*/
private Collection subNodes;
}
}
Then go to context, create an attribute as tableSorter . Select the attribute & change the type in the property tab.It opens a wizard. Select java nativa type in that. click on browse .it will open a wizard. in that u have to check for TableSorter type.It will take from ur package.Then u can use the same code available in blog.
In wdDoModifyView method of ur view, take this code
if(firstTime)
{
IWDTable table = (IWDTable)view.getElement("Table");
wdContext.currentTableSorterElement().setLibSorter(new TableSorter(table,wdThis.wdGetLibSortAction(),null,new String[] {"PersonCode","ConcatinatedName","Role","PrimaryAddress"}));
}
Change the column id specified after new String[] as ur column id's in ur table. Select the table in Layout tab for ur view. Select the table & create the onSort event for the table. For that onSort method, u can give this implementation
wdContext.currentTableSorterElement().getLibSorter().sort(wdEvent,wdContext.nodePersonnel());
here instead of "getLibSorter" , give ur context element .Change the node name .instead of "nodePersonnel" give ur node what u r using for ur table as dataSource.
Hope its clear now.
regards
Sumit
User | Count |
---|---|
85 | |
10 | |
10 | |
9 | |
6 | |
6 | |
6 | |
5 | |
4 | |
3 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.