[英]GWT celltable - delete button does not delete row from table
我使用以下代碼將buttoncell列添加到單元格表:
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")
));
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.