Python中文网

一个关于 编程问题的解答网站.

有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

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

我在GWT中有一个datagrid,我使用RPC来填充数据,我可以让数据很好地显示出来,我也可以选择单个单元格,但在排序时它就是不起作用!我可以偶尔点击列标题(这是间歇性的,我不知道为什么),但当我什么都不做的时候。我使用的是数据提供程序,但我认为我的实现不正确,我附加了相关代码,有人能给我一个如何正确实现的指针吗

首先是实际的表格本身

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类,用于保存datagrid中的每个对象

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

}


共 (1) 个答案

  1. # 1 楼答案

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