cancel
Showing results for 
Search instead for 
Did you mean: 

Functionality to dynamically sort tableview columns

Former Member
0 Kudos

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

Accepted Solutions (1)

Accepted Solutions (1)

Former Member
0 Kudos

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

Former Member
0 Kudos

Hi Max,

I am very interested in your code, it would be really great if you could post it here, or send it via E-Mail.

Kind regards

Francisco

Former Member
0 Kudos

Hello All,

First of all thanks a lot for sharing interest & your experience here.

Max, Could you please send DefaultTableViewModel code to me on maulindoshi@hotmail.com ?

BR,

Maulin

Message was edited by: Maulin Doshi

Former Member
0 Kudos

Hi Fransisco,

Send your e-mail so that i can send the code to you.

BR

Max

Former Member
0 Kudos

Hi Max,

i have the same request , could u please senf me yr code?(my mail juranj1@fel.cvut.cz)

but i thing that a lot of users is interested in yr code so i thing that it will be better to post here.

thanks

JJ

Former Member
0 Kudos

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

}

}

}

Answers (6)

Answers (6)

Former Member
0 Kudos

Hi Guys,

I need your help,

I am trying to sort the tableview, my thread id is,

Regards,

Devasarathy Pandi

Former Member
0 Kudos

I am also having same requirement.Please let me know how can i cal this code in jsp.

Former Member
0 Kudos

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)

Former Member
0 Kudos

Max

Could you please send DefaultTableViewModel code to me on theov@iolmail.co.za ?

Theo,

Former Member
0 Kudos

My e-mail address is: f.villar@gmx.de

Thanks in advance,

Francisco

Former Member
0 Kudos

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

Ulli_Hoffmann
Contributor
0 Kudos

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

Former Member
0 Kudos

Hi Ulli,

is this for EP5 only? I get an error under EP6 when using the sort attribute.

Caused by: com.sapportals.portal.prt.servlets_jsp.server.jsp.ParseException:  Attribute 'sort' not found in tag library 'HTMLB' for the tag tableView.

Kind regards

Francisco

Ulli_Hoffmann
Contributor
0 Kudos

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