将选定的 DropDownChoice 转换为 CompoundPropertyModel 中的模型
Convert selected DropDownChoice to Model in CompoundPropertyModel
当表单具有附加了模型的 CompoundPropertyModel 时,是否有可能以 Wicket 方式转换选定的 DropDownChoices 值,该模型具有特定属性的另一种类型。
简单的例子,因为我想我的解释不是很准确:
public enum MyChoices {
ONE(1),TWO(2),THREE(3);
// ... etc
}
public class MyEntityModel {
private int number;
private String text;
}
// the WebPages constructor:
public ChoicePage() {
IModel<MyEntityModel> model = new CompoundPropertyModel<>(new EntityModel());
Form<MyEntityModel> form = new Form<MyEntityModel>("form", model);
add(form);
form.add(new TextField<String>("text"));
form.add(new DropDownChoice<>("choices", Model.of(MyChoices.ONE),
Arrays.asList(MyChoices.values()))
}
提交表单时选择了一个,我希望模型对象的值为 1
。
我知道,我可以命名 DropDownChoice 组件而不是 MyEntityModel 字段,并在提交后将其值复制到模型中。但这不是 Wickets 模型方法,对吗?
P.s.: 我正在使用 Wicket 6.17.0
你必须做一些转换。
要么转换选择列表:
form.add(new DropDownChoice<Integer>("number",
new AbstractReadOnlyModel<List<Integer>>() {
public List<Integer> getObject() {
return MyChoices.getAllAsInts();
}
}
);
或选择的选项:
form.add(new DropDownChoice<MyChoices>("number", Arrays.asList(MyChoices.values()) {
public IModel<?> initModel() {
final IModel<Integer> model = (IModel<Integer>)super.initModel();
return new IModel<MyChoice>() {
public MyChoice getObject() {
return MyChoice.fromInt(model.getObject());
}
public void setObject(MyChoice myChoice) {
model.setObject(myChoice.toInt());
}
};
}
);
当表单具有附加了模型的 CompoundPropertyModel 时,是否有可能以 Wicket 方式转换选定的 DropDownChoices 值,该模型具有特定属性的另一种类型。
简单的例子,因为我想我的解释不是很准确:
public enum MyChoices {
ONE(1),TWO(2),THREE(3);
// ... etc
}
public class MyEntityModel {
private int number;
private String text;
}
// the WebPages constructor:
public ChoicePage() {
IModel<MyEntityModel> model = new CompoundPropertyModel<>(new EntityModel());
Form<MyEntityModel> form = new Form<MyEntityModel>("form", model);
add(form);
form.add(new TextField<String>("text"));
form.add(new DropDownChoice<>("choices", Model.of(MyChoices.ONE),
Arrays.asList(MyChoices.values()))
}
提交表单时选择了一个,我希望模型对象的值为 1
。
我知道,我可以命名 DropDownChoice 组件而不是 MyEntityModel 字段,并在提交后将其值复制到模型中。但这不是 Wickets 模型方法,对吗?
P.s.: 我正在使用 Wicket 6.17.0
你必须做一些转换。
要么转换选择列表:
form.add(new DropDownChoice<Integer>("number",
new AbstractReadOnlyModel<List<Integer>>() {
public List<Integer> getObject() {
return MyChoices.getAllAsInts();
}
}
);
或选择的选项:
form.add(new DropDownChoice<MyChoices>("number", Arrays.asList(MyChoices.values()) {
public IModel<?> initModel() {
final IModel<Integer> model = (IModel<Integer>)super.initModel();
return new IModel<MyChoice>() {
public MyChoice getObject() {
return MyChoice.fromInt(model.getObject());
}
public void setObject(MyChoice myChoice) {
model.setObject(myChoice.toInt());
}
};
}
);