I made a download page in Wicket. As you can see, it's a DataView where you can download a file, depending on the id column and the DropDownChoice 'version'.
So clicking 'Download' on id 160 with version 3 should download file160ver3.txt, while on id 159 with version 2 it should download file159ver2.txt. Unfortunately updating the DropDownChoice doesn't get reflected in the model. So clicking on the Download button always downloads the file in the same version. As I have defaulted to Version 2 in my DropDownChoice, it always downloads this version.
Here's my code:
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);
What is it that I'm missing? How do I update the DropDownChoice?
Update and solution (changed according to Svens suggestion):
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();
Thanks.
... And here's the complete code (Java and HTML below):
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", 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);
DownloadLink download;
// The value of choice.getModelObject() doesn't change
download = new DownloadLink("download", getFile(p.getId(), 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>
You have to use a AjaxFormComponentUpdatingBehavior
to transfer the newly selected item to the Java component (and its model):
choice.add(new AjaxFormComponentUpdatingBehavior("change") {
@Override
protected void onUpdate(AjaxRequestTarget target) {
}
});
https://ci.apache.org/projects/wicket/guide/8.x/single.html#_ajaxformcomponentupdatingbehavior
And then your downloadLink has dynamically adjust to the current selection too:
download = new DownloadLink("download", () -> {
return getFile(p.getId(), choice.getModelObject()
});
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments