GWT Celltable更新按钮

时间:2011-07-01 18:16:43

标签: gwt

我有以下单元格表。我在每一行都添加了一个按钮。单击它时,应更新新数据。 但是,当我单击更新时,有关Contact对象的旧信息将传递给updateContact方法。如何获取用户输入的编辑文本

以下是我的代码:

public class CellTableExample implements EntryPoint {

  /**
   * A simple data type that represents a contact.
   */
  private static class Contact {
    private final String address;
    private final Date birthday;
    private final String name;

    public Contact(String name, Date birthday, String address) {
      this.name = name;
      this.birthday = birthday;
      this.address = address;
    }
  }

  /**
   * The list of data to display.
   */
  @SuppressWarnings("deprecation")
  private static final List<Contact> CONTACTS = Arrays.asList(
      new Contact("John", new Date(80, 4, 12), "123 Abc Avenue"), 
      new Contact("Joe", new Date(85, 2, 22), "22 Lance Ln fasfasdfasfdasdfasfasdfasfasdfasfasfasdfasdfasdf"), 
      new Contact("Tom", new Date(85, 3, 22), "33 Lance Lnasdfasfdasdfffffffffffffffffff"), 
      new Contact("Jack", new Date(85, 4, 22), "44 Lance Lnsddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd"));

  public void onModuleLoad() {
    // Create a CellTable.
    final CellTable<Contact> table = new CellTable<Contact>();
    // Display 3 rows in one page
    table.setPageSize(3);

    // Add a text column to show the name.
    TextColumn<Contact> nameColumn = new TextColumn<Contact>() {
      @Override
      public String getValue(Contact object) {
        return object.name;
      }
    };
    table.addColumn(nameColumn, "Name");

    // Add a date column to show the birthday.
    DateCell dateCell = new DateCell();
    Column<Contact, Date> dateColumn = new Column<Contact, Date>(dateCell) {
      @Override
      public Date getValue(Contact object) {
        return object.birthday;
      }
    };
    table.addColumn(dateColumn, "Birthday");

    // Add a text column to show the address.
    TextColumn<Contact> addressColumn = new TextColumn<Contact>() {
      @Override
      public String getValue(Contact object) {
        return object.address;
      }
    };
    table.addColumn(addressColumn, "Address");


    ButtonCell updateButton= new ButtonCell();
    Column <Contact,String> update= new Column <Contact,String>(updateButton)
    {
        @Override
        public String getValue(Contact c) 
        {
            return "Update";
        }
    };
    update.setFieldUpdater(new FieldUpdater<Contact,String>()
    {
        @Override
        public void update(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)
            {
            }
        };
        service.updateContact(c.id, c.name, callback);
    }
});


    provider.addDataDisplay(table);
    provider.updateRowCount(CONTACTS.size(), true);

    SimplePager pager = new SimplePager();
    pager.setDisplay(table);

    VerticalPanel vp = new VerticalPanel();
    vp.add(table);
    vp.add(pager);

    // Add it to the root panel.
    RootPanel.get().add(vp);
  }
}

找到解决方案,我所要做的就是对每一列进行以下操作:

colName.setFieldUpdater(new FieldUpdater<Contact,String>()
                {
                    public void update(int index, Contact c, String value) 
                    {
                        comp.name=value;
                    }

                });

1 个答案:

答案 0 :(得分:3)

更新值时,它会在变量中更新,但不会在表格中显示。要显示您必须使用的更新数据:

cellTable.redraw();

redraw函数使用更新的数据呈现表格。