How do I display a Wicket Datatable, sorted by a specific column by default?
Posted
by David
on Stack Overflow
See other posts from Stack Overflow
or by David
Published on 2010-04-28T02:00:22Z
Indexed on
2010/04/28
2:03 UTC
Read the original article
Hit count: 361
Hello everyone!
I have a question regarding Wicket's Datatable. I am currently using DataTable to display a few columns of data.
My table is set up as follows:
DataTable<Column> dataTable = new DataTable<Column>("columnsTable", columns, provider, maxRowsPerPage) {
@Override
protected Item<Column> newRowItem(String id, int index, IModel<Column> model) {
return new OddEvenItem<Column>(id, index, model);
}
};
The columns look like so:
columns[0] = new PropertyColumn<Column>(new Model<String>("Description"), "description", "description");
columns[1] = new PropertyColumn<Column>(new Model<String>("Logic"), "columnLogic");
columns[2] = new PropertyColumn<Column>(new Model<String>("Type"), "dataType", "dataType");
Here is my column data provider:
public class ColumnSortableDataProvider extends SortableDataProvider<Column> {
private static final long serialVersionUID = 1L;
private List<Column> list = null;
public ColumnSortableDataProvider(Table table) {
this.list = Arrays.asList(table.getColumns().toArray(new Column[0]));
}
public ColumnSortableDataProvider(List<Column> list) {
this.list = list;
}
@Override
public Iterator<? extends Column> iterator(int first, int count) {
/*
first - first row of data
count - minimum number of elements to retrieve
So this method returns an iterator capable of iterating over {first, first+count} items
*/
Iterator<Column> iterator = null;
try {
if(getSort() != null) {
Collections.sort(list, new Comparator<Column>() {
private static final long serialVersionUID = 1L;
@Override
public int compare(Column c1, Column c2) {
int result=1;
PropertyModel<Comparable> model1= new PropertyModel<Comparable>(c1, getSort().getProperty());
PropertyModel<Comparable> model2= new PropertyModel<Comparable>(c2, getSort().getProperty());
if(model1.getObject() == null && model2.getObject() == null)
result = 0;
else if(model1.getObject() == null)
result = 1;
else if(model2.getObject() == null)
result = -1;
else
result = ((Comparable)model1.getObject()).compareTo(model2.getObject());
result = getSort().isAscending() ? result : -result;
return result;
}
});
}
if (list.size() > (first+count))
iterator = list.subList(first, first+count).iterator();
else
iterator = list.iterator();
}
catch (Exception e) {
e.printStackTrace();
}
return iterator;
}
Sorting by clicking a column works perfectly, but I would like the table to initially be sorted, by default, by the Description column. I am at a loss to do this. If you need to see some other code, please let me know.
Thank you in advance!!! - D
© Stack Overflow or respective owner