2

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.

Xorty
  • 18,367
  • 27
  • 104
  • 155
sap
  • 1,141
  • 6
  • 41
  • 62

3 Answers3

3

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();
Chris
  • 1,119
  • 1
  • 8
  • 26
1

Try this after removing element from contacts:

cellTable.setVisibleRangeAndClearData(cellTable.getVisibleRange(), 
true); 
Xorty
  • 18,367
  • 27
  • 104
  • 155
  • I am sorry to hear that. Anyway, it should be automatic after updating the model, shouldn't it? :/ Btw, do you wish to delete this answer if it was not useful to you? Thanks – Xorty Aug 09 '11 at 15:00
  • That's what I thought. But the since gwt gets the data once the application starts the first time, if a table is altered the you have to make a call to the database to get the data out again to show the edited table. I was hoping that the cell table does that automatically when a row is deleted. – sap Aug 09 '11 at 15:55
  • Well I guess it's fine to make async callback and reload the whole table, but reasonable pagination must be considered then (what if the table gets too big ...) – Xorty Aug 09 '11 at 15:56
  • For me on GWT 2.5.1 this fixed the refresh of a cellList after deleting entries from the selectionmodel list. – membersound Jun 21 '13 at 13:03
0

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")

));

Prince
  • 20,353
  • 6
  • 39
  • 59