【发布时间】:2018-06-07 15:50:48
【问题描述】:
我有一个 ListView 以显示一个项目开始,我在其中为每个新项目附加一个 AjaxSubmitLink,它工作正常。 在 ListView 中,我有两个 DropDownChoices,第一个通过 AjaxFormComponentUpdatingBehavior 触发第二个的选择。这也有效,但前提是我将另一个项目添加到默认项目。 如果未点击 AjaxSubmitLink,则不会更新第二个 DropDownChoice,并且在 Ajax 调试窗口中有一个空白,而不是第一个 DropDownChoice 的 id。
这是我的代码:
final MarkupContainer devicescontainer = new WebMarkupContainer("devicesContainer");
devicescontainer.setOutputMarkupId(true);
add(devicescontainer);
final ListView devicesListView = new ListView<Device>("devices", devices) {
@Override
protected void populateItem(ListItem<Device> item) {
item.setModel(new CompoundPropertyModel<Device>(item.getModel()));
final List<Device.DeviceCategory> cats = Arrays.asList(Device.DeviceCategory.values());
final DropDownChoice<Device.DeviceCategory> categoryDropDownChoice = new DropDownChoice<Device.DeviceCategory>("deviceCategory", cats);
final DropDownChoice<Device.DeviceSubcategory> subcategoryDropDownChoice = new DropDownChoice<>("deviceSubcategory");
categoryDropDownChoice.setOutputMarkupId(true);
subcategoryDropDownChoice.setOutputMarkupId(true);
categoryDropDownChoice.add(new AjaxFormComponentUpdatingBehavior("change") {
@Override
protected void onUpdate(AjaxRequestTarget target) {
List<Device.DeviceSubcategory> subcats = Device.getPotentialSubcategories(categoryDropDownChoice.getModelObject());
subcategoryDropDownChoice.setChoices(subcats);
target.add(subcategoryDropDownChoice);
}});
item.add(categoryDropDownChoice);
item.add(subcategoryDropDownChoice);
}
}.setReuseItems(true);
devicescontainer.add(devicesListView);
AjaxSubmitLink addDeviceLink = new AjaxSubmitLink("addDevice") {
@Override
public void onSubmit(AjaxRequestTarget target, Form form) {
devicesListView.getModelObject().add(new Device(newId));
if (target != null){
target.add(devicescontainer);
}
}
};
addDeviceLink.setDefaultFormProcessing(false);
devicescontainer.add(addDeviceLink);
如何在不先单击“添加设备”链接的情况下让 Ajax 驱动的 DropDownChoice 工作?
编辑:所有元素生成的id都是完整且唯一的。 ListView 项目没有 id,如果有,也无济于事。
【问题讨论】:
-
您的标记中是否有空 id (id="")?
-
@svenmeier 不,任何地方都没有空 ID。 setOutputMarkupId() 不设置id吗?
-
Wicket 优先考虑 I 将出现在标记中,如果它们不再是唯一的,这有时会导致混乱和问题。
-
@svenmeier 啊谢谢。