tags:

views:

1560

answers:

2

I want to make a form with "Add" button. After pressing "Add" button new panel adds to the wicket ListView element. How do I do that? I want to be able add unlimited number of rows.

EDIT:

InteractivePanelPage.html

<table>
    <tr>
        <td><a href="#" wicket:id="addPanelLink">Add Panel</a></td>
    </tr>
    <tr wicket:id="interactiveListView">
        <td>
        <span wicket:id="interactiveItemPanel"></span>
        </td>
    </tr>
</table>

InteractivePanelPage.java

// ... imports
public class InteractivePanelPage extends WebPage {
    public LinkedList<InteractivePanel> interactivePanels = new LinkedList<InteractivePanel>();

    private ListView<InteractivePanel> interactiveList;

    public InteractivePanelPage() {
        add(new AjaxLink<String>("addPanelLink") {
            private static final long serialVersionUID = 1L;

            @Override
            public void onClick(AjaxRequestTarget target) {
                try {
                    System.out.println("link clicked");

                    InteractivePanel newInteractivePanel = new InteractivePanel(
                            "interactiveItemPanel");
                    newInteractivePanel.setOutputMarkupId(true);

                    interactiveList.getModelObject().add(newInteractivePanel);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });

        interactivePanels.add(new InteractivePanel("interactiveItemPanel"));

        interactiveList = new ListView<InteractivePanel>("interactiveListView",
                new PropertyModel<List<InteractivePanel>>(this, "interactivePanels")) {
            private static final long serialVersionUID = 1L;

            @Override
            protected void populateItem(ListItem<InteractivePanel> item) {
                item.add(item.getModelObject());
            }
        };

        interactiveList.setOutputMarkupId(true);

        add(interactiveList);
    }

    public List<InteractivePanel> getInteractivePanels() {
        return interactivePanels;
    }
}

InteractivePanel.html

<html xmlns:wicket>
<wicket:panel>
<span><input type="button" value="BLAAA" wicket:id="simpleButton"/></span>
</wicket:panel>
</html>

InteractivePanel.java

// ... imports
public class InteractivePanel extends Panel {
    private static final long serialVersionUID = 1L;

    public InteractivePanel(String id) {
        super(id);

        add(new Button("simpleButton"));
    }
}

This simply doesn't work. Can anybody see why? Thanks

+1  A: 

I suppose you already display a list of elements in a ListView? You than simply add new elements to the list that backup your ListView. Consider that the ListView will not refresh the items if you pass in the List in the constructor.

So, instead of

List<Person> personList = new LinkedList<Person>();
ListView<Person> personView = new ListView<Person("listview", personList);

you should use a Model that wraps the List:

ListView<Person> personView = new ListView<Person("listview"
               , new PropertyModel<List<Person>>(this, "personList");

along with a getPersonList() accessor in this.

You can have a look at Legup and generate the Wicket, Spring, JPA archetype. In the code you will find a EventPage that does this.

bert
I have updated my question according to your comment. But it's still doesn't work. Could you please take a look why it doesn't work?
zdmytriv
A: 

I think you should start from this point: http://cwiki.apache.org/WICKET/forms-with-dynamic-elements.html

leonidv