java 如何在 DropDownChoice Wicket 中保留“选择一个”选项?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6984466/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 18:09:32  来源:igfitidea点击:

How to keep "Choose One" option in a DropDownChoice Wicket?

javawicketdropdownchoice

提问by Sephy

When I first load a page, the default option on a dropdownchoice is "Choose One". Is there a way to keep it in the dropdown, even when I have selected a choice?
(In case I would like to put nothing later)

当我第一次加载页面时,下拉选择的默认选项是“选择一个”。有没有办法将它保留在下拉列表中,即使我选择了一个选项?
(以防我以后什么都不想放)

回答by Marcelo

You need to use the DropDownChoice.setNullValid()method. From the javadoc:

您需要使用该DropDownChoice.setNullValid()方法。从javadoc:

Determines whether or not the null value should be included in the list of choices when the field's model value is nonnull, and whether or not the null_valid string property (e.g. "Choose One") should be displayed until a nonnull value is selected. If set to false, then "Choose One" will be displayed when the value is null. After a value is selected, and that change is propagated to the underlying model, the user will no longer see the "Choose One" option, and there will be no way to reselect null as the value. If set to true, the null string property (the empty string, by default) will always be displayed as an option, whether or not a nonnull value has ever been selected. Note that this setting has no effect on validation; in order to guarantee that a value will be specified on form validation, FormComponent.setRequired(boolean). This is because even if setNullValid() is called with false, the user can fail to provide a value simply by never activating (i.e. clicking on) the component.

确定当字段的模型值为非空时是否应将空值包含在选择列表中,以及是否应显示 null_valid 字符串属性(例如“选择一个”),直到选择了非空值。如果设置为false,则当值为空时将显示“选择一个”。选择一个值并将该更改传播到底层模型后,用户将不再看到“选择一个”选项,并且将无法重新选择 null 作为值。如果设置为 true,则空字符串属性(默认情况下为空字符串)将始终显示为选项,无论是否曾经选择过非空值。请注意,此设置对验证没有影响;为了保证在表单验证中指定一个值,FormComponent。setRequired(布尔值)。这是因为即使 setNullValid() 以 false 调用,用户也可能因为从不激活(即单击)组件而无法提供值。

If you want to keep the "Choose One" text with NullValid = true you can use a line similar to the following one in your Application.properties file:

如果您想保留 NullValid = true 的“选择一个”文本,您可以在 Application.properties 文件中使用类似于以下内容的一行:

nullValid=[Choose one]

回答by Rytis Guntulis

In .java:

在 .java 中:

DropDownChoice<Boolean> myDropDown = new DropDownChoice<>(
    "myDropDownWicketId", model, Arrays.asList(true, false), renderer);
myDropDown.setNullValid(true);

In .properties file associated with java class add:

在与 java 类关联的 .properties 文件中添加:

myDropDownWicketId.nullValid=Choose One
myDropDownWicketId.true=Yes
myDropDownWicketId.false=No