Reputation: 5185
I'm trying to implement an AJAXfied Wicket list view. On my research I stumbled upon the code on this site and modified it a bit.
The model is not updated properly. So whenever a value is entered in a text field, it is forgotten if the AJAX submit link is invoked (the text field is empty). Why does this happen? I don't see any issue with this code. Wicket version is 1.5.2.
Here is the Java code:
// Initialization of form
...
// List all rows
ArrayList<String> rows = new ArrayList<String>(2);
rows.add(new String());
rows.add(new String());
final ListView<String> lv = new ListView<String>("rows", rows) {
@Override
protected void populateItem(ListItem<String> item) {
int index = item.getIndex() + 1;
item.add(new Label("index", index + "."));
TextField<String> text = new TextField<String>("text", item.getModel());
item.add(text);
}
};
rowPanel.add(lv);
AjaxSubmitLink addLink = new AjaxSubmitLink("addRow", form) {
@Override
protected void onError(AjaxRequestTarget target, Form<?> form) {
if (target != null) target.add(rowPanel);
}
@Override
protected void onSubmit(AjaxRequestTarget target, Form<?> form) {
lv.getModelObject().add(new String());
if (target != null) target.add(rowPanel);
}
};
addLink.setDefaultFormProcessing(false);
rowPanel.add(addLink);
...
And here is the mark up:
<div wicket:id="rowPanel">
<span wicket:id="rows">
<span wicket:id="index">1.</span>
<input type="text" wicket:id="text"/>
</span>
<a href="#" wicket:id="addRow">Add row</a>
</div>
Upvotes: 1
Views: 5340
Reputation: 11
You have to use the model from the target like this:
IModel model = target.getModel();
Then cast to the listView. I don't know if this works, but I have some similar code here.
Upvotes: 1
Reputation: 780
Your example would work fine if you hadn't added the following line:
addLink.setDefaultFormProcessing(false);
Your link doesnt process the form like he would normally do (update models etc, see IFormSubmitter)
You could use nested Forms to updated only the area you need and set defaultFormProcessing back to true.
Like:
<form wicket:id="form">
(...) <!-- (other form elements you dont want to get updated -->
<form wicket:id="repeaterForm"> <!-- form for your repeater textfields-->
<div wicket:id="refreshDiv">
<input type="textfield" wicket:id="repeaterText" />
</div>
<a wicket:id="addMoreLink>add more</a>
</form>
</form>
Wicket will make sure you don't actually have nested forms within your markup (it will replace the nested form with a div) because it ain't valid HTML. But it would work as if the forms are nested nevertheless.
Upvotes: 1
Reputation: 7696
My previous comment as anwser:
You may need to call setReuseItems(true) on the listview.
Another way of ajaxifying a listview can be found at the Wicket in Action blog
Upvotes: 3