我是GWT的新手,我需要创建一个可排序的动态单元格表。
我看到了IndexedColumn类的示例。我生成了一个动态单元格表(String的ArrayList的列表),但无法对其进行排序。
有人有一个简单的实现示例吗?
谢谢
答案 0 :(得分:0)
发现我的错误...
这是我的工作示例:
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.user.cellview.client.CellTable;
import com.google.gwt.user.cellview.client.ColumnSortEvent.ListHandler;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.view.client.ListDataProvider;
/**
* Entry point classes define <code>onModuleLoad()</code>.
*/
public class DynamicTable implements EntryPoint {
/**
* This is the entry point method.
*/
public void onModuleLoad() {
List<ArrayList<String>> data = this.getThedata();
CellTable<List<String>> table = new CellTable<List<String>>();
ArrayList<String> dataHeaders = data.remove(0);
ArrayList<IdxColumn> columnList = new ArrayList<IdxColumn>();
ListDataProvider<List<String>> dataProvider = new ListDataProvider<List<String>>();
dataProvider.addDataDisplay(table);
List<List<String>> list = dataProvider.getList();
for (List<String> l : data) {
list.add(l);
}
ListHandler<List<String>> columnSortHandler = new ListHandler<List<String>>(list);
table.getColumnSortList().clear();
for (int i = 0; i < data.get(0).size(); i++) {
final int index = i;
IdxColumn iCol = new IdxColumn(index);
columnList.add(iCol);
table.addColumn(iCol, dataHeaders.get(index) + " (" +iCol.getIndex() + ")");
columnSortHandler.setComparator(iCol, new Comparator<List<String>>() {
public int compare(List<String> o1, List<String> o2) {
if (o1 == o2) {
return 0;
}
if (o1 != null) {
return (o2 != null) ? o1.get(0).compareTo(o2.get(0)) : 1;
}
return -1;
}
});
iCol.setSortable(true);
table.getColumnSortList().push(iCol);
}
table.addColumnSortHandler(columnSortHandler);
table.setRowCount(data.size(), true);
table.setRowData(0, data);
RootPanel.get().add(table);
}
// build the data
private List<ArrayList<String>> getThedata() {
List<ArrayList<String>> data = new ArrayList<ArrayList<String>>();
ArrayList<String> dataHeaders = new ArrayList<String>();
dataHeaders.add("Header 1");
dataHeaders.add("Header 2");
dataHeaders.add("Header 3");
dataHeaders.add("Header 4");
dataHeaders.add("Header 5");
dataHeaders.add("Header 6");
data.add(dataHeaders);
for (int i = 0; i < 20; i++) {
ArrayList<String> dataRow = new ArrayList<String>();
dataRow.add("Col1 Row" + i);
dataRow.add("Col2 Row" + i);
dataRow.add("Col3 Row" + i);
dataRow.add("Col4 Row" + i);
dataRow.add("Col5 Row" + i);
dataRow.add("Col6 Row" + i);
data.add(dataRow);
}
return data;
}
}
和IdxColumn类:
import java.util.List;
import com.google.gwt.cell.client.TextCell;
import com.google.gwt.user.cellview.client.Column;
public class IdxColumn extends Column<List<String>, String> {
private final int index;
public IdxColumn( int index) {
super(new TextCell());
this.index = index;
}
@Override
public String getValue(List<String> object) {
return object.get(index);
}
public int getIndex() {
return this.index;
}
}