将组件动态添加到Wicket中的ListView [英] Dynamically add components to ListView in Wicket

查看:161
本文介绍了将组件动态添加到Wicket中的ListView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

编辑:

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

推荐答案

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

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.

所以,而不是

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

你应该使用一个包裹List的模型:

you should use a Model that wraps the List:

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

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

along with a getPersonList() accessor in this.

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

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.

这篇关于将组件动态添加到Wicket中的ListView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆