DropDownChoice 设置选定的特定索引
我有一个 DropDownChoice :
DropDownChoice timePeriod = new DropDownChoice("timePeriod", Arrays.asList(new TimePeriod(1, "Weekly"), new TimePeriod(2, "Bi-Weekly"), new TimePeriod(3, "Semi-Monthly"), new TimePeriod(4, "Monthly"), new TimePeriod(5, "Yearly")), new IChoiceRenderer() {
private static final long serialVersionUID = 10102L;
@Override
public String getIdValue(Object object, int index) {
return ((TimePeriod) object).getId() + "";
}
@Override
public Object getDisplayValue(Object object) {
return ((TimePeriod) object).getPeriodType();
}
});
timePeriod.setNullValid(false);
我的问题是:
- 如何将所选索引设置为 2 或任何其他索引?
- 如何删除第一个默认空白选项?
谢谢。
I have a DropDownChoice :
DropDownChoice timePeriod = new DropDownChoice("timePeriod", Arrays.asList(new TimePeriod(1, "Weekly"), new TimePeriod(2, "Bi-Weekly"), new TimePeriod(3, "Semi-Monthly"), new TimePeriod(4, "Monthly"), new TimePeriod(5, "Yearly")), new IChoiceRenderer() {
private static final long serialVersionUID = 10102L;
@Override
public String getIdValue(Object object, int index) {
return ((TimePeriod) object).getId() + "";
}
@Override
public Object getDisplayValue(Object object) {
return ((TimePeriod) object).getPeriodType();
}
});
timePeriod.setNullValid(false);
My question is:
- How to set the selected index to 2 or any other?
- How to remove first default blank option?
Thank you.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
DropDownChoice 有一个接受默认值的构造函数。
或者在您的代码中,您可以添加 timePeriod.setModel(Model.of(new TimePeriod(2, "Bi-Weekly")))
我猜 TimePeriod 有正确的 #equals() 和 #hashCode() 实现。
关于空白选项:参见 org.apache.wicket.markup.html.form.AbstractSingleSelectChoice.isNullValid()
DropDownChoice has a constructor which accepts the default value.
Or in your code you can add timePeriod.setModel(Model.of(new TimePeriod(2, "Bi-Weekly")))
I guess TimePeriod has proper #equals() and #hashCode() implementations.
About the blank option: see org.apache.wicket.markup.html.form.AbstractSingleSelectChoice.isNullValid()
托加莫斯勋爵和马丁感谢你们俩。我做了一个小测试。而且它工作得很好。正如 Torgamus 勋爵所指出的,
上述代码是为其他用户提供的,因为它可能会有所帮助,我编写它是为了测试目的,因此所有类都写在一个 .java 文件中,尽管这是不可取的。
谢谢。
Lord Torgamus and martin-g thank you both of you. I did a small test. And it is working perfectly. As Lord Torgamus indicated,
The above code is provided for other users as it might be helpful and I wrote it for testing purpose so all the classes are written in one .java file although it is not advisable.
Thank you.
您应该能够使用
PropertyModel
设置所选索引,而不是对下拉列表的值进行硬编码。我目前无法测试,但它会像作为奖励,设置默认值的行为本身应该消除空白选项问题。
You should be able to set the selected index by using a
PropertyModel
instead of hard-coding the values of the dropdown. I can't test at the moment, but it would be something likeAs a bonus, the very act of setting a default value should eliminate the blank option problem.