简体   繁体   English

如何将动态表嵌套在检票口中?

[英]How do I nest dynamic tables in wicket?

I'm using wicket to display a list of tables. 我正在使用检票口来显示表格列表。 The number of tables, and the number of rows in each table, are both dynamic. 表的数量以及每个表中的行数都是动态的。

I'm trying to keep it simple and use a RepeatingView for each, and nest them, but wicket says: 我试图保持简单,并为每个对象使用RepeatingView并将其嵌套,但是检票口说:

java.lang.IllegalArgumentException: A child with id 'table' already exists java.lang.IllegalArgumentException:标识为“ table”的孩子已经存在

Here is my Java: 这是我的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);

And my html is : 而我的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/>

I concede that I'm probably going about this the wrong way, but I can't see what the right way should be. 我承认我可能会以错误的方式进行操作,但是我看不出正确的方法应该是什么。

Hope someone can help!? 希望有人可以帮助!?

Ok I got it working: 好的,我可以正常工作:

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

And the html looks as follows: 并且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/>

You can't add two components with the same ID ( ListView("table") ) to a single container ( RepeatingView ). 您不能将具有相同ID( ListView("table") )的两个组件添加到单个容器( RepeatingView )。

The RepeatingView object has a method that generates IDs for its child components. RepeatingView对象具有一种为其子组件生成ID的方法。 From the javadoc : 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);

You can also create your own logic to generate IDs: 您还可以创建自己的逻辑来生成ID:

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

You're adding the same object multiple times in your loop, which isn't what you're trying to achieve. 您要在循环中多次添加同一对象,而这并不是您要实现的目标。 A named component can only be added once to a given page. 命名组件只能添加到给定页面一次。

You need to define your class as an extension of ListView and pass the list of objects to be rendered to it. 您需要将您的类定义为ListView的扩展,并将要呈现的对象列表传递给它。 Take a look here https://cwiki.apache.org/WICKET/listview-and-other-repeaters.html 在这里看看https://cwiki.apache.org/WICKET/listview-and-other-repeaters.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM