Dependent Dropdowns

Dears,

In my Edit screen, I have 2 dropdowns: One for the list of projects, and one of the list of services within each project.
When the user selects a project, the services dropdown should display the list of services pertaining to the selected project.

I have done this using the PostInit() method:


protected void postInit() {
    super.postInit();

    activationAdjustmentsDs.addItemPropertyChangeListener(e -> {
        ActivationAdjustments curActivationAdjustment = (ActivationAdjustments) e.getItem();
        // Filter the services drop down based on the selected project
        If (e.getProperty().equals("projectId")) {
            if (curActivationAdjustment.getProjectId() != null) {
                projectServiceIdField.setEditable(true);
                int curProjectId = curActivationAdjustment.getProjectId().getId();
                projectServicesDs.setQuery("select e from billing$ProjectServices e where e.projectId.id = "
                        + curProjectId
                        + " and e.cancelled = false and e.recurringPeriod > 0 and e.terminationDate is null order by e.id");
                projectServicesDs.refresh();
            }
        }
    });

Assume I have 2 projects P1 (with services a and b) and P2 (with services c and d).
The user picks P2, the services dropdown shows c and d. The user selects service c.
Then he changes his mind and selects P1. The service dropdown still shows c, and when the user clicks on the arrow of the dropdown, it shows c, a and b. It is not until the user chooses a or b that the option c is removed.

Appreciate your feedback on how to fix this issue.

Regards,

Shady

1 Like

Hi,

i created an general example on how dependent lookup fields in an editor work here: GitHub - mariodavid/cuba-example-dependent-lookup

I hope this helps to get a better understanding on how this generally works. You can look at the README.md to see a description of the project.

Bye
Mario

Hi guys,

In order to fix the problem with remaining value in dependent field you should add the following to Mario’s example:


public class CustomerEdit extends AbstractEditor<Customer> {

    @Inject
    private Datasource<Customer> customerDs;

    @Override
    protected void postInit() {
        customerDs.addItemPropertyChangeListener(e -> {
            if ("project".equals(e.getProperty())) {
                customerDs.getItem().setProjectService(null);
            }
        });
    }
}

Thanks guys for the help. The issue is fixed.

Hi guys,
I needed the same. And I did everything you wrote, but when I select the right value in the drop-down, then that disapears, like I would never select that…
As I saw only a CollectionDatasource and the right screen descriptor needed.
Any solution?
Okay so I commented out the Edit screen controller’s code and works :slight_smile: