【问题标题】:How do I nest dynamic tables in wicket?如何在检票口中嵌套动态表?
【发布时间】:2013-01-07 11:40:01
【问题描述】:

我正在使用 wicket 来显示表格列表。表的数量和每个表中的行数都是动态的。

我试图保持简单,并为每个使用 RepeatingView,并将它们嵌套,但检票口说:

java.lang.IllegalArgumentException: id 为“table”的孩子已经存在

这是我的 Java:

   RepeatingView view = new RepeatingView("listoftables");

    for (CCSubscription sub: getCustomer().getSubscriptions())  {

        //resource balance table
        ResourceBalance[] resBals = sub.getResourceBalances();
        if(resBals!=null && resBals.length>0)   {
        ListView<ResourceBalance> resBalTable = new ListView<ResourceBalance>("table", Arrays.asList(resBals)) {

            @Override
            protected void populateItem(ListItem<ResourceBalance> item) {
                ResourceBalance bal = item.getModelObject();
                item.add(new Label("resource", bal.getResource().getName()));
                item.add(new Label("balance", bal.getBalance()));
            }
        };
        view.add(resBalTable);
        }   //end if sub has non-null resource balance

    }   //end loop through subscriptions

    add(view);

我的 html 是:

<ul>
<li wicket:id="listoftables">
<fieldset>
<legend><b>Resource Balances</b>
</legend>
    <table class="dataview" style="width:50%">
        <thead>
            <tr class="headers">
                <th>Resource</th>
                <th>Balance</th>

            </tr>
        </thead>
        <tr wicket:id="table">
            <td>
                <span wicket:id="resource"></span>
            </td>
            <td>
                <span wicket:id="balance"></span>
            </td>
        </tr>
    </table>
</fieldset>
<br/>

我承认我可能会以错误的方式解决这个问题,但我看不出正确的方式应该是什么。

希望有人能帮忙!?

【问题讨论】:

    标签: java listview html-table wicket


    【解决方案1】:

    好的,我得到了它的工作:

    final ListView<CCSubscription> tables = new ListView<CCSubscription>(
                "tables", getCustomer().getSubscriptions()) {
            @Override
            protected void populateItem(ListItem<CCSubscription> item) {
                ListView<ResourceBalance> resBalTable = new ListView<ResourceBalance>("listview", Arrays.asList(item.getModel().getObject().getResourceBalances())) {
    
                    @Override
                    protected void populateItem(ListItem<ResourceBalance> item) {
                        ResourceBalance bal = item.getModelObject();
                        item.add(new Label("resource", bal.getResource().getName()));
                        item.add(new Label("balance", bal.getBalance()+""));
                    }
                };
                item.add(resBalTable);
            }
        };
    
        add(tables);
    

    html 如下所示:

    <span wicket:id="tables">
    <fieldset>
    <legend><b>Resource Balances</b>
    </legend>
        <table class="dataview" style="width:50%">
            <thead>
                <tr class="headers">
                    <th>Resource</th>
                    <th>Balance</th>
    
                </tr>
            </thead>
            <tr wicket:id="listview">
                <td>
                    <span wicket:id="resource"></span>
                </td>
                <td>
                    <span wicket:id="balance"></span>
                </td>
    
    
            </tr>
        </table>
    </fieldset>
    <br/>
    

    【讨论】:

    • 也可以放上
    【解决方案2】:

    您不能将具有相同 ID (ListView("table")) 的两个组件添加到单个容器 (RepeatingView)。

    RepeatingView 对象有一个为其子组件生成 ID 的方法。来自javadoc

    RepeatingView view = new RepeatingView("repeater");
    view.add(new Label(view.newChildId(), "hello"));
    view.add(new Label(view.newChildId(), "goodbye"));
    view.add(new Label(view.newChildId(), "good morning"));
    add(view);
    

    您还可以创建自己的逻辑来生成 ID:

    RepeatingView view = new RepeatingView("repeater");
    for (int i=0; i < 10; i++)
        view.add(new MyListView("list" + i));
    add(view);
    

    【讨论】:

      【解决方案3】:

      您在循环中多次添加相同的对象,这不是您想要实现的目标。一个命名组件只能添加到给定页面一次。

      您需要将您的类定义为 ListView 的扩展,并将要呈现的对象列表传递给它。看这里https://cwiki.apache.org/WICKET/listview-and-other-repeaters.html

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-04-11
        • 1970-01-01
        • 2016-03-12
        • 2012-10-17
        • 2010-12-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多