Ajouter dynamiquement des composants à la liste de Wicket

Je veux faire un formulaire avec un bouton "Ajouter". Après avoir appuyé sur le bouton "Ajouter" nouveau panneau ajoute au guichet élément ListView. Comment dois-je faire? Je veux être en mesure d'ajouter un nombre illimité de lignes.

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"));
    }
}

Cela ne fonctionne simplement pas. Peut-on savoir pourquoi? Grâce

Je pense que vous devriez commencer à partir de ce point: cwiki.apache.org/WICKET/forms-with-dynamic-elements.html
pour ce travail avec l'ajax vous avez besoin de 2 choses: tout composant qui vous permettra de mettre à jour dans votre ajax lien. mettre un WebMarkupContainer autour de votre ListView (par exemple, un <div wicket:id="wmc">) et dans le onClick() ajoutent que ling à la AjaxRequestTarget (la cible.addComponent(wmc) La cible contient tous les Composants qui seront actualisées via Ajax. Et, ensemble wmc.setOutputMarkupId(true); vous n'avez pas besoin de cela sur le newInteractivePanel
Cela a aidé. Merci
Vous feriez mieux d'utiliser un composant comme RepeatingView de sorte que vous n'avez pas à emballer vos widgets dans une liste/ modèle pour les ajouter à une liste.

OriginalL'auteur zinovii | 2010-01-22