将ComboBox值设置为null后如何清除字段?

时间:2019-05-29 11:59:48

标签: java spring vaadin

我有一个名为projectRequirementComboBox的ComboBox,它依赖于projectComboBox,从那里我可以看到要显示在projectRequirementComboBox下拉列表中的列表,但是我想做类似的事情:当用户更改项目时,我想将projectRequirementComboBox清空为更清楚地说,不会选择任何项目。我现在正在执行此操作,但是我的projectRequirementComboBox仍然具有旧值,我不知道缺少什么。我正在使用vaadin.version 8.0.7。

private void refreshProjectRequirementCombobox()
{
    List<ProjectRequirement> projectRequirements = new ArrayList<>();
    if (projectComboBox.getValue() != null)
    {
        projectRequirements = projectRequirementService.findCurrentProjectRequirements(projectComboBox.getValue().getProjectId());
    }
    projectRequirementComboBox.setItems(projectRequirements);
    projectRequirementComboBox.setValue(null);

}

private void loadProjectRequirement(Project project)
{
    List<ProjectRequirement> projectRequirements = new ArrayList<>();
    if (project != null)
    {
        projectRequirements = projectRequirementService.findCurrentProjectRequirements(project.getProjectId());
    }
    projectRequirementComboBox.setItems(projectRequirements);
}

我在这里调用refreshProjectRequirementCombobox。

 projectComboBox.addValueChangeListener(event ->
            {
                refreshProjectRequirementCombobox();
                loadRejectReason();
            });

2 个答案:

答案 0 :(得分:1)

通常这应该起作用。我创建了一个带有两个组合框“ main”和“ dependent”的最小示例。从属组合框的选择取决于主组合框的选择。因此,主组合框上有一个ValueChangeListener,用于重置项和从属组合框的选定值。启动应用程序时,您会看到从属ComboBox的提供的项目发生了变化,并且这些新项目均未被选中。

我认为您必须发布更多代码(从哪里叫refreshProjectRequirementCombobox?),以查看您在做什么不同。

这是我的示例最低项目代码:

@Override
protected void init(VaadinRequest vaadinRequest) {
    final VerticalLayout layout = new VerticalLayout();
    final ComboBox<String> main = new ComboBox<>();
    final ComboBox<String> dependent = new ComboBox<>();
    final Map<String, String[]> dependentsByMain = new HashMap<>();
    dependentsByMain.put("A", new String[]{"AA", "AB", "AC"});
    dependentsByMain.put("B", new String[]{"BA", "BB", "BC"});
    dependentsByMain.put("C", new String[]{"CA", "CB", "CC"});

    List<String> mainItems = new ArrayList<>(dependentsByMain.keySet());
    main.setItems(mainItems);
    dependent.setItems(Arrays.asList("Test1", "Test2", "Test3"));
    dependent.setValue("Test1");
    main.addValueChangeListener((HasValue.ValueChangeListener<String>) valueChangeEvent -> {
        if (valueChangeEvent.getValue() != null) {
            dependent.setItems(dependentsByMain.get(valueChangeEvent.getValue()));
            dependent.setValue(null);
        }
    });
    layout.addComponents(main, dependent);
    setContent(layout);
}

更新

看看Srinivasan Sekar的回答及其评论。这是使用的版本(8.0.7)中的一个错误,该错误似乎已在8.5版中修复(根据https://github.com/vaadin/framework/issues/9047#issuecomment-437864866)。我尝试使用8.7.1版的示例代码,因此可以正常工作。对于版本8.0.7,则不是。

所以主要的解决方案是更新使用的Vaadin版本。解决方法(无法升级Vaadin版本时),首先必须将ComboBox的值设置为null,然后设置新项。因此,在我的示例中,ValueChangeListener必须类似于:

main.addValueChangeListener((HasValue.ValueChangeListener<String>) valueChangeEvent -> {
    if (valueChangeEvent.getValue() != null) {
        dependent.setValue(null);
        dependent.setItems(dependentsByMain.get(valueChangeEvent.getValue())); 
    }
});

答案 1 :(得分:1)

vaddin https://github.com/vaadin/framework/issues/9566中涉及https://github.com/vaadin/framework/issues/2813的未解决问题

解决我发现的问题,

通过创建自定义组合框,您可以解决此问题,

public class ClearableComboBox<T> extends ComboBox<T> {
    public ClearableComboBox(String in) {
        super(in);
    }

    protected void setSelectedFromServer(T item) {
        String key = itemToKey(item);

        T oldSelection = getSelectedItem().orElse(getEmptyValue());
        doSetSelectedKey(key);

        fireEvent(new SingleSelectionEvent<>(ClearableComboBox.this, oldSelection, false));
    }       
}

另外,请确保先致电setValue,然后再致电setItems清除项目。

cmb.setValue(null);
cmb.setItems(aEmptyCollection);