如何在RepeatingView中重复按钮

时间:2014-07-23 13:20:54

标签: button wicket repeat

如何将一个Button添加到RepeatingView?像Label这样的其他组件运行良好,但Button无法正常渲染。

我的代码:

RepeatingView repeatingView = new RepeatingView(componentId);
for (...) {
    AjaxButton button = new AjaxButton(repeatingView.newChildId()) {
        ...
    };
    repeatingView.add(button);
 }

编辑:

我决定创建自定义列,可以在一行中渲染更多按钮。 我读到该按钮必须包含在面板中才能正确显示。 我们假设自定义列的名称是MultiButtonColumn。

原理如下:从对象T获取列表L,对于每个属性P从L创建按钮的项目。文本按钮是P值。

Java类:MultiButtonColumn.java

/**
* @param <T>
* @param <L> list property
* @param <P> item property
*/
public abstract class MultiButtonColumn<T, L, P> extends AbstractColumn<T, String> {

    private static final long serialVersionUID = 3512450712767559519L;

    private List<Button> buttonList = new ArrayList<Button>();
    private String propertyList;
    private String property;

    public MultiButtonColumn(IModel<String> displayModel, final String propertyList, final String property) {
        super(displayModel);
        this.propertyList = propertyList;
        this.property = property;
    }

    @Override
    public void populateItem(Item<ICellPopulator<T>> cellItem, String componentId, IModel<T> rowModel) {
        List<L> list = (List<L>) new PropertyModel<L>(rowModel, propertyList).getObject();
        cellItem.add(new RepeaterPanel(componentId, list));
        cellItem.getParent().getParent().setOutputMarkupId(true);
    }

    protected abstract Button newButton(String id, PropertyModel<P> propertyModel);

    private class RepeaterPanel extends Panel {

        private static final long serialVersionUID = 1L;

        public RepeaterPanel(String id, List<L> list) {
            super(id);

            RepeatingView listItems = new RepeatingView("repeaterPanel");

            for (L l : list) {
                add(new ButtonPanel(listItems.newChildId(), new PropertyModel<P>(l, property)));
            }
            add(listItems);
        }
    }

    // wrap button to panel for correct view
    private class ButtonPanel extends Panel {

        private static final long serialVersionUID = 1L;

        public ButtonPanel(String id, PropertyModel<P> propertyModel) {
            super(id);
            Button button = newButton("button", propertyModel);
            buttonList.add(button);
            add(button);
        }

    }

    public List<Button> getButtonList() {
        return buttonList;
    }

}

Html:MultiButtonColumn $ RepeaterPanel.html

<?xml version="1.0" encoding="UTF-8" ?>

<wicket:panel xmlns:wicket="http://wicket.apache.org">
    <div wicket:id="repeaterPanel" />       
</wicket:panel>

但我在MultiButtonColumn类中使用MarkUp时遇到了麻烦。

Last cause: Failed to find markup file associated. ButtonPanel: [ButtonPanel [Component id = 1]]

Root cause:

org.apache.wicket.markup.MarkupNotFoundException: Failed to find markup file associated. ButtonPanel: [ButtonPanel [Component id = 1]]
     at org.apache.wicket.markup.html.panel.AssociatedMarkupSourcingStrategy.getMarkup(AssociatedMarkupSourcingStrategy.java:97)
     at org.apache.wicket.MarkupContainer.getMarkup(MarkupContainer.java:451)
     at org.apache.wicket.Component.getMarkup(Component.java:743)
     at org.apache.wicket.Component.getMarkupTag(Component.java:1389)
     at org.apache.wicket.Component.getMarkupIdFromMarkup(Component.java:752)
                      .......

我已经实现了类MultiLabelColumn。目的是相同的,在一行中渲染更多的Label。这门课很好用。

MultiLabelColumn.java和MultiButtonColumn.java之间的区别在于MultiButtonColumn包含2个面板(Button包含在Panel中,Panel包含在Repeater面板中),而MultiLabelColumn只包含1个面板(标签包含在RepeaterPanel中)。

1 个答案:

答案 0 :(得分:1)

您收到该错误,因为您没有关联的标记文件。您需要创建ButtonPanel.html

但我建议您只使用ListView而不是创建RepeaterPanel,而且已经拥有了您想要的功能。