我对Wicket的数据表有疑问。我目前正在使用DataTable来显示几列数据。
我的表格设置如下:
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);
}
};
列看起来像这样:
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");
这是我的列数据提供者:
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;
}
通过单击列进行排序可以很好地完成,但我希望默认情况下通过Description列对表进行排序。我不知道这样做。如果您需要查看其他代码,请告诉我们。
提前谢谢!!! - D
答案 0 :(得分:5)
setSort
方法中有一些重载的setSortState
方法和SortableDataProvider
方法可以调用来设置此方法。
有关使用setSort
的示例代码,请参阅此examples page上排序DataView示例中的SortableContactDataProvider
来源。