如何在将 ComboBox 值设置为 null 后清除该字段?
How to clear the field after i set ComboBox value as null?
我有一个名为 projectRequirementComboBox 的组合框,它依赖于 projectComboBox,从那里我得到列表以显示在 projectRequirementComboBox 的下拉列表中,但我想做类似的事情:当用户更改项目时,我想清空 projectRequirementComboBox ,为了更清楚 none 的项目将不会被选中我现在正在这样做但我的 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();
});
通常这应该有效。我创建了一个包含两个 ComboBoxes "main" 和 "dependent" 的最小示例。从属 ComboBox 的选择取决于主 ComboBox 的选择。因此,主 ComboBox 上有一个 ValueChangeListener,用于重置从属 ComboBox 的项目和选定值。当您启动该应用程序时,您会看到相关 ComboBox 的提供项目发生变化,并且这些新项目中的 none 被选中。
我认为你必须 post 更多你的代码(你从哪里调用 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()));
}
});
Vaadin 中存在未解决的问题 https://github.com/vaadin/framework/issues/9566 which refers to 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));
}
}
此外,请确保在调用 setItems
清除项目之前调用 setValue
。
cmb.setValue(null);
cmb.setItems(aEmptyCollection);
我有一个名为 projectRequirementComboBox 的组合框,它依赖于 projectComboBox,从那里我得到列表以显示在 projectRequirementComboBox 的下拉列表中,但我想做类似的事情:当用户更改项目时,我想清空 projectRequirementComboBox ,为了更清楚 none 的项目将不会被选中我现在正在这样做但我的 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();
});
通常这应该有效。我创建了一个包含两个 ComboBoxes "main" 和 "dependent" 的最小示例。从属 ComboBox 的选择取决于主 ComboBox 的选择。因此,主 ComboBox 上有一个 ValueChangeListener,用于重置从属 ComboBox 的项目和选定值。当您启动该应用程序时,您会看到相关 ComboBox 的提供项目发生变化,并且这些新项目中的 none 被选中。
我认为你必须 post 更多你的代码(你从哪里调用 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()));
}
});
Vaadin 中存在未解决的问题 https://github.com/vaadin/framework/issues/9566 which refers to 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));
}
}
此外,请确保在调用 setItems
清除项目之前调用 setValue
。
cmb.setValue(null);
cmb.setItems(aEmptyCollection);