0

I just want to update the CellTable columns.

See for eg.

         Header1 |  Header2 | Header3 |
        -------------------------------
        Column11|  Column12| Column13| 
        Column21|  Column22| Column23| 
        Column31|  Column32| Column33| 

   final TextColumn<Student> nameColumn = new TextColumn<Student>() {

        @Override
        public String getValue(Student object) {
            return object.getName();
        }
    };
    cellTable.addColumn(nameColumn, "Name");

    final TextColumn<MyDTO> classColumn = new TextColumn<MyDTO>() {

        @Override
        public String getValue(Student object) {
            return object.getClassName();
        }
    };
    cellTable.addColumn(classColumn, "Age");

    final TextColumn<Student> subjectColumn = new TextColumn<Student>() {

        @Override
        public String getValue(Student object) {
            return object.getMark();
        }
    };

    cellTable.addColumn(subjectColumn, "Subject");

When performing some operation, I just want to update Column22, column23 etc... like that Column32, Column33....

Can anyone help me?

Thanks in advance, Gnik

Gnik
  • 7,120
  • 20
  • 79
  • 129
  • It is not clear what exactly you want to achieve? What do you mean by update? Is it editing of the data in the table? Also, can you post the code that you tried to achieve functionality? – Ganesh Kumar May 17 '12 at 16:32
  • Ganesh I added my code. Yes I want to edit the text of those columns – Gnik May 17 '12 at 17:33

1 Answers1

1

You can use a data provider like ListDataProvider for the CellTable. The data provider holds the data for the table and is associated with the table. So when there are any changes to the data in the data provider, the changes will be reflected in the table when you call refresh() method. Here is the complete code(I have modified a little the code available here)

public class GWTCellTable implements EntryPoint {
// A simple data type that represents a contact.
private static class Contact {
    private String address;
    private String name;

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

// The list of data to display.
private static List<Contact> CONTACTS = Arrays.asList(new Contact("John", "123 Fourth Road"), new Contact("Mary", "222 Lancer Lane"), new Contact("Zander",
        "94 Road Street"));

public void onModuleLoad() {

    // Create a CellTable.
    CellTable<Contact> table = new CellTable<Contact>();

    // Create name column.
    TextColumn<Contact> nameColumn = new TextColumn<Contact>() {
        @Override
        public String getValue(Contact contact) {
            return contact.name;
        }
    };

    // Create address column.
    TextColumn<Contact> addressColumn = new TextColumn<Contact>() {
        @Override
        public String getValue(Contact contact) {
            return contact.address;
        }
    };

    // Add the columns.
    table.addColumn(nameColumn, "Name");
    table.addColumn(addressColumn, "Address");

    // Create a data provider.
    final ListDataProvider<Contact> dataProvider = new ListDataProvider<Contact>();

    // Connect the table to the data provider.
    dataProvider.addDataDisplay(table);

    // Add the data to the data provider, which automatically pushes it to
    // the
    // widget.
    List<Contact> list = dataProvider.getList();
    for (Contact contact : CONTACTS) {
        list.add(contact);
    }

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

    Button button = new Button("Update table");
    RootPanel.get().add(button);

  //Clicking on the update button, updates the first row of the table
    button.addClickHandler(new ClickHandler() {
        @Override
        public void onClick(ClickEvent event) {
            dataProvider.getList().get(0).name = "Ganesh";
            dataProvider.getList().get(0).address = "Gachibowli";
            dataProvider.refresh();
        }
    });
}
}
Ganesh Kumar
  • 3,220
  • 1
  • 19
  • 27
  • Ganesh really i'm sorry. Its my mistake. I'm not going to edit the column manually. I want to update which means overwrite the column by another value. Sorry for that. Can you help me for this? – Gnik May 18 '12 at 04:36
  • I'm getting the error "Unexpected type. Required variable, found value" in the line dataProvider.getList().get(0).name = "Ganesh"; Please help me. – Gnik May 21 '12 at 14:41
  • You should not get this exception if your grid is typed with 'Contact'. – Ganesh Kumar May 21 '12 at 16:20
  • Yes I coded correctly. But see you just access the variable "name" and "address" from the Contact class. But for me I have to access the getter method. Since my server returns JSON. If you add getter() and setter() in Contact class. For ex. dataProvider.getList().get(0).getName() = "Ganesh"; dataProvider.getList().get(0).getAddress() = "Gachibowli"; You too will get same exception. This is my case. Please help me. – Gnik May 22 '12 at 11:49
  • 1
    Then call the setter method for name rather than accessing it directly. – Ganesh Kumar May 22 '12 at 12:28