Reputation: 99
I want to sort celltable column, have written so far this, but it doesn't seem to be working, i did it all like in gwt showcase with celltable column sorting:
ListHandler<M> sort;
public View() {
getM(0,m);
createM();
}
void createM() {
Column<M, String> firstColumn = new Column<M, String>(
new TextCell()) {
@Override
public String getValue(M object) {
return object.getName();
}
};
table.addColumn(firstColumn,"Name");
firstColumn.setSortable(true);
sort.setComparator(firstColumn, new Comparator<M>() {
@Override
public int compare(M m1, M m2) {
return m1.getName().compareTo(m2.getName());
}
});
void getM(int dataID, M m) {
final ListDataProvider<M> listProvider = new ListDataProvider<M>();
listProvider.addDataDisplay(table);
listProvider.refresh();
final List<M> mList = listProvider.getList();
sort = new ListHandler<M>(mList);
AsyncCallback<List<M>> callback = new AsyncCallback<List<M>>() {
@Override
public void onFailure(Throwable caught) {
// TODO Auto-generated method stub
}
@Override
public void onSuccess(final List<M> result) {
for (final M m : result) {
mList.add(m);
}
table.addColumnSortHandler(sort);
}
};
rpcService.getDataSource(dataID, m, callback);
any suggestion, what is wrong here? How can i solve this?
i moved my code from getM() into createM() like:
void createM() {
final ListDataProvider<M> listProvider = new ListDataProvider<M>();
listProvider.addDataDisplay(table);
listProvider.refresh();
final List<M> mList = listProvider.getList();
sort = new ListHandler<M>(mList);
Column<M, String> firstColumn = new Column<M, String>(
new TextCell()) {
@Override
public String getValue(M object) {
return object.getName();
}
};
table.addColumn(firstColumn,"Name");
firstColumn.setSortable(true);
sort.setComparator(firstColumn, new Comparator<M>() {
@Override
public int compare(M m1, M m2) {
return m1.getName().compareTo(m2.getName());
}
});
and it sort the column, but if i choose the next data to show in column it will be added to the existing items in column, can anyone point me why it is so?
Upvotes: 0
Views: 39
Reputation: 41089
The first example is not working because you re-initialize your sort
object:
sort = new ListHandler<M>(mList);
This removes all settings from sort
.
In the second example, you need to call
mList.clear();
before you add new items to this list. Also, move
table.addColumnSortHandler(sort);
from the callback to your create
method. There is no need to call it every time you add data.
Upvotes: 1