【问题标题】:How to repeat Button in RepeatingView如何在RepeatingView中重复按钮
【发布时间】:2014-07-23 13:20:53
【问题描述】:

如何将 Button 添加到 RepeatingView? Label 等其他组件运行良好,但 Button 无法正常呈现。

我的代码:

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

编辑:

我决定创建一个可以在一行中呈现更多按钮的自定义列。 我读到该按钮必须包裹在面板中才能正确查看。 假设自定义列的名称是MultiButtonColumn。

原理如下:从对象T中获取列表L,为L的item中的每个属性P创建按钮。文字按钮为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 Panel 中),而 MultiLabelColumn 仅包含 1 个面板(Label 包裹在 RepeaterPanel 中)。

【问题讨论】:

  • 您可以为这段代码添加 html 吗?
  • RepeatingView 是 DataTable 的一部分,由于渲染行多于一行,所以专门放在 Cell 中。

标签: button wicket repeat


【解决方案1】:

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

但我建议不要创建您的 RepeaterPanel,而是使用 ListView,它已经具有您想要的功能。

【讨论】:

  • 按钮正文中添加文字
猜你喜欢
  • 1970-01-01
  • 2017-02-07
  • 2017-03-25
  • 2020-11-15
  • 2016-03-02
  • 1970-01-01
  • 2017-02-15
  • 2018-09-09
  • 2021-01-06
相关资源
最近更新 更多