gpt4 book ai didi

java - 使用 GWT 对数据网格进行排序

转载 作者:行者123 更新时间:2023-11-29 08:58:51 25 4
gpt4 key购买 nike

我在 GWT 中有一个数据网格,我正在使用 RPC 用数据填充它,我可以让数据显示得很好,我也可以选择单个单元格,但是当涉及到排序时它就不会了'不工作!我偶尔可以点击列标题(它间歇性地发生,我不确定为什么)但是当我什么都不做时。我正在使用 dataProvider,但我认为我没有正确实现它,我附上了相关代码,有人可以指导我如何正确执行此操作吗?

首先是实际的表本身

public class GuiInventory {

public final static LayoutPanel hpMain = new LayoutPanel();

static ListHandler<OpInventory> sortHandler;
/*
* Define a key provider for a Contact. We use the unique ID as the key,
* which allows to maintain selection even if the name changes.
*/
static ProvidesKey<OpInventory> keyProvider = new ProvidesKey<OpInventory>() {
@Override
public Object getKey(OpInventory item) {
// Always do a null check.
return (item == null) ? null : item.getPartID();
}
};

//the table
final static DataGrid<OpInventory> table = new DataGrid<OpInventory>(keyProvider);

final static SelectionModel<OpInventory> selectionModel = new MultiSelectionModel<OpInventory>(keyProvider);

/**
* The provider that holds the list of contacts in the database.
*/
private final static ListDataProvider<OpInventory> dataProvider = new ListDataProvider<OpInventory>();

public ListDataProvider<OpInventory> getDataProvider() {
return dataProvider;
}

/**
* Add a display to the database. The current range of interest of the display
* will be populated with data.
*
* @param display a {@Link HasData}.
*/
public void addDataDisplay(HasData<OpInventory> display) {
dataProvider.addDataDisplay(display);
}

/**
* Refresh all displays.
*/
public void refreshDisplays() {
dataProvider.refresh();
}

public static Widget init() {

hpMain.clear();
table.setWidth("100%");
table.setSelectionModel(selectionModel);
Ioma.dataservice.getPartInventory(new AsyncCallback<ArrayList<OpInventory>>() {
@Override
public void onFailure(Throwable caught) {
// TODO Auto-generated method stub
}

@Override
public void onSuccess(ArrayList<OpInventory> result) {
dataProvider.setList(result);
// Attach a column sort handler to the ListDataProvider to sort the list.
sortHandler = new ListHandler<OpInventory>(result);
table.addColumnSortHandler(sortHandler);
dataProvider.addDataDisplay(table);

if (table.getColumnCount() == 0) {
initTable();
}
}
});

//add in table
hpMain.add(table);
return hpMain;
}

public static void initTable() {

// Add a text column to show the part ID.
Column<OpInventory, Number> partIDColumn = new Column<OpInventory, Number>(new NumberCell()) {
@Override
public Integer getValue(OpInventory object) {
return object.getPartID();
}
};
table.addColumn(partIDColumn, "Part ID");
table.setColumnWidth(partIDColumn, 4, Unit.PX);

//add a sort to partID
partIDColumn.setSortable(true);
sortHandler.setComparator(partIDColumn, new Comparator<OpInventory>() {
@Override
public int compare(OpInventory o1, OpInventory o2) {
return Integer.valueOf(o1.getPartID()).compareTo(o2.getPartID());
}
});

// Add a text column to show the part Number.
Column<OpInventory, String> partNumberColumn = new Column<OpInventory, String>(new EditTextCell()) {
@Override
public String getValue(OpInventory object) {
return object.getPartNumber();
}
};
table.addColumn(partNumberColumn, "Part Number");
table.setColumnWidth(partNumberColumn, 4, Unit.PX);

//add a sort to the part Number
partNumberColumn.setSortable(true);
sortHandler.setComparator(partNumberColumn, new Comparator<OpInventory>() {
@Override
public int compare(OpInventory o1, OpInventory o2) {
return o1.getPartNumber().compareTo(o2.getPartNumber());
}
});

//add a field updater to be notified when the user enters a new Part Number
partNumberColumn.setFieldUpdater(new FieldUpdater<OpInventory, String>() {
@Override
public void update(int index, OpInventory object, String value) {
object.setPartNumber(value);
//TODO add async call to database to update part Number
table.redraw();
}
});

// Add a text column to show the name.
Column<OpInventory, String> nameColumn = new Column<OpInventory, String>(new EditTextCell()) {
@Override
public String getValue(OpInventory object) {
return object.getName();
}
};
table.addColumn(nameColumn, "Name");
table.setColumnWidth(nameColumn, 10, Unit.PX);

//add a field updater to be notified when the user enters a new part name
nameColumn.setFieldUpdater(new FieldUpdater<OpInventory, String>() {
@Override
public void update(int index, OpInventory object, String value) {
object.setName(value);
//TODO add async call to database to update part name

table.redraw();
}
});

//add a sort to the name
nameColumn.setSortable(true);
sortHandler.setComparator(nameColumn, new Comparator<OpInventory>() {
@Override
public int compare(OpInventory o1, OpInventory o2) {
return o1.getName().compareTo(o2.getName());
}
});

}

这是 Opinventory 类,用于保存数据网格中的每个对象

public class OpInventory implements Comparable<OpInventory>, IsSerializable {

int partID;
String partNumber;
String name;
String desc;
String partLotNumber;
String supplier;
String reOrderNumber;
boolean isActive;
int quantity;
Double price;

/**
* The key provider that provides the unique ID of a contact.
*/
public static final ProvidesKey<OpInventory> KEY_PROVIDER = new ProvidesKey<OpInventory>() {
@Override
public Object getKey(OpInventory item) {
return item == null ? null : item.getPartID();
}
};

@Override
public int compareTo(OpInventory o) {
return (o == null || o.partNumber == null) ? -1 : -o.partNumber.compareTo(partNumber);
}

@Override
public boolean equals(Object o) {
if (o instanceof OpInventory) {
return partID == ((OpInventory) o).partID;
}
return false;
}

@Override
public int hashCode() {
return partID;
}

public OpInventory(int partID, String partNumber, String name, String desc, String partLotNumber, String supplier, String reOrderNumber, Double price, boolean isActive) {
this.partID = partID;
this.partNumber = partNumber;
this.name = name;
this.desc = desc;
this.partLotNumber = partLotNumber;
this.supplier = supplier;
this.reOrderNumber = reOrderNumber;
this.price = price;
this.isActive = isActive;
}

public OpInventory() {

}

//getters and setters here

最佳答案

显然我的问题是我在两个类中都有一个 keyProvider,而不是只有一个。我从 OpInventory 类中删除了它,现在它似乎可以工作了。这看起来是一个非常具体的问题,所以我希望这个问题很快就会结束。但我会将代码留在那里以供将来分析。

关于java - 使用 GWT 对数据网格进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18705994/

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