开发者

GWT Celltable update button

I have the following cell table. I've added a button to every row. when it is clicked the new data should be updated. But when I click update, the old information about the Contact object is being passed to the updateContact method. How Do I get the edited text entered by the user

following is my code:

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 Lnsddddddddddddd开发者_运维问答dddddddddddddddddddddddddddddddddddddddddddddddddd"));

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

Found the solution, all I had to do was the following to each column:

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

                });


When you update the value it is updated in the variable but not shown in the table. To show the updated data you have to use:

cellTable.redraw();

The redraw function renders the table with the updated data.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜