简体   繁体   English

GWT单元格表-删除按钮不会从表中删除行

[英]GWT celltable - delete button does not delete row from table

I added a buttoncell column to a celltable using the following code: 我使用以下代码将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); 
}

} }

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. 当从数据库中成功删除联系人时,我添加了代码:t​​able.redraw(),但是它不起作用。

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();

Try this after removing element from contacts: 从联系人中删除元素后,请尝试以下操作:

cellTable.setVisibleRangeAndClearData(cellTable.getVisibleRange(), 
true); 

Since you have not used ListDataProvider , try using LinkedList , modify your list as-- 由于您尚未使用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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM