2

我使用以下代码将按钮单元列添加到单元表中:

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(),但它不起作用。

4

3 回答 3

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();
于 2011-08-25T14:10:50.427 回答
1

从联系人中删除元素后试试这个:

cellTable.setVisibleRangeAndClearData(cellTable.getVisibleRange(), 
true); 
于 2011-08-08T21:10:53.363 回答
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")

));

于 2012-02-28T05:42:01.173 回答