CustomField with a Grid, presentation value comes from SelectedValue of grid

I have a CustomField composed by some fields which allow filtering on a grid, and the idea is that the value of the field is the “selected value” of the grid.

My code is as follows:

    @Override
    protected MyDto generateModelValue() {
        return grid.getSelectedItems().stream().findFirst().orElse(null);
    }
    protected void setPresentationValue(MyDto myDto) {
        if (myDto == null) {
            filter1.setValue(null);
            filter2.setValue(null);
            grid.getDataProvider().refreshAll();
            grid.getSelectionModel().deselectAll();
        } else {
            filter1.setValue(myDto.getField1());
            filter2.setValue(myDto.getField2());
            grid.getDataProvider().refreshAll();
            grid.getSelectionModel().select(myDto);
        }

This field is used in a binder.

however, when one of the filters is selected (filter1, filter2), generateModelValue is called; but not when the grid value is actually selected.

I see getElement().addEventListener("change", e -> this.updateValue()); in the CustomField constructor

… which triggers the generateModelValue when I select something in one of the filter fields

but it’s not called when I select something in the grid

I wonder if I have to do updateValue() manually when an item of the grid is selected

Is there a specific reason why you are programming this yourself instead of using the build-in multiselection grid that is accessible via the selection mode? https://github.com/vaadin/flow-components/blob/afdd1af13bbf322d41096dc4c7a8a1b551799846/vaadin-grid-flow-parent/vaadin-grid-flow/src/main/java/com/vaadin/flow/component/grid/GridMultiSelectionModel.java#L79

You need to call manually updateValue() in the grid.addSelectionListener ( I don’t know if it’s the correct name of the method)

( the change event is working well when you have fields in it, the grid doesn’t fire the change event)

@quirky-zebra well, the “grid” is part of a CustomField, where I want to encapsulate.

if I do the updateValue() in the selection listener, it works well

I guess the asSingleSelect also triggers the change event when the selection is done?

I don’t think the grid is firing a change event at all

thanks a lot. it was very helpful.