Hi.
We have two comboboxes A and B. B depends on A’s value.
ComboBox A = new ComboBox();
A.setImmediate(true);
ComboBox B = new ComboBox();
B.setImmediate(true);
A.addValueChangeListener(event -> {
Collection<Stirng> data = getData((String) event.getProperty().getValue());
B.removeAllItems();
B.addItems(data);
B.setValue(B.getItemIds().iterator().next()); // i don't like such selection of the first item
});
initialLoad(A);
So far so good.
We add some functionality that will replace all items in combobox A.
repopulate(final Collection<String> newOptions) {
if (newOptions.isEmpty()) {
A.setEnabled(false);
B.setEnabled(false);
} else {
A.setEnabled(true);
B.setEnabled(true);
A.removeAllItems();
A.addItems(newOptions);
A.setValue(A.getItemIds().iterator().next()); // again (
}
}
So, pretty claer here, we clear items and add the new one. Reset value is needed to update the B combobox, but…!
RemoveAllItems also set the value, NULL value. And what we get in result? Replace items triggers two value change events: one is for NULL, second is for the first value. Which will be first? Who knows! One time it’s NULL another is a real value. On UI it looks like empty combobox - but there is actually no reason for this from the user pov, then it looks good, then again empty and so on.
I have two options for this:
- save listener to the field and remove/add it before removing all items and adding the new one;
- recreate whole combobox (what?) with all realted stuff.
Actually, I don’t like any of them.
Could it be implemented in some other way? Maybe I missed smth (high chance). Thanks in advance.