我使用以下代码为单元格表添加了一个按钮单元格列:
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);
}
}
按下该键,联系人将从数据库中删除。但是,在刷新页面之前,表保持不变。我能做些什么,以便按下按钮时,行也将立即从表中删除?
当联系人成功从数据库中删除时,我添加了代码:table.redraw(),但它不起作用
尝试使用
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();
从联系人中删除元素后试试:
cellTable.setVisibleRangeAndClearData(cellTable.getVisibleRange(),
true);
由于您没有使用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")
));