问题
I am trying to sort my CellTable and I am following the advice mentioned in the following example and the SO answer which are here.
http://gwt.googleusercontent.com/samples/Showcase/Showcase.html#!CwCellTable CellTable: dynamically reordering rows
but none of the solution is working. Here is my code please select me how can I enable the sorting on the table.
private void setComparator(ColumnSortEvent.ListHandler<MyModel> myHandler) {
table.getColumn(0).setSortable(true);
myHandler.setComparator(table.getColumn(0), new Comparator<MyModel>() {
@Override public int compare(MyModel o1, MyModel o2) {
return o1.getOrderId().compareTo(o2.getOrderId());
}
});
table.getColumn(1).setSortable(true);
myHandler.setComparator(table.getColumn(1), new Comparator<MyModel>() {
@Override public int compare(MyModel o1, MyModel o2) {
return o1.getOrderDate().compareTo(o2.getOrderDate());
}
});
table.getColumn(2).setSortable(true);
myHandler.setComparator(table.getColumn(2), new Comparator<MyModel>() {
@Override public int compare(MyModel o1, MyModel o2) {
return o1.getLastUpdated().compareTo(o2.getLastUpdated());
}
});
table.getColumn(3).setSortable(true);
myHandler.setComparator(table.getColumn(3), new Comparator<MyModel>() {
@Override public int compare(MyModel o1, MyModel o2) {
return o1.getQuantity().compareTo(o2.getQuantity());
}
});
table.getColumn(4).setSortable(true);
myHandler.setComparator(table.getColumn(4), new Comparator<MyModel>() {
@Override public int compare(MyModel o1, MyModel o2) {
return o1.getStatus().compareTo(o2.getStatus());
}
});
}
public void setTableData(MyDate tableData) {
dataProvider.setData(tableData);
}
public void addTableToPage() {
if (initialize == false) {
dataProvider.addDataDisplay(table);
initialize = true;
}
addTableElements();
}
private void addTableElements() {
addSortHandler();
theView.addTable(table);
theView.setPagerVisible(true);
}
public void addSortHandler() {
ColumnSortEvent.ListHandler<SearchNewNumberOrderModel> myHandler =
new ColumnSortEvent.ListHandler<SearchNewNumberOrderModel>(
dataProvider.getOrders());
setComparator(myHandler);
table.addColumnSortHandler(myHandler);
}
Code for setting the data:
public void setData(MyModelFilter dataForTable) {
List<MyModel1> modelList = dataForTable.getList();
setOrderList(modelList);
updateRowCount();
}
private void setOrderList(List<MyModel11> list) {
if (!UtilityService.isEmpty(list)) {
for (myModel1 element : list) {
MyModel model = new MyModel();
model.setfield1(element.getfieldA());
model.setfield2(element.getfieldB());
.... and so on
orders.add(model);
}
}
}
来源:https://stackoverflow.com/questions/21340004/unable-to-sort-the-celltable-column