GWT celltable - 删除按钮不会从表中删除行

时间:2011-08-08 20:27:22

标签: java gwt gwt-2.2-celltable

我使用以下代码将一个buttoncell列添加到了一个celltable:

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

    //delete button column
    ButtonCell deleteButton= new ButtonCell();
    Column <Contact,String> delete= new Column <Contact,String>(deleteButton)
    {
        @Override
        public String getValue(Contact c) 
        {
            return "Delete";
        }
    };
    delete.setFieldUpdater(new FieldUpdater<Contact,String>()
    {
        @Override
        public void update(final int index, Contact c,String value) 
        {
            SQLRunnerAsync service = (SQLRunnerAsync) GWT.create(SQLRunner.class);
            AsyncCallback callback = new AsyncCallback()
            {
                @Override
                public void onFailure(Throwable caught) 
                {}
                @Override
                public void onSuccess(Object result)
                {
                    CONTACTS.remove(index);
                    table.redraw();
                }
                };
                service.deleteContact(c.id,callback);
            }
    });
    // Add the columns.
    table.addColumn(nameColumn, "Name");
    table.addColumn(addressColumn, "Address");
    table.addColumn(delete, "Delete");

    table.setRowCount(CONTACTS.size(), true);
    table.setRowData(0, CONTACTS);

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

}

按下按钮时,将从数据库中删除联系人。但是,在刷新页面之前,表格保持不变。有什么我可以这样做,当按下按钮时,行也将立即从表中删除?

我从数据库中成功删除联系人时添加了代码:t​​able.redraw(),但它无法正常工作。

3 个答案:

答案 0 :(得分:3)

尝试使用

ListDataProvider<Contact> dataProvider = new ListDataProvider<Contact> (CONTACTS);
dataProvider.addDataDisplay(table);

以及删除时

List<Contact> list = dataProvider.getList();
int indexOf = list.indexOf(object);
list.remove(indexOf);
dataProvider.refresh();

答案 1 :(得分:1)

从联系人中删除元素后尝试此操作:

cellTable.setVisibleRangeAndClearData(cellTable.getVisibleRange(), 
true); 

答案 2 :(得分:0)

由于您尚未使用 ListDataProvider ,请尝试使用 LinkedList ,将列表修改为 -

 private static List<Contact> CONTACTS = new LinkedList<Contact>(Arrays.asList(
new Contact("John", "123 Fourth Road asdf asdf asdfasdf"),
new Contact("Mary", "222 Lancer Lane")

));