Gnik
Gnik

Reputation: 7426

How can I update the GWT CellTable columns dynamically

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

Upvotes: 0

Views: 7239

Answers (1)

Ganesh Kumar
Ganesh Kumar

Reputation: 3240

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

Upvotes: 1

Related Questions