gwtcelltablegwt-celltable

GWT CellTable: How to Update A TextBox Dynamically


I have the following CellTable CellTable

When the user clicks the Pay Min. CheckBox, it should copy the value from the Due Now column over to the Pay Today text field AND recalculate the total for the Pay Today column.

Here is the code for the CheckboxCell (Pay Min.) and the TextInputCell (Pay Today) columns:

private Column<AccountInvoice, Boolean> buildPayMin() {

    columnPayMin = new Column<AccountInvoice, Boolean>(new CheckboxCell(true, false)) {
        @Override
        public Boolean getValue(AccountInvoice object) {
            return object.isPayMinimum();
        }

        @Override 
        public void onBrowserEvent(Context context, Element elem, AccountInvoice object, NativeEvent event){

            // Get event type
            int eventType = Event.as(event).getTypeInt();

            // See if this is a 'change' event
            if (eventType == Event.ONCHANGE) {

                String value = columnMinDue.getValue(object);

                // Get the cell to copy the value from                  
                TextInputCell cell = (TextInputCell) columnPayToday.getCell();
                // Re-create the view data for the cell
                TextInputCell.ViewData viewData = new TextInputCell.ViewData(value);
                cell.setViewData(object, viewData);
                // Refresh
                cellTable.redraw();

                event.preventDefault();
                event.stopPropagation();
            }
        }
    };
    columnPayMin.setDataStoreName(columnPayMinHeader);
    columnPayMin.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_CENTER);
    columnPayMin.setVerticalAlignment(HasVerticalAlignment.ALIGN_MIDDLE);
    return columnPayMin;
}

// -----------------------------------------------------------

private Column<AccountInvoice, String> buildPayToday() {
    columnPayToday = new Column<AccountInvoice, String>(new TextInputCell()) {
        @Override
        public String getValue(AccountInvoice object) {
            return object.getPaymentAmount();
        }
    };
    columnPayToday.setDataStoreName(columnPayTodayHeader);
    columnPayToday.setFieldUpdater(new FieldUpdater<AccountInvoice, String>() {
        @Override
        public void update(int index, AccountInvoice object, String value) {
            object.setPaymentAmount(value);
            cellTable.redraw();
        }
    });
    return columnPayToday;
}

I can get the value to copy over, but the total for the Pay Today column doesn't refresh. It seems to refresh only when a value is manually entered into the Pay Today text field. I even tried doing:

columnPayToday.getFieldUpdater().update(context.getIndex(), object, value);

which didn't help either.

Thanks for any help you might provide.


Solution

  • ViewData represents a temporary value in your TextInputCell. When you call cellTable.redraw(), the TextInputCell reverts to the original value (the one from getValue() method for that column).

    Do not redraw the table if you want to modify only the "view" state of TextInputCell, or call accountInvoice.setPayToday# (or whatever method is there) to update the object and then call refresh() on your ListDataProvider (which is a more efficient way to update a table compared to redraw).