Hi James, The best explanation for adding items via Ajax you could read at http://wicketinaction.com/2008/10/repainting-only-newly-created-repeater-items-via-ajax/ Removing an item should be easier if the ListItems have id.
On Tue, Mar 27, 2018 at 3:42 PM, James Carman <ja...@carmanconsulting.com> wrote: > It has been a while since I've used Wicket and man, I really forgot how > much I love this framework! It may be that I'm rusty, but I've searched > quite a bit and tried all the suggestions I've found, but I can't seem to > make add/remove via AJAX work for a ListView while preserving the input > data. I am using Wicket 8.0.0-M9. Here's what I've done so far: > > correctChoiceGroup = new CheckGroup<>("correctChoices", > Model.ofSet(Sets.newHashSet())); > correctChoiceGroup.setRequired(true); > correctChoiceGroup.setOutputMarkupId(true); > correctChoiceGroup.setRenderBodyOnly(false); > > final IModel<List<String>> choicesModel = Model.ofList(new LinkedList<>()); > choicesView = new ListView<String>("choices", choicesModel) { > @Override > protected void populateItem(ListItem<String> item) { > final int index = item.getIndex(); > > item.add(new TextField<>("field", item.getModel())); > item.add(new Check<>("check", Model.of(index))); > > final AjaxSubmitLink deleteLink = new > AjaxSubmitLink("deleteButton") { > @Override > @SuppressWarnings("unchecked") > protected void onSubmit(AjaxRequestTarget target) { > final ListView listView = findParent(ListView.class); > listView.getModelObject().remove(index); > listView.removeAll(); > target.add(correctChoiceGroup); > } > }; > deleteLink.setDefaultFormProcessing(false); > item.add(deleteLink); > } > }.setReuseItems(true); > > > correctChoiceGroup.add(choicesView); > > add(new AjaxSubmitLink("addButton") { > @Override > protected void onSubmit(AjaxRequestTarget target) { > choicesView.getModelObject().add(""); > choicesView.removeAll(); > target.add(correctChoiceGroup); > } > }.setDefaultFormProcessing(false)); > > add(correctChoiceGroup); > > > When I click on the delete link (I have a similarly-implemented "add" link > outside of the ListView), all of the input data goes away, since I am > losing the original items from the removeAll() call. Now, I am doing the > removeAll() to force Wicket to realize that I've changed the underlying > list, otherwise, the original items are reused and the it always looks like > the last item is removed. I'm sure I'm just rusty, but I am banging my > head on this one. > > Thanks, > > James >