如何解决Wicket:更新 DataView 中的 DropDownChoice
我在 Wicket 中制作了一个下载页面。如您所见,它是一个 DataView,您可以在其中下载文件,具体取决于 id 列和 DropDownChoice“版本”。
因此,在版本 3 的 id 160 上单击“下载”应下载 file160ver3.txt,而在版本 2 的 id 159 上应下载 file159ver2.txt。不幸的是,更新 DropDownChoice 并没有反映在模型中。因此,单击“下载”按钮始终会下载相同版本的文件。因为我在 DropDownChoice 中默认使用版本 2,所以它总是下载这个版本。
这是我的代码:
DropDownChoice<Integer> choice = new DropDownChoice<>("version",new Model<Integer>(2),List.of(1,2,3));
choice.add(new AjaxEventBehavior("change") {
@Override
protected void onEvent(AjaxRequestTarget target) {
target.add();
System.out.println(choice.getModelObject()); // doesn't change
}
});
item.add(choice);
// The value of choice.getModelObject() doesn't change
DownloadLink download = new DownloadLink("download",getFile(p.getId(),choice.getModelObject()));
download.setoutputMarkupId(true);
item.add(download);
我错过了什么?如何更新 DropDownChoice?
choice.add(new AjaxFormComponentUpdatingBehavior("change") {
@Override
protected void onUpdate(AjaxRequestTarget target) {
System.out.println(choice.getModelObject());
}
});
item.add(choice);
DownloadLink download = new DownloadLink("download",() -> {
return getFile(p.getId(),choice.getModelObject());
});
// ...
private File getFile(int id,DropDownChoice<Integer> choice) throws FileNotFoundException,IOException {
Integer version = choice.getModelObject();
谢谢。
... 这是完整的代码(下面是 Java 和 HTML):
public DownloadPage(PageParameters params) {
List<PrefKey> prefKeys = db.getPrefKeys();
DataView<PrefKey> dataView = getDataView(prefKeys);
Form<Void> form = new Form<>("form");
add(form);
form.add(dataView);
}
private DataView<PrefKey> getDataView(List<PrefKey> prefKeys) {
IDataProvider<PrefKey> provider = new ListDataProvider<>(prefKeys);
DataView<PrefKey> dataView = new DataView<>("dbAsDataView",provider,10) {
private static final long serialVersionUID = 12345L;
@Override
protected void populateItem(Item<PrefKey> item) {
PrefKey p = item.getModelObject();
item.add(new Label("tdId",p.getId()));
item.add(new Label("tdKey",p.getKey()));
try {
DropDownChoice<Integer> choice = new DropDownChoice<>("version",3));
choice.add(new AjaxEventBehavior("change") {
@Override
protected void onEvent(AjaxRequestTarget target) {
target.add();
System.out.println(choice.getModelObject()); // doesn't change
}
});
item.add(choice);
DownloadLink download;
// The value of choice.getModelObject() doesn't change
download = new DownloadLink("download",choice.getModelObject()));
download.setoutputMarkupId(true);
item.add(download);
} catch (IOException e) {
e.printstacktrace();
}
}
};
return dataView;
}
<h1>Wicket Download</h1>
<form wicket:id="form" action="">
<table id="tblDataView" class="table table-striped table-hover">
<thead>
<tr>
<th>Id</th>
<th>Key</th>
<th>Version</th>
<th>Download</th>
</tr>
</thead>
<tbody>
<tr wicket:id="dbAsDataView">
<td wicket:id="tdId"></td>
<td wicket:id="tdKey"></td>
<td><select wicket:id="version"></select></td>
<td><input type="button" wicket:id="download" value="Download"></input></td>
</tr>
</tbody>
</table>
</form>
解决方法
您必须使用 AjaxFormComponentUpdatingBehavior
将新选择的项目传输到 Java 组件(及其模型):
choice.add(new AjaxFormComponentUpdatingBehavior("change") {
@Override
protected void onUpdate(AjaxRequestTarget target) {
}
});
https://ci.apache.org/projects/wicket/guide/8.x/single.html#_ajaxformcomponentupdatingbehavior
然后您的下载链接也已根据当前选择动态调整:
download = new DownloadLink("download",() -> {
return getFile(p.getId(),choice.getModelObject()
});
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。