我正在阅读有关如何将列排序功能添加到单元格表但我不理解谷歌提供的代码。以下是我的celltable,我将如何添加nameColumn sort-able?
public class CellTableExample implements EntryPoint {
private static class Contact {
private String address;
private String name;
public Contact(String name, String address) {
super();
this.address = address;
this.name = name;
}
}
// The list of data to display.
private static List<Contact> CONTACTS = Arrays.asList(
new Contact("John", "123 Fourth Road asdf asdf asdfasdf"),
new Contact("Mary", "222 Lancer Lane")
);
@Override
public void onModuleLoad() {
CellTable<Contact> table = new CellTable<Contact>();
//address column
TextColumn<Contact> addressColumn = new TextColumn<Contact>(){
@Override
public String getValue(Contact contact) {
return contact.address;
}
};
//name column
TextColumn<Contact> nameColumn = new TextColumn<Contact>(){
@Override
public String getValue(Contact contact) {
return contact.name;
}
};
// Add the columns.
table.addColumn(nameColumn, "Name");
table.addColumn(addressColumn, "Address");
table.setRowCount(CONTACTS.size(), true);
table.setRowData(0, CONTACTS);
RootPanel.get().add(table);
}
}
答案 0 :(得分:9)
我想你正在考虑Developer's Guide - Cell Table 。如果您运行了第一个示例,则可以使用
将名称colum排序(将小箭头添加到名称中)table.getColumn(0).setSortable(true);
顺便说一下,这是我必须自动运行示例的代码(只需在“RootPanel.get()。add(table);”之后复制它,并将“_01_scheduleDeferred”替换为您的项目名称)
// Create a data provider.
ListDataProvider<Contact> dataProvider = new ListDataProvider<Contact>();
// Connect the table to the data provider.
dataProvider.addDataDisplay(table);
// Add the data to the data provider, which automatically pushes it to the
// widget.
List<Contact> list = dataProvider.getList();
for (Contact contact : CONTACTS) {
list.add(contact);
}
// Add a ColumnSortEvent.ListHandler to connect sorting to the
// java.util.List.
ListHandler<Contact> columnSortHandler = new ListHandler<_01_scheduleDeferred.Contact>(
list);
columnSortHandler.setComparator(nameColumn,
new Comparator<_01_scheduleDeferred.Contact>() {
@Override
public int compare(_01_scheduleDeferred.Contact o1, _01_scheduleDeferred.Contact o2) {
if (o1 == o2) {
return 0;
}
// Compare the name columns.
if (o1 != null) {
return (o2 != null) ? o1.name.compareTo(o2.name) : 1;
}
return -1;
}
});
table.addColumnSortHandler(columnSortHandler);
// We know that the data is sorted alphabetically by default.
table.getColumnSortList().push(nameColumn);
答案 1 :(得分:0)
我编写了一个utilc来封装将一个可排序的TextColumn添加到CellTable中,这将使事情变得更容易。我相信它会在接下来的几天内发展,但这就是我迄今为止所做的工作......
package your.package.here;
import com.google.gwt.user.cellview.client.CellTable;
import com.google.gwt.user.cellview.client.ColumnSortEvent;
import com.google.gwt.user.cellview.client.TextColumn;
import com.google.gwt.view.client.ListDataProvider;
import java.util.Comparator;
/**
* Created by alex on 8/3/16.
*/
public class CellTableUtil
{
public static <D> void addSortableTextColumn(CellTable<D> table, ListDataProvider<D> dataProvider,
String columnTitle, final FieldExtractor<D, String> fieldExtractor)
{
final TextColumn<D> col = new TextColumn<D>()
{
@Override
public String getValue(D client)
{
return String.valueOf(fieldExtractor.getField(client));
}
};
col.setSortable(true);
table.addColumn(col, columnTitle);
ColumnSortEvent.ListHandler<D> columnSortHandler = new ColumnSortEvent.ListHandler<>(
dataProvider.getList());
columnSortHandler.setComparator(col,
new Comparator<D>() {
public int compare(D o1, D o2) {
if (o1 == o2) {
return 0;
}
// Compare the fields of the Client
if (o1 != null) {
return (o2 != null) ?
fieldExtractor.getField(o1).compareTo(fieldExtractor.getField(o2)) : 1;
}
return -1;
}
});
table.addColumnSortHandler(columnSortHandler);
}
/**
* Created by alex on 8/3/16.
*/
public static interface FieldExtractor<D, T>
{
public T getField(D dataEntity);
}
}
在我的代码中,我在表中列出的数据类型称为“Client”,其上有一个方法getFirstName()。现在,您可以调用此方法向表中添加可排序列,如此...
CellTableUtil.addSortableTextColumn(table, dataProvider, "First Name",
new CellTableUtil.FieldExtractor<Client, String>() {
@Override
public String getField(Client c)
{
return c.getFirstName();
}
});