Bdoserror
Bdoserror

Reputation: 1195

GXT 3 ToggleButtonCell not updating data store

I have a GXT 3 app and I'm trying to use a ToggleButtonCell to allow the user to modify a Boolean value.

Here's the code for the data:

public class InspectionListGridData {
private Boolean posted;

public InspectionListGridData(InspectionListGridData dataToCopy) {
    setPosted(dataToCopy.getPosted());
}

public Boolean getPosted() {
    return posted;
}

public void setPosted(Boolean posted) {
    this.posted = posted;
}

}

For the grid to access the data, I provide this property access interface:

interface ListProperties extends PropertyAccess<InspectionListGridData> {
    ValueProvider<InspectionListGridData, Boolean> posted();
}

The Grid & column config are declared like this:

final ListProperties properties = GWT.create(ListProperties.class);
final List<ColumnConfig<InspectionListGridData,?>> columnConfigList = new ArrayList<ColumnConfig<InspectionListGridData,?>>();
final ListStore<InspectionListGridData> store = new ListStore<InspectionListGridData>(
    new ModelKeyProvider<InspectionListGridData>() {
        @Override
        public String getKey(InspectionListGridData item) {
            return item.getInspectionDocumentId().toString();
        }
    }
});

final ColumnConfig<InspectionListGridData, Boolean> postedColumnConfig = new ColumnConfig<InspectionListGridData, Boolean>(properties.posted(), 5, "Posted");

ToggleButtonCell postedButtonCell = new ToggleButtonCell();
postedButtonCell.setText("posted");
postedButtonCell.setIcon(SafedoorPM.localizedResources.postedIcon());
postedButtonCell.setIconAlign(IconAlign.TOP);
postedColumnConfig.setCell(postedButtonCell);
postedColumnConfig.setSortable(false);
columnConfigList.add(postedColumnConfig);

Grid<InspectionListGridData> inspectionListGrid = new Grid<InspectionListGridData>(store, columnModel);

When I load this screen, the buttons do not initialize to the corresponding state indicated by the data. [EDIT: the failed loading of initial values was due to a different bug. Once I fixed that the initial values loaded correctly]

Once the screen is loaded, if I click a button it changes state just fine but the store doesn't get updated. I set breakpoint on the InspectionListGridData.setPosted() method, it is not called when I click on the button.

Can anyone see what I'm doing wrong? Or am I just wrong in thinking this is supposed to just work? I thought that was the point of the ValueProvider interfaces.

Bonus extra weirdness, the grid displays the red triangle in the corner to indicate that the cell is dirty when it is clicked and button does display properly when clicked i.e. it stays down or up. It just doesn't seem to read or update the data store.

Upvotes: 0

Views: 226

Answers (1)

Colin Alworth
Colin Alworth

Reputation: 18331

Two questions here, at first I only keyed off of the first one (which I still can't answer, but more info might help), but the second is very clear.

When I load this screen, the buttons do not initialize to the corresponding state indicated by the data.

This is confusing, and contradicted by a quick sample I put together, as I indicated in my comment It could be that you are changing the data after drawing the grid and not informing the store or grid that the data has changed, but if you are building the data with both true and false values, the grid should be displaying with both true and false values.

I set breakpoint on the InspectionListGridData.setPosted() method, it is not called when I click on the button.

By default, this is expected while store.isAutoCommmit() is true, which is the default value. This tells the store that it should queue up changes rather than applying them directly to the objects in the store. These changed values are marked in the UI with the red triangle you noticed, and other code can check for changed values through the Store.getRecord(M) method or Store.getModifiedRecords() calls. Calling store.commitChanges() will apply all of them to the underlying models, or you can commit to specific models with Record.commit(). You can also reject changes with Store.rejectChanges() or Record.revert().

When this is turned off, the setPosted method should be called by clicking the button. No change tracking can occur, so no dirty flag will be set, either visually or in the store's records.

If you change an object that is already rendered, you have two (main) choices - you can modify the object directly via its setters and inform the store, or you can use the store's record objects. If autocommit is false and you invoke store.getRecord(object).addChange(properties.posted(), true), then instead of creating a new change to be committed, this will call setPosted(true), so these methods are effectively the same when autocommit is false. If you directly call a setter, be sure to inform the store that the object has changed via store.update.

Upvotes: 1

Related Questions