1

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.

user1576840
  • 113
  • 1
  • 2
  • 7

1 Answers1

3

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).

Andrei Volgin
  • 40,755
  • 6
  • 49
  • 58
  • Thank you for your reply. Please see how I modified the code according to your suggestions: http://pastebin.com/Z4bQYrzf Unfortunately, it still doesn't work. The ***`Window.alert`*** triggers only ones, somehow ***`dataProviderList.refresh()`*** following it, kills it and it doesn't trigger even when the checkbox is checked again. I also decided to use ***FieldUpdater*** instead of ***onBrowserEvent***, which has no effect, but the code is shorter. – user1576840 Nov 06 '14 at 04:52
  • 1
    You still call `cellTable.redraw()` - this time in the FieldUpdater in the other column. Your poor table gets redrawn all the time! You should almost never call redraw() anywhere in your code. – Andrei Volgin Nov 06 '14 at 05:00