cancel
Showing results for 
Search instead for 
Did you mean: 

TableFilter source for NW2004

Former Member
0 Kudos

Hi,

I saw the nice blog of the TableFilter https://www.sdn.sap.com/irj/sdn/weblogs?blog=/pub/wlg/5439%3Fpage%3Dlast"; [original link is broken] [original link is broken] [original link is broken]>https [original link is broken] [original link is broken]

and in the comments i saw it was also possible to use it with NW2004 but i looked on the forum for the code of TableFilter.java but couldnt find it. Can anyone supply me with this or tell me where to find it?

Much thanks,

best regards,

Hugo Hendriks

Accepted Solutions (0)

Answers (2)

Answers (2)

Former Member
0 Kudos

/people/peter.vignet/blog/2007/01/03/generic-web-dynpro-java-table-filter

http://searchsap.techtarget.com/loginMembersOnly/1,289498,sid21_gci1238730,00.html?Error=Passwordprovidedis+incorrect%2E&NextURL=http%3A%2F%2Fsearchsap%2Etechtarget%2Ecom%2Ftip%2F0%2C289483%2Csid21%5Fgci1238730%2C00%2Ehtml&priTopic=299360&Offer=

you can also google it with :

"Web Dynpro Java generic table filter"

Former Member
0 Kudos

Hi,

Thanks for the code. I adjusted the calls from the tutorial to make it compile. I created a source node which contains all the attributes like the tablenode and make sure the data is the same as the tablenode. I also created a filternode which in which i created 2 filter for my first 2 rows. The problem is that the filtering works doesnt work the proper way and the filter icon gets placed on the first row while filtering in the second and vice versa.... Can anyone tell me where i screwed up?

Much thanks,

Hugo

Former Member
0 Kudos

Hugo:

Can you be a little more explicit about the filter does not work for the 1st column.

In the instruction , read carefully about icon and not creating a column for the legend icon and also be sure that your hashtable icon is correctly mapped. As explained you map a columnId to a attribute (and not a columnid to a column id or an attribute to an attribute)

Former Member
0 Kudos

Hi Peter,

Ill be a bit more specific. Im using the tablefilter code for NW2004. I have a table which has 2 fixed column but more columns are generated dynamically. Im implementing the filter for th first 2 columns first. I got the filtering working...which works nice only the filter-icon is placed on the opposite column still....filter on the first column, second column gets the icon in the header and vice versa. I also make use of the tablesorter....can this cause the problem?

Much thanks,

Hugo

Former Member
0 Kudos

Hugo:

1. Table sorter & table filter should be able to work together. I tested them together.

2. For the filtering switching problem. If the first colum is filter according the value of filter of the second column and the second column according the filter of the fisrt column.

a)Are you sure that the soruceNode (node with all data) and the filterNode attributes have the same corresponding name?

b)are you sure that the first column property "filter value" is mapped to the correct attribute of the of the filter node?

c) for the icon column are you sure that the propery "filter value " is mapped to the attribute legend of the filter node?

Former Member
0 Kudos

Peter,

I guess i didnt explain it too well. I got the filtering working for all the correct columns. Also the dynamic column filter generation seems to work. The only problem is the funnel icon which is displayed in the headercolumn to show that a filter is active. ( I dont have a column with icons for the record.) When i apply a filter to a column, the funnel icon isnt displayed on this column but allways on the last column....When i look at the TableFilter source, it looks at the currentlyFilteredColumn to display the icon but i cant figure out how this is set. I dont have to map anything to make the funnel icon appear as I can see from the tutorial.

regards,

Hugo

Former Member
0 Kudos

Hugo:

I did not test the table with dynamic building. May be that is a reason.

I suggest that for the time being , you comment the line that set the icon filter in the column header. it is the filter method were the column is set to isFiltered(true) see below:

if (valuehash.size()==0){

elementComparator.column.setIsFiltered(false);

} else {

/// comment this line: elementComparator.column.setIsFiltered(true);

no funnel will be shown , for any filtered column ,this is a temporary solution.

Regards

Former Member
0 Kudos

Hi hugo,

You can refer to the following links to get more information on Table Filter:

1)<a href="/people/kapil.kamble/blog/2006/12/21/how-to-get-ready-made-filter-for-your-web-dynpro-table-with-minimal-coding:///people/kapil.kamble/blog/2006/12/21/how-to-get-ready-made-filter-for-your-web-dynpro-table-with-minimal-coding

2)<a href="://

And I am also providing the code for "TableFilter.java"

package com.sap.tableproject;

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.IWDCaption;

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

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;

import com.sap.tc.webdynpro.progmodel.api.WDCopyService;

/**

  • Helper class that makes a Web Dynpro table UI element filterable (column-wise).

*/

public final class TableFilter

{

/**

  • Creates a table filter for the given table using the given filter 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.TableFilter</code>.

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

  • event of filter columns and must at least call this table filter's

  • <code>filter(wdEvent)</code> method.

  • When a table is made filterable 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 filtered column.

  • @see filter()

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

*/

public TableFilter(IWDTable table, IWDAction filterAction)

{

this.table = table;

// sanity checks

if (filterAction == null)

throw new IllegalArgumentException("Filter 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();

// 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

table.setOnFilter(filterAction);

}

}

/**

  • This method must be called from the event handler of this table filter's

  • filter action. It performs the actual filter operation.

*/

public void filter(IWDCustomEvent wdEvent,IWDNode dataSource, IWDNode originalSource, IWDNode filterNode)

{

// find the things we need

String refinedFilter = null;

String field = null;

String attributeName = null;

String bindingOfPrimaryProperty = null;

String dataSourcePrefix = table.bindingOfDataSource() + ".";

IWDTableColumn[] columns = table.getColumns();

String path = null;

String attribute = null;

StringTokenizer tokenizer = null;

dataSource.invalidate();

WDCopyService.copyElements(originalSource,dataSource);

int totalElements = originalSource.size();

for(int i = totalElements - 1; i >= 0 ; i--)

{

IWDNodeElement element = dataSource.getElementAt(i);

for(int j =0; j < columns.length; j++ )

{

IWDTableColumn colmn = columns[j];

bindingOfPrimaryProperty = bindingOfPrimaryProperty(colmn.getTableCellEditor());

if(bindingOfPrimaryProperty != null

&& bindingOfPrimaryProperty.startsWith(dataSourcePrefix))

attributeName = bindingOfPrimaryProperty.substring(dataSourcePrefix.length());

if(attributeName.indexOf('.') < 0)

field = element.getAttributeAsText(attributeName);

if(colmn.bindingOfFilterValue() != null)

{

path = colmn.bindingOfFilterValue();

tokenizer = new StringTokenizer(path, ".");

while(tokenizer.hasMoreTokens())

{

attribute = tokenizer.nextToken();

}

refinedFilter = refineFilterString(filterNode.getCurrentElement().getAttributeAsText(attribute));

if(!(field.matches(refinedFilter)))

{

dataSource.removeElement(element);

}

else

{

// remove icon of previously filtered column

if ( currentlyFilteredColumn != null

&& currentlyFilteredColumn.getHeader() != null )

currentlyFilteredColumn.getHeader().setImageSource(null);

currentlyFilteredColumn = columns[j];

if ((currentlyFilteredColumn.getHeader() != null) && (dataSource.size() != originalSource.size()))

currentlyFilteredColumn.getHeader().setImageSource("~sapicons/s_b_filt.GIF");

}

}

}

}

}

/**

  • 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();

if (element instanceof IWDImage)

return ((IWDImage)element).bindingOfAlt();

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;

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

/**

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

}

public String refineFilterString (String filterString)

{

if ( ( null == filterString ) || ( 0 >= filterString.length() ) )

{

filterString = ".*"; //show All

}

if ( ! filterString.equalsIgnoreCase(".*") )

{

if(filterString.indexOf('*')>=0)

{

filterString = filterString.replaceAll("
","
.
");

}

else

{

filterString = "." + filterString + ".";

}

if(!filterString.endsWith("") && !filterString.startsWith(".") && (filterString.indexOf('*')> 0))

{

filterString = "." + filterString + ".";

}

}

return filterString;

}

}

Regards,

Jhansi