0

我发现 wicket 的 RadioGroup 完全令人困惑。我在转发器中有 n 个实体,我想在其中设置字段“布尔值”。所以这是我的代码:

RadioGroup radioGroup = new RadioGroup<>("someGroup", new Model(entityXYZ));
radioValue = new Radio("radioValue", 
    new PropertyModel(entityXYZ, "booleanValue"), radioGroup);
repeaterContainer.add(radioValue);
// add other stuff to repeater

我发现的所有例子似乎都不适用。我不想要 radioGroup 中的单个实体,但我希望只允许其中一个实体设置它的字段。我尝试了各种模型组合,但都不起作用。

更新:这似乎是组件层次结构的问题。我无法将 radioValues 添加到同一层次结构中,因为在转发器中可以添加自定义用户输入以及其他 RadioGroups。此外,该组不是由单个对象组成,而是由许多对象组成,其中只有一个对象应具有布尔值集。在 HTML 中这没问题,但我在 Wicket 中看不到任何解决此问题的方法 :(

4

2 回答 2

0

How a radiogroup works is basically you add the radio's for every choice to the radiogroup. Then when one of those radio's is selected, the model object for the group changes to the model object from the selected radio.

See also http://www.wicket-library.com/wicket-examples/compref/wicket/bookmarkable/org.apache.wicket.examples.source.SourcesPage;jsessionid=13F0ADB2C785F4A9A1C04519A050A37A?0&SourcesPage_class=org.apache.wicket.examples.compref.Index&source=RadioGroupPage.java

In essence:

RadioGroup<SomeEntity> group = new RadioGroup<SomeEntity>("somegroup", new Model<Entity>(null));

group.add(new Radio("choice1", new Model<SomeEntity>(someEntityA));
group.add(new Radio("choice2", new Model<SomeEntity>(someEntityB));

form.add(group);

Then in the form submit you could do:

SomeEntity selectedEntity = group.getModelObject();
于 2013-08-14T06:32:44.133 回答
0

您想更新一组实体的属性吗?

RadioGroup<Entity> group = new RadioGroup<Entity>("someGroup", new IModel<Entity>() {
  public void setObject(Entity entity) {
    for (Entity candidate : entities) {
      candidate.setBooleanValue(candidate == entity);
    }
  }
  public Entity getObject() {
    for (Entity candidate : entities) {
      if (candidate.getBooleanValue()) {
        return candidate;
      }
    }
    return null;
  }
});

group.add(new ListView("entities", entities) {
  protected void populateItem(final ListItem<Entity> item)
    item.add(new Radio("radio", item.getModel());
  }
});
于 2013-08-11T18:11:19.723 回答