on ‎2004 Sep 29 3:07 PM
Has anybody successfully tried sorting columns of a tableview component by clicking on its header? I need to sort a column containing dates in mm/dd/yyyy format. Please share your hints or code. BR, Maulin
Request clarification before answering.
Hello,
I have created a new TableModel that extends the DefaultTableViewModel. I has this functionality as a standard....
Let me know if you want the code so i can send it to you..
BR
Max
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi,
Here is the code, no support included :-):
import java.util.Collections;
import java.util.Comparator;
import java.util.Vector;
import com.sapportals.htmlb.table.DefaultTableViewModel;
public class SortTableModel extends DefaultTableViewModel implements Comparator {
protected int currCol;
protected Vector ascendCol; // this vector stores the state (ascending or descending) of each column
protected Integer one = new Integer(1);
protected Integer minusOne = new Integer(-1);
public SortTableModel() {
super();
ascendCol = new Vector();
}
public SortTableModel(Vector vec) {
super(vec);
ascendCol = new Vector();
}
public SortTableModel(Vector vec, Vector vec2, int numberOfColumns) {
super(vec, vec2);
ascendCol = new Vector();
setSortOrder(numberOfColumns);
}
/*****************************************************************
This method is the implementation of the Comparator interface.
It is used for sorting the rows
*****************************************************************/
public int compare(Object v1, Object v2) {
// the comparison is between 2 vectors, each representing a row
// the comparison is done between 2 objects from the different rows that are in the column that is being sorted
int ascending = ((Integer)ascendCol.get(currCol)).intValue();
if (v1 == null && v2 == null) {
return 0;
} else if (v2 == null) { // Define null less than everything.
return 1 * ascending;
} else if (v1 == null) {
return -1 * ascending;
}
Object o1 = ((Vector)v1).get(currCol);
Object o2 = ((Vector)v2).get(currCol);
// If both values are null, return 0.
if (o1 == null && o2 == null) {
return 0;
} else if (o2 == null) { // Define null less than everything.
return 1 * ascending;
} else if (o1 == null) {
return -1 * ascending;
}
if (o1 instanceof Number && o2 instanceof Number) {
Number n1 = (Number)o1;
double d1 = n1.doubleValue();
Number n2 = (Number)o2;
double d2 = n2.doubleValue();
if (d1 == d2) {
return 0;
} else if (d1 > d2) {
return 1 * ascending;
} else {
return -1 * ascending;
}
} else if (o1 instanceof Boolean && o2 instanceof Boolean) {
Boolean bool1 = (Boolean)o1;
boolean b1 = bool1.booleanValue();
Boolean bool2 = (Boolean)o2;
boolean b2 = bool2.booleanValue();
if (b1 == b2) {
return 0;
} else if (b1) {
return 1 * ascending;
} else {
return -1 * ascending;
}
} else {
// default case
if (o1 instanceof Comparable && o2 instanceof Comparable) {
Comparable c1 = (Comparable)o1;
Comparable c2 = (Comparable)o2; // superflous cast, no need for it!
try {
return c1.compareTo(c2) * ascending;
} catch (ClassCastException cce) {
// forget it... we'll deal with them like 2 normal objects below.
}
}
String s1 = o1.toString();
String s2 = o2.toString();
return s1.compareTo(s2) * ascending;
}
}
/***************************************************************************
This method sorts the rows using Java's Collections class.
After sorting, it changes the state of the column -
if the column was ascending, its new state is descending, and vice versa.
***************************************************************************/
public void sort() {
Collections.sort(dataVector, this);
Integer val = (Integer)ascendCol.get(currCol);
ascendCol.remove(currCol);
if (val.equals(one)) // change the state of the column
ascendCol.add(currCol, minusOne);
else
ascendCol.add(currCol, one);
}
public void sortByColumn(int column) {
this.currCol = column;
sort();
}
public void setSortOrder(int numberOfColumns) {
for (int i = 0; i < numberOfColumns; i++) {
ascendCol.add(one);
}
}
}
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
I am also having same requirement.Please let me know how can i cal this code in jsp.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi Maulin,
You had some good answers, but did you forget to give points? It's a way to say "thanks".
See: <u><></u> for directions.
Click on the Yellow Star icon in each reply.
You can give:
1 - 10 pointer (solves problem, marks as answered)
2 - 6 pointers (very helpful)
Lots of 2 pointers (helpful)
This time I did it for you, but you can change them.
Cheers,
Joan (and Mark Finnern)
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Max
Could you please send DefaultTableViewModel code to me on theov@iolmail.co.za ?
Theo,
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi,
I'm using java classes (not jsp's). In the funtion where you create the TableView component, do the following after creating the table:
for( int i=1; i<this.columnsCount; i++ ) {
com.sapportals.htmlb.table.TableColumn column = tableView.getColumn(i);
// sorry for the strange notation
if( 'column is sotable' && !('data is empty') ) {
column.setOnHeaderSort("onTableHeaderSortEventId");
column.setSortState( getColumnSortState() );
}
}
private TableSortState getSortState() {
// this method should return for the sorted column
// either TableSortState.ASCENDING or
// TableSortState.DESCENDING
// for all others: TableSortState.NONE
}
If someone now clicks on the header to sort the column you get the following declared event:
//...
if( "onTableHeaderSortEventId".equals(pageCtx.getCurrentEvent().getAction()) ) {
if( pageCtx.getCurrentEvent() instanceof TableHeaderSortEvent ) {
TableHeaderSortEvent event = (TableHeaderSortEvent) pageCtx.getCurrentEvent();
int column = event.getColumn();
// now sort the data according to the column
}
}
Hope this helps.
Greetings,
Stefan
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi maulin,
I assume your using the htmlb tag <htmlb:tableView>. In that case set the attribute 'sort' to 'SERVER'. In each column tag <htmlb:tableViewColumn> that you want to being sortable set attribute 'sort' to 'true'. That should do the trick.
regards,
Ulli
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi Francisco,
seems like the implementation of the htmlb tag library is not consistent across technologies. You are right, it's not working in Java htmlb. We use BSP (it's JSP equivalent but ABAP scripting) and connect the EP to the WEB AS, where the BSP runs. The BSP htmlb has tons more of advanced features for tableView (like sort and filter).
regards,
Ulli
| User | Count |
|---|---|
| 9 | |
| 5 | |
| 4 | |
| 4 | |
| 3 | |
| 3 | |
| 2 | |
| 2 | |
| 2 | |
| 2 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.