问题
I'm reading about how to add column sort functions to a cell table but I'm not understanding the code provided by google. Following is my celltable, How would I add make the 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);
}
}
回答1:
I suppose you are reffering to Developer's Guide - Cell Table . If you got the first example running you can make the name colum sortable (add the little arrow to the name) with
table.getColumn(0).setSortable(true);
Btw this is the codes I had to addeto get your example running (just copy it after "RootPanel.get().add(table);" and replace "_01_scheduleDeferred" with your project name)
// 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);
回答2:
I've written a util class to encapsulate adding a sortable TextColumn to a CellTable that will make things easier. I'm sure it will evolve over the next few days, but here's what I have working so far...
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);
}
}
In my code, the datatype I'm listing in the table is called 'Client', which has a method getFirstName() on it. Now you can call this method to add a sortable column to your table like so...
CellTableUtil.addSortableTextColumn(table, dataProvider, "First Name",
new CellTableUtil.FieldExtractor<Client, String>() {
@Override
public String getField(Client c)
{
return c.getFirstName();
}
});
来源:https://stackoverflow.com/questions/7026882/gwt-how-to-sort-celltable-column