代码之家  ›  专栏  ›  技术社区  ›  tefozi

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

  •  4
  • tefozi  · 技术社区  · 15 年前

    我想用“添加”按钮创建一个表单。按下“添加”按钮后,新面板将添加到wicket listview元素。我该怎么做?我想能够添加无限数量的行。

    编辑:

    交互面板page.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>
    

    互动面板page.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;
        }
    }
    

    交互式面板.html

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

    互动面板.java

    // ... imports
    public class InteractivePanel extends Panel {
        private static final long serialVersionUID = 1L;
    
        public InteractivePanel(String id) {
            super(id);
    
            add(new Button("simpleButton"));
        }
    }
    

    这根本不起作用。有人知道为什么吗?谢谢

    1 回复  |  直到 13 年前
        1
  •  3
  •   bert    15 年前

    我想您已经在列表视图中显示了元素列表了吧?您不只是简单地将新元素添加到备份ListView的列表中。如果在构造函数中传递列表,则考虑ListView不会刷新这些项。

    所以,而不是

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

    您应该使用包装列表的模型:

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

    以及getPersonalList()访问器。

    你可以看看 Legup 生成wicket,spring,jpa原型。在代码中,您将找到一个事件页面来执行此操作。