Hello Friends,
I have a HTMLB table with 6 columns, all the columns except the date column are being sorted.
Date column is being sorted incorrectly with day as ascending irrespective of year. MMDDYYYY.
e.g 03-04-2007, 04-04-2007, 04-01-2008, 01-01-2008 is being sorted as
01-01-2008,03-04-2007, 04-04-2007, 05-01-2008 instead of
01-01-2008,03-04-2007, 04-04-2007, 04-01-2008
The table data is being populated from R/3.
Should i change the Java class or is this an issue on R/3 side. here is the code which might be of help. Please look into it.
public class MyDynPage extends JSPDynPage {
private final Vector ABCTableColumns= new Vector(Arrays.asList(new String[] {"Date", "Name" ,"Status"}
//
//
public void onHeaderClick(Event event) throws PageException {
TableHeaderClickEvent tne = (TableHeaderClickEvent) event;
this.equipmentTableViewBean.getRepairList().sortByColumn(tne.getColumn());
"Sorting code 'Java' "
import java.util.Collections;
import java.util.Comparator;
import java.util.Enumeration;
import java.util.Vector;
import com.sapportals.htmlb.table.DefaultTableViewModel;
import com.sapportals.htmlb.table.TableColumn;
/*
<br>This class is an extension of the TableViewModel that provides column header click sorting.
*/
public class XTableViewModel 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);
/*
Construct an empty GFCTableViewModel.
*/
public XTableViewModel() {
super();
}
/*
Construct a XTableViewModel with the specified column names.
@param columnNames A Vector of column names.
*/
public XTableViewModel(Vector columnNames) {
this(new Vector(), columnNames);
}
/*
Construct a XTableViewModel with the specified data and column names.
@param vectorData A Vector of data for the table.
@param columnNames A Vector of column names.
*/
public XTableViewModel(Vector data, Vector columnNames) {
this(data, columnNames, true);
}
/*
Construct a XTableViewModel with the specified data and column names and column auto-creation command.
@param vectorData A Vector of data for the table.
@param columnNames A Vector of column names.
@param columnAutoCreated Default is true. A boolean value that indicates whether to automatically create
the TableView columns from the columnNames.
*/
public XTableViewModel(Vector data, Vector columnNames, boolean columnAutoCreated) {
columns = new Vector(columnNames.size());
dataVector = data;
TableColumn column;
for(Enumeration columnNamesEnum = columnNames.elements(); columnNamesEnum.hasMoreElements(); columns.addElement(column))
column = new TableColumn(this, columnNamesEnum.nextElement().toString());
if(columnAutoCreated)
visibleColumns = (Vector)columns.clone();
ascendCol = new Vector();
setSortOrder(columnNames.size());
}
/*
Construct a XTableViewModel with the specified column names.
@param columnNames An array of column names.
*/
public XTableViewModel(Object columnNames[]) {
Object data[][] = new Object[0][];
setDataVector(data, columnNames);
ascendCol = new Vector();
setSortOrder(columnNames.length);
}
/*
Construct a XTableViewModel with the specified data and column names.
@param vectorData An array of data for the table.
@param columnNames An array of column names.
*/
public XTableViewModel(Object data[][], Object columnNames[]) {
setDataVector(data, columnNames);
ascendCol = new Vector();
setSortOrder(columnNames.length);
}
/*
Construct a XTableViewModel with the specified data and column names and column auto-creation command.
@param vectorData An array of data for the table.
@param columnNames An array of column names.
@param columnAutoCreated Default is true. A boolean value that indicates whether to automatically create
the TableView columns from the columnNames.
*/
public XTableViewModel(Object data[][], Object columnNames[], boolean columnAutoCreated) {
setDataVector(data, columnNames, columnAutoCreated);
ascendCol = new Vector();
setSortOrder(columnNames.length);
}
/*
Returns the Vector of data inside this TableViewModel.
@return The Vector of data inside this TableViewModel.
*/
public Vector getDataVector() {
return this.dataVector;
}
/*
Sets the Vector of data inside this TableViewModel.
@param dataVector The Vector of data inside this TableViewModel.
*/
public void setDataVector(Vector newDataVector) {
this.dataVector = newDataVector;
}
/*****************************************************************
This method is the implementation of the Comparator interface.
It is used for sorting the rows
*****************************************************************/
public int compare(Object v1, Object v2) {
int vectorColumnIndex = this.currCol - 1;
// 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(vectorColumnIndex);
Object o2 = ((Vector)v2).get(vectorColumnIndex);
// 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);
}
}
}