0

我在一个表中有 4 列单选按钮。问题:在 Wicket 中,需要将同一组的单选按钮分组在一个 Wicket 标记下,但在 HTML 中不可能这样做,因为表格是逐行声明的。

代码示例

<table id="table1">
    <thead>
        <tr>
            <td>Group 1</td>
            <td>Group 2</td>
            <td>Group 3</td>
            <td>Group 4</td>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td>Value 1</td>
            <td>Value 2</td>
            <td>Value 3</td>
            <td>Value 4</td>
        </tr>
        <tr>
            <td><input type="radio" name="group1" wicket:id="g1b1"><\td>
            <td><input type="radio" name="group2" wicket:id="g2b1"><\td>
            <td><input type="radio" name="group3" wicket:id="g3b1"><\td>
            <td><input type="radio" name="group4" wicket:id="g4b1"><\td>
        </tr>
        <tr>
            <td><input type="radio" name="group1" wicket:id="g1b2"><\td>
            <td><input type="radio" name="group2" wicket:id="g2b2"><\td>
            <td><input type="radio" name="group3" wicket:id="g3b2"><\td>
            <td><input type="radio" name="group4" wicket:id="g4b2"><\td>
        </tr>
        <tr>
            <td><input type="radio" name="group1" wicket:id="g1b3"><\td>
            <td><input type="radio" name="group2" wicket:id="g2b3"><\td>
            <td><input type="radio" name="group3" wicket:id="g3b3"><\td>
            <td><input type="radio" name="group4" wicket:id="g4b3"><\td>
        </tr>
        <tr>
            <td><input type="radio" name="group1" wicket:id="g1b4"><\td>
            <td><input type="radio" name="group2" wicket:id="g2b4"><\td>
            <td><input type="radio" name="group3" wicket:id="g3b4"><\td>
            <td><input type="radio" name="group4" wicket:id="g4b4"><\td>
        </tr>
    </tbody>
</table>

如您所见,这四个组混合在一起,如果我创建的组在 HTML 中不存在,则会引发异常。

如果我声明具有相同检票口 ID 的不同组,则仅考虑我添加的最后一个单选按钮。

谁能帮我吗?

4

2 回答 2

1

用四个嵌套的 RadioGroup 包装 Radio,并将正确的组实例传递给每个 Radio 构造函数:

final RadioGroup<Integer> radioGroup1 = new RadioGroup<Integer>("radioGroup1", new Model<Integer>());
final RadioGroup<Integer> radioGroup2 = new RadioGroup<Integer>("radioGroup2", new Model<Integer>());
final RadioGroup<Integer> radioGroup3 = new RadioGroup<Integer>("radioGroup3", new Model<Integer>());
final RadioGroup<Integer> radioGroup4 = new RadioGroup<Integer>("radioGroup4", new Model<Integer>());

add(new FeedbackPanel("feedback"));
add(new Form<Void>("form")
    .add(radioGroup1
        .add(radioGroup2
            .add(radioGroup3
                .add(radioGroup4
                    .add(new Loop("loop", 10) {
                        @Override
                        protected void populateItem(LoopItem item) {
                            Model<Integer> itemModel = Model.of(item.getIndex());
                            item.add(new Radio<Integer>("radio1", itemModel, radioGroup1));
                            item.add(new Radio<Integer>("radio2", itemModel, radioGroup2));
                            item.add(new Radio<Integer>("radio3", itemModel, radioGroup3));
                            item.add(new Radio<Integer>("radio4", itemModel, radioGroup4));
                            item.add(new Label("label", itemModel));
                        }
                    })))))
    .add(new Button("submit") {
        @Override
        public void onSubmit() {
            info(Strings.join(", ",
                radioGroup1.getModelObject().toString(),
                radioGroup2.getModelObject().toString(),
                radioGroup3.getModelObject().toString(),
                radioGroup4.getModelObject().toString()));
        }
    }));



<div wicket:id="feedback"></div>

<form wicket:id="form">

  <div wicket:id="radioGroup1">
  <div wicket:id="radioGroup2">
  <div wicket:id="radioGroup3">
  <div wicket:id="radioGroup4">

    <table>
      <tr>
        <th>A</th>
        <th>B</th>
        <th>C</th>
        <th>D</th>
      </tr>
      <tr wicket:id="loop">
        <td><input type="radio" wicket:id="radio1"></td>
        <td><input type="radio" wicket:id="radio2"></td>
        <td><input type="radio" wicket:id="radio3"></td>
        <td><input type="radio" wicket:id="radio4"></td>
        <td><span wicket:id="label"></span></td>
      </tr>
    </table>

  </div>
  </div>
  </div>
  </div>

  <button wicket:id="submit" type="submit">Submit</button>

</form>
于 2013-09-20T14:05:44.430 回答
0

我认为您可以使用RadioGroup包装表格的标签(甚至tbody标签)。wicket:container标签不会与 HTML 结构混淆。像这样:

<wicket:container wicket:id="group">
  <table id="table1">
    <!-- The rest of the table markup -->
  </table>
</wicket:container>
于 2013-09-20T11:56:06.347 回答