Katie.Sun
Katie.Sun

Reputation: 711

Wicket form components value changes within ListView affecting all elements

I have a ListView I am using to add multiple rows with form elements (text inputs, selects, etc.) that correspond to a property on a model that is a list of elements. When the form first loads, the elements are initialized as new instances of that class. Wicket doesn't seem to be able to differentiate them unless I explicitly add different values to a property of that object. It only changes the value when I change something in the last row of the ListView, and when it does, it changes all of them as if they are all the same object.

For example:

Arrays.asList(new String(), new String(), new String()); 

vs.

Arrays.asList("a", "b", "c");

Wicket only changes the values for the individual row when the list passed into the ListView is implemented the second way.

Here is a simplified example of the code I am using:

WebMarkupContainer container = new WebMarkupContainer("container");
ListView<String> listView = new ListView<String>("propertyListView", model.getObjectsList()) {
                @Override
                protected void populateItem(final ListItem listItem) {
                    listItem.add(new TextField<String>("string", new PropertyModel<>(listItem.getModelObject(), "stringField")));
                }
            };
            listView.setReuseItems(true);
container.add(listView);
form.add(container);

My question is this: is there some sort of interface I can implement so I don't have to use the "cheat" of initializing the objects with different property values, and if so, what/how?

Upvotes: 0

Views: 93

Answers (1)

Imperative
Imperative

Reputation: 3183

You are unwrapping the models, that is not very Wicket'ish and is probably the reason for your problem.

I'd rewrite the code to something like this:

  ListView<String> listView = new ListView<String>("meetingFiles", model) {
                @Override
                protected void populateItem(final ListItem<String> listItem) {
                    listItem.add(new TextField<String>("string", listItem.getModel()));
                }
            };
            listView.setReuseItems(true);
   container.add(listView);
   form.add(container);

Upvotes: 2

Related Questions