gpt4 book ai didi

GWT - 如何对 celltable 列进行排序?

转载 作者:行者123 更新时间:2023-12-04 05:43:39 25 4
gpt4 key购买 nike

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

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

}

最佳答案

我想你指的是 Developer's Guide - Cell 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);

关于GWT - 如何对 celltable 列进行排序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7026882/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com