我有简单的代码:
ComboBox<String> combo=new ComboBox<>("Combo");
Button button = new Button("Button");
button.addClickListener(new ComponentEventListener<ClickEvent<Button>>() {
@Override
public void onComponentEvent(ClickEvent<Button> event) {
combo.setItems("11","22");
combo.setValue("22");
}
});
首次单击按钮时,我有combobox和value" 22"中的项目" 11"one_answers" 22"。
第二次点击使价值清除,但仍然存在" 11"one_answers" 22"的项目。如果我选择" 11"或留下" 22"在Combobox和单击按钮中 - 值清除。
看来SetValue()仅在ComboBox为空时才有效,但遵循代码也无济于事:
combo.setValue(null);
combo.clear();
combo.setItems("11","22");
combo.setValue(null);
combo.clear();
combo.setValue("22");
以下代码设置Combobox的值正确,无论我在单击之前选择一些值还是清除值:
:ComboBox<String> combo=new ComboBox<>("Combo");
combo.setItems("11","22");
Button button = new Button("Button");
button.addClickListener(new ComponentEventListener<ClickEvent<Button>>() {
@Override
public void onComponentEvent(ClickEvent<Button> event) {
combo.setValue("22");
}
});
,但我必须动态设置Combobox项目,最后一个解决方案不适合我。Vaadin版本为10.0.9。有人有一些建议或建议吗?
ps。谢谢!
我尝试了遵循代码:
combo.setItems(Collections.emptyList());
combo.setItems("11","22");
combo.setValue("22");
,但它也不起作用。仅当组合中的值为空时,此代码才能起作用,但是如果我在组合中输入某些内容,则代码仅通过.setItems()
清除值,而进一步的.setValue()
不起作用。
如果组合的值为空,则代码效果很好。
基于https://vaadin.com/start/latest/project/project-base(使用vaadin 12.0.7),您的代码在最低项目中的工作原理非常好。
@Route("")
@PWA(name = "Project Base for Vaadin Flow", shortName = "Project Base")
public class MainView extends VerticalLayout {
public MainView() {
ComboBox<String> combo=new ComboBox<>("Combo");
Button button = new Button("Button");
button.addClickListener(new ComponentEventListener<ClickEvent<Button>>() {
@Override
public void onComponentEvent(ClickEvent<Button> event) {
combo.setItems("11","22");
combo.setValue("22");
}
});
add(combo,button);
}
}
您通过UI在ComboBox中设置的任何值。当单击按钮时,所选值将切换为22。
如果这是您的选项,则可以更新到较新的vaadin版本,然后尝试。
更好地显示我的评论中的意思,我将其写为答案。
我的意思是设置一个空的集合,而不是在ClickListener中,而是在初始化Combobox之后直接设置:
ComboBox<String> combo=new ComboBox<>("Combo");
combo.setItems(Collections.emptyList());
Button button = new Button("Button");
button.addClickListener(new ComponentEventListener<ClickEvent<Button>>() {
@Override
public void onComponentEvent(ClickEvent<Button> event) {
combo.setItems("11","22");
combo.setValue("22");
}
});
请尝试一下,让我知道是否有效