如何排序单元格列



我正在阅读如何将列排序函数添加到单元格表,但我不理解谷歌提供的代码。下面是我的单元格表,我如何添加使namecoluml可排序?

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); 
    }
}

我想你指的是开发人员指南-单元格表。如果您运行了第一个示例,您可以使用

使名称列可排序(在名称上添加小箭头)
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);

我已经编写了一个util类来封装添加一个可排序的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();
            }
        });

相关内容

  • 没有找到相关文章