Hi Michael,
One thing that I personally think is important
to understanding ListView is knowing that each item in the ListView is one
concrete model object instance. If you think about it, then you will see
you shouldn't need many(if any) collections in your form backing model
classes. So a ListView takes a 'list' of model objects and
displays each one independent of the other. One of the cool things about
wicket is that your model object instance fields can be strongly typed
(more easily than Struts). As you'll see in the model object below. Take a
look at the following code which is based on your example (I only used generics
for type clarity):
public class DetailForm extends Form { private final List ageList = Arrays.asList(new Integer[] { Integer.valueOf(10), Integer.valueOf(20), Integer.valueOf(30) }); // You'll probably want to pass your list in to your form
constructor, as opposed to this.
private final static List<DetailModel> model = initModelList(); public DetailForm(final String id) {
super(id);
// This model is a list of DetailModels
ListView listView = new ListView("detailList", model) { public void populateItem(final ListItem listItem) {
listItem.setModel(new
CompoundPropertyModel(
(DetailModel) listItem.getModelObject())); // This is bound to
model.getCode()/setCode()
listItem.add(new TextField("code")); // Same for
model.getAge()/setAge()
listItem.add(new DropDownChoice("age", ageList)); // Same for
model.getName()/setName()
listItem.add(new TextField("name")); } }; add(listView); }
// Just a test to see that your changes are taking
effect.
@Override protected void onSubmit() { System.out.println(model); } // Again, not ideal, but for the sake of this example.
private static List<DetailModel> initModelList()
{
List<DetailModel> model = new ArrayList<DetailModel>(); model.add(new DetailModel(Integer.valueOf(10), "Harry", "bigboy")); model.add(new DetailModel(Integer.valueOf(20), "Jan", "foo")); model.add(new DetailModel(Integer.valueOf(30), "Meg", "boss")); return model; } public static class DetailModel implements Serializable {
private Integer age;
private String name; private String code; public DetailModel() {
}
public DetailModel(Integer age, String name, String code)
{
super(); this.age = age; this.name = name; this.code = code; } public String getCode() {
return code; } public void setCode(String code)
{
this.code = code; } public Integer getAge() {
return age; } public void setAge(Integer age) {
this.age = age; } public String getName() {
return name; } public void setName(String name)
{
this.name = name; } @Override
public String toString() { return name + ", " + age + "(" + code + ")"; } } }
Good luck, I hope this helps you understand. Just remember that it
really is as easy as it should be, which (sadly) takes some getting used to if
you've used other frameworks.
|
- Re: [Wicket-user] Newbie question: applying dynamic form c... Jonathan Cone
- Re: [Wicket-user] Newbie question: applying dynamic f... Vincent Jenks
- Re: [Wicket-user] Newbie question: applying dynam... Jonathan Cone
- Re: [Wicket-user] Newbie question: applying dynamic f... Michael K
- Re: [Wicket-user] Newbie question: applying dynam... Eelco Hillenius