如何从单元格表中删除行

时间:2010-12-03 13:17:55

标签: java gwt

起初我使用了网格。 在创建GWT的新版本之后,我想替换CellTable上的Grid。

3 个答案:

答案 0 :(得分:10)

查看javadoc了解详情。我的例子就像你在那里找到的那​​个(只是稍微扩展一下):

public void onModuleLoad() {
    final CellTable<Row> table = new CellTable<Row>();

    TextColumn<Row> firstColumn = new TextColumn<Starter.Row>() {

        @Override
        public String getValue(Row object) {
            return object.firstColumn;
        }
    };
    table.addColumn(firstColumn, "header one");

    TextColumn<Row> secondColumn = new TextColumn<Starter.Row>() {

        @Override
        public String getValue(Row object) {
            return object.secondColumn;
        }
    };
    table.addColumn(secondColumn, "header two");

    TextColumn<Row> thirdColumn = new TextColumn<Starter.Row>() {

        @Override
        public String getValue(Row object) {
            return object.thirdColumn;
        }
    };
    table.addColumn(thirdColumn, "header three");

    table.setRowCount(getList().size());
    final ListDataProvider<Row> dataProvider = new ListDataProvider<Starter.Row>(getList());
    dataProvider.addDataDisplay(table);

    final SingleSelectionModel<Row> selectionModel = new SingleSelectionModel<Starter.Row>();
    table.setSelectionModel(selectionModel);

    Button btn = new Button("delete entry");
    btn.addClickHandler(new ClickHandler() {

        @Override
        public void onClick(ClickEvent event) {
            Row selected = selectionModel.getSelectedObject();
            if (selected != null) {
                dataProvider.getList().remove(selected);
            }
        }
    });

    RootPanel.get().add(table);
    RootPanel.get().add(btn);

}

private class Row {

    private String firstColumn;
    private String secondColumn;
    private String thirdColumn;

    public Row(String firstColumn, String secondColumn, String thirdColumn) {
        this.firstColumn = firstColumn;
        this.secondColumn = secondColumn;
        this.thirdColumn = thirdColumn;
    }

}

private LinkedList<Row> getList() {
    LinkedList<Row> list = new LinkedList<Row>();
    list.add(new Row("first", "entry", "foo"));
    list.add(new Row("second", "entry", "foo"));
    list.add(new Row("third", "entry", "foo"));
    list.add(new Row("fourth", "entry", "foo"));
    return list;
}

答案 1 :(得分:2)

或者您可以像这样运行

@UiHandler("combo") 
public void onChange(ChangeEvent e) {
    textBoxes.clear();
    searchFields.clear();
    while(resultsTable.getColumnCount()!=0) {
        resultsTable.removeColumn(0);
    }
    resultsTable.redraw();

其中resultsTable是一个CellTable

答案 2 :(得分:1)

CellTable是新DataPresentationWidgets的一部分,仅用于显示数据。所以你应该从CellTable用来显示的数据列表中删除成员。