Additions seem to work fine with setReuseItems(true), but I've been
having trouble with removals. My remove AjaxButton calls
listView.getList().remove(item.getIndex()), but that always removes
the last element. Am I making an incorrect assumption about how this
should work?
The following is a simplified version of my FormComponentPanel that
replicates the problem. It's used the a CompoundPropertyModel with a
POJO containing the string list.
public class TextFieldListView extends FormComponentPanel<List<String>> {
private static final long serialVersionUID = 1L;
private final ListView<String> listView;
public TextFieldListView(final String id) {
super(id);
setOutputMarkupId(true);
listView = new ListView<String>("list") {
private static final long serialVersionUID = 1L;
@Override
protected void populateItem(final ListItem<String> item) {
final TextField<String> textField = new
TextField<String>("item", item.getModel());
item.add(textField);
item.add(new RemoveButton("remove", item));
}
};
listView.setReuseItems(true);
add(listView);
add(new AddButton("add"));
}
@Override
public void onInitialize() {
super.onInitialize();
listView.setModel(getModel());
}
@SuppressWarnings("unchecked")
protected void convertInput() {
this.setConvertedInput((List<String>)listView.getList());
}
private class AddButton extends AjaxButton {
private static final long serialVersionUID = 1L;
public AddButton(final String id) {
super(id);
setDefaultFormProcessing(false);
}
@Override
protected void onSubmit(final AjaxRequestTarget target, final
Form<?> form) {
listView.getModelObject().add("");
target.add(TextFieldListView.this);
}
@Override
protected void onError(final AjaxRequestTarget target, final
Form<?> form) {
throw new IllegalStateException("AJAX request threw an error");
}
}
private class RemoveButton extends AjaxButton {
private static final long serialVersionUID = 1L;
private final ListItem<String> item;
public RemoveButton(final String id, final ListItem<String> item) {
super(id);
this.item = item;
setDefaultFormProcessing(false);
}
@Override
protected void onSubmit(final AjaxRequestTarget target, final
Form<?> form) {
listView.getList().remove(item.getIndex());
target.add(TextFieldListView.this);
}
@Override
protected void onError(final AjaxRequestTarget target, final
Form<?> form) {
throw new IllegalStateException("AJAX request threw an error");
}
};
}
Thanks!
On Thu, Aug 2, 2012 at 10:13 AM, Andrea Del Bene <[email protected]> wrote:
> Hi,
>
> what kind of problem have you encountered with ListView?
>>
>> Hi,
>>
>> I have a List<String> that I want to represent with TextFields. I need
>> to be able to dynamically add and remove them via AJAX.
>>
>> From what I’ve read online (and experienced firsthand) ListViews do
>> not seem to be designed for this.
>>
>> What is the best way to approach this problem?
>>
>> Thanks!
>> Ben
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: [email protected]
>> For additional commands, e-mail: [email protected]
>>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [email protected]
> For additional commands, e-mail: [email protected]
>
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]