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:

    protected MyDto generateModelValue() {
        return grid.getSelectedItems().stream().findFirst().orElse(null);
    protected void setPresentationValue(MyDto myDto) {
        if (myDto == null) {
        } else {

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?

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.