2

我在 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

}

4

1 回答 1

0

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

于 2013-09-09T21:21:57.177 回答