Lookup screen with filter query

I have set optionsDatasource property in superior field using my custom collectiondatasource, so i can filter the lookup items. But when i add lookupAction to superior field, it seems like the lookup action is still using the main datasource “departmentDs”.
How to set optionsDatasource property in the lookupAction ?


 <dsContext>
        <datasource id="departmentDs"
                    class="id.irfani.democuba.entity.Department"
                    view="department-view"/>
        <collectionDatasource id="superiorsDs"
                              class="id.irfani.democuba.entity.Department"
                              view="_minimal">
            <query>
                <![CDATA[select e from democuba$Department e
where e.departmentLevelNo > :ds$departmentDs.departmentLevelNo]]>
            </query>
        </collectionDatasource>
    </dsContext>
    <layout expand="windowActions"
            spacing="true">
        <fieldGroup id="fieldGroup"
                    datasource="departmentDs">
            <column width="250px">
                <field id="departmentNo"/>
                <field id="departmentName"/>
                <field id="departmentLevel"/>
                <field id="departmentLevelNo"
                       visible="true"/>
                <field id="superior" editable="false"
                       optionsDatasource="superiorsDs"/>
            </column>
        </fieldGroup>
        <frame id="windowActions"
               screen="extendedEditWindowActions"/>
    </layout>

@Named("fieldGroup.superior")
    private PickerField superiorField;

    @Named("fieldGroup.departmentLevel")
    private PickerField departmentLevelField;

    @Override
    public void init(Map<String, Object> params) {
        super.init(params);
        superiorField.addLookupAction();
        departmentLevelField.addValueChangeListener(e -> {
            DepartmentLevel deptLevel = (DepartmentLevel)e.getValue();
            if(deptLevel != null) {
                getItem().setDepartmentLevelNo(deptLevel.getLevelNo());
                superiorField.setEditable(true);
            }
            else {
                getItem().setDepartmentLevelNo(null);
                superiorField.setEditable(false);
            }
        });

    }
1 Like

You have to pass a parameter to the opening lookup screen and modify the query according to it.

Please take a look at the screen-manipulation example project available in Studio samples. In this method, a lookup screen is opened passing a customer instance to it. In the opening screen controller, the query is modified.

You can provide parameters to your lookup action:


PickerField.LookupAction action = (PickerField.LookupAction) superiorField.getAction(PickerField.LookupAction.NAME);
action.setLookupScreenParams(ParamsMap.of("departmentLevelNo", someValue));

Thanks Krivopustov.
It works!

  1. Send parameter from department editor to department browser

public void init(Map<String, Object> params) {
        super.init(params);
        superiorField.addLookupAction();
        departmentLevelField.addValueChangeListener(e -> {
            DepartmentLevel deptLevel = (DepartmentLevel)e.getValue();
            if(deptLevel != null) {
                getItem().setDepartmentLevelNo(deptLevel.getLevelNo());
                superiorField.setEditable(true);

                PickerField.LookupAction action = (PickerField.LookupAction) superiorField.getAction(PickerField.LookupAction.NAME);
                action.setLookupScreenParams(ParamsMap.of("departmentLevel", getItem().getDepartmentLevel()));
            }
            else {
                getItem().setDepartmentLevelNo(null);
                superiorField.setEditable(false);
            }
        });

    }
  1. Department Browse : Get parameter and modify departmentDs query

@WindowParam
    private DepartmentLevel departmentLevel;

    @Inject
    private HierarchicalDatasource departmentsTreeDs;

    @Override
    public void init(Map<String, Object> params) {
        super.init(params);
        if (departmentLevel != null) {
            departmentsTreeDs.setQuery(
                    "select e from democuba$Department e where e.departmentLevelNo > :param$departmentLevel.levelNo");
        }
    }

But i think its better to design cuba platform automatically detect optionsDatasource property, and use it instead of create dynamic action handler.

> i think its better to design cuba platform automatically detect optionsDatasource property, and use it instead of create dynamic action handler.

That’s an interesting idea, we will take it into consideration.

1 Like