将组件动态添加到Wicket中的ListView

我想用“添加”按钮创建一个表单。 按“添加”按钮后,新面板将添加到检票口ListView元素中。 我怎么做? 我希望能够添加无限数量的行。

编辑:

InteractivePanelPage.html

Add Panel

InteractivePanelPage.java

 // ... imports public class InteractivePanelPage extends WebPage { public LinkedList interactivePanels = new LinkedList(); private ListView interactiveList; public InteractivePanelPage() { add(new AjaxLink("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("interactiveListView", new PropertyModel<List>(this, "interactivePanels")) { private static final long serialVersionUID = 1L; @Override protected void populateItem(ListItem item) { item.add(item.getModelObject()); } }; interactiveList.setOutputMarkupId(true); add(interactiveList); } public List getInteractivePanels() { return interactivePanels; } } 

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

这根本不起作用。 谁能明白为什么? 谢谢

我想你已经在ListView中显示了一个元素列表? 您只需将新元素添加到备份ListView的列表中即可。 如果您在构造函数中传入List,则认为ListView不会刷新项目。

所以,而不是

 List personList = new LinkedList(); ListView personView = new ListView 

你应该使用一个包装List的Model:

 ListView personView = new ListView>(this, "personList"); 

以及此中的getPersonList()访问器。

您可以查看Legup并生成Wicket,Spring,JPA原型。 在代码中,您将找到执行此操作的EventPage。