Search code examples
gwtcellcelltable

How to get the value of a CellTable cell


I am working on a GWT app that uses CellTable to display 2 columns for denomination and quantity. The denomination values are not editable while the quantity values are editable.

What I envisage is, if a user selects for instance, 5 for denomination and 20 for quantity, a TextBox for total should populate automatically with 5 * 20 = 100.

My question is is, how can I retrieve the value for a cell so that I can do the multiplication.

PS: The values are all stored in a database table.


Solution

  • You can always attach a selectionModel to the celltable to get currently selected row and then get the current selected object and in turn their values. I am not entirely sure about the next statement but you might want to use the FieldUpdater too. Refer to the GWT docs here

    Sample :

    Selection Model:

    SingleSelectionModel<Contact> selectionModel = new SingleSelectionModel<Contact>();
        table.setSelectionModel(selectionModel);
        selectionModel.addSelectionChangeHandler(new SelectionChangeEvent.Handler() {
          public void onSelectionChange(SelectionChangeEvent event) {
            Contact selected = selectionModel.getSelectedObject();
            if (selected != null) {
              Window.alert("You selected: " + selected.name);
            }
          }
        });
    

    FieldUpdater :

    // Add a field updater to be notified when the user enters a new name.
        nameColumn.setFieldUpdater(new FieldUpdater<Contact, String>() {
          @Override
          public void update(int index, Contact object, String value) {
            // Inform the user of the change.
            Window.alert("You changed the name of " + object.name + " to " + value);
    
            // Push the changes into the Contact. At this point, you could send an
            // asynchronous request to the server to update the database.
            object.name = value;
    
            // Redraw the table with the new data.
            table.redraw();
          }
        });