Reputation: 1141
I added a buttoncell column to a celltable using the following code:
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);
}
}
when the Button is pressed, the contact is deleted from the database. However, the table remains the same until the page is refreshed. Is there anything that I can do so that when the button is pressed, the row will also be immediately deleted from the table?
I added the code: table.redraw() when the contact is successfully deleted from the database, but it's not working.
Upvotes: 2
Views: 4516
Reputation: 20882
Since you have not used ListDataProvider, try using LinkedList, modify your list as--
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")
));
Upvotes: 0
Reputation: 1171
Try using
ListDataProvider<Contact> dataProvider = new ListDataProvider<Contact> (CONTACTS);
dataProvider.addDataDisplay(table);
and when you delete
List<Contact> list = dataProvider.getList();
int indexOf = list.indexOf(object);
list.remove(indexOf);
dataProvider.refresh();
Upvotes: 3
Reputation: 18861
Try this after removing element from contacts:
cellTable.setVisibleRangeAndClearData(cellTable.getVisibleRange(),
true);
Upvotes: 1