How to reload item from a lookup field with options datasource

Hi

We have a screen allowing to edit a commercial offer (very similar to editing a customer order), using a DataGrid for inline editing of lines.

One of the line field is a lookup field to select a Product entity from a list of 8000+ items. For performance reason I use an options datasource with a minimal view (ds var in the code below).

Once the Product item has been selected, I would like to reload it with a more extensive view and make it the field value. Is there a simple way to do that ?

Iā€™m afraid that if I use setValue with the reloaded Product on the field I will either provoke an infinite recursive ValueChangeListener listener loop, or have it being ignored.

linesGrid.getColumnNN(p).setEditorFieldGenerator((datasource, property) -> {
    LookupPickerField field = componentsFactory.createComponent(LookupPickerField.class);
    field.setOptionsDatasource((CollectionDatasource) masterEditor.getDsContext().getNN(ds));
    field.setDatasource(datasource, property);
    PickerField.LookupAction action = field.addLookupAction();
    action.setLookupScreenOpenType(WindowManager.OpenType.THIS_TAB);
    field.setRefreshOptionsOnLookupClose(true);
    return field;
});

EDIT: tried following approach but it does not work.

           linesGrid.getColumnNN(p).setEditorFieldGenerator((datasource, property) -> {
                LookupPickerField field = componentsFactory.createComponent(LookupPickerField.class);
                CollectionDatasource optionsDs = (CollectionDatasource) masterEditor.getDsContext().getNN(ds);
                field.setOptionsDatasource(optionsDs);
                field.setDatasource(datasource, property);
                PickerField.LookupAction action = field.addLookupAction();
                action.setLookupScreenOpenType(WindowManager.OpenType.THIS_TAB);
                field.setRefreshOptionsOnLookupClose(true);
                Reloader reloader = fieldOptionReloader.get(p);
                if(reloader != null) {
                    field.addValueChangeListener(ev->{
                        if(ev.getValue() == null) return;
                        //noinspection unchecked
                        optionsDs.excludeItem((Entity) ev.getValue());
                        Entity reloaded = reloader.reload((Entity) ev.getValue());
                        if(optionsDs instanceof CollectionDatasource.Ordered) {
                            //noinspection unchecked
                            ((CollectionDatasource.Ordered) optionsDs).includeItemFirst(reloaded);
                        } else {
                            //noinspection unchecked
                            optionsDs.includeItem(reloaded);
                        }
                        field.setValue(reloaded);
                    });
                }
                return field;
            });

Best Regards
Michael

Hi Michael,

The following should work:

ordersTable.getColumnNN("customer").setEditorFieldGenerator((datasource, property) -> {
    LookupPickerField<Customer> field = componentsFactory.createComponent(LookupPickerField.class);
    field.setOptionsDatasource(customersDs);
    
    // do not assign datasource to the field
    // field.setDatasource(datasource, property); 
    
    // set current value to field directly
    field.setValue(datasource.getItem().getValue(property));

    PickerField.LookupAction action = field.addLookupAction();
    action.setLookupScreenOpenType(WindowManager.OpenType.THIS_TAB);
    field.setRefreshOptionsOnLookupClose(true);

    field.addValueChangeListener(valueChangeEvent -> {
        // set reloaded instance to the datasource
        datasource.getItem().setValue(property, dataManager.reload(valueChangeEvent.getValue(), View.BASE));
    });
    return field;
});

Regards,
Konstantin

1 Like

It works great thanks @knstvk

Regards,
Michael