java - 在 Wicket 中动态添加组件到 ListView

标签 java wicket

我想制作一个带有“添加”按钮的表单。按下“添加”按钮后,新面板将添加到 wicket ListView 元素。我怎么做?我希望能够添加无限数量的行。

编辑:

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

这根本行不通。谁能明白为什么?谢谢

最佳答案

我想您已经在 ListView 中显示了一个元素列表?您不是简单地将新元素添加到支持您的 ListView 的列表中。考虑到如果您在构造函数中传入 List,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");

与此中的 getPersonList() 访问器一起。

你可以看看Legup并生成 Wicket、Spring、JPA 原型(prototype)。在代码中,您会找到执行此操作的 EventPage。

关于java - 在 Wicket 中动态添加组件到 ListView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2114351/

相关文章:

java - 单元测试 ModalWindow 的内容刷新失败,而实际功能按预期工作 - 我做错了什么?

java - Wicket NestedTree 在展开节点后挂起 Internet Explorer 10

Java Bean 约定

java - 内部类和局部变量问题

java - Play 2.5.3 : Using dependency injection to get configuration values

Java 将匹配项分配给变量

wicket - 使用 Wicket 1.5 提供动态内容

java - 将对象传递给构造函数最好是作为对象还是作为页面参数中的序列化对象?

java - 在多模块项目配置文件中让 Maven 站点插件工作?

javascript - 尽管 JavascriptFilteredIntoFooterHeaderResponse 将 JS 添加到 header