【问题标题】:Wicket add other-id on labelWicket 在标签上添加其他 ID
【发布时间】:2020-11-19 07:56:05
【问题描述】:

我是 wicket 的新手,我想向标签添加一个属性(名为 other-id)。代码如下:

private String getName(ExampleList list) {
    return getBrandName() + " " + getString("ExampleList." + list.name());
}  

IModel<List<ExampleList>> listModel = new AbstractReadOnlyModel<List<ExampleList>>() {
            @Override
            public List<ExampleList> getObject() {
                return availableTechnologies.getObject().keySet().stream().collect(Collectors.toList());
            }
        };


ListView<ExampleList> example = new ListView<ExampleList>("list", listModel) {
            @Override
            protected void populateItem(ListItem<ExampleList> el) {
                el.add(new Radio<ExampleList>("radio", item.getModel()));
                el.add(new Label("labelName", getName(el.getModelObject())));               
            }
        };

这是对应的html:

<li wicket:id="list" class="radio">
      <input wicket:id="radio" type="radio" name="radioChoice"/>
      <label wicket:for="radio"><wicket:container wicket:id="labelName" /></label>
</li>

我想将 other-id 添加到带有标签名称的标签到 other-id 中。直到现在我都试过了

el.add(new Label("labelName", getName(el.getModelObject())).add(new AttributeAppender("other-id", getName(el.getModel()))); 

但我的标签中没有出现任何内容。 我也在html中尝试过:

<label wicket:for="radio" other-id= wicket:id="labelName">

但在我最后一次尝试时它给了我错误。 关于如何做到这一点的任何想法?

【问题讨论】:

    标签: html attributes label wicket


    【解决方案1】:

    要在表单组件上设置标签,您需要使用setLabel 方法。在您的 populateItem 方法中,您可以执行以下操作:

    Radio<ExampleList> radio = new Radio<>("radio", item.getModel())
    radio.setLabel(Model.of(getName(el.getModelObject())));
    
    el.add(radio);
    

    要完成这项工作,您应该修改 HTML 以删除标签内的 wicket:container

    <li wicket:id="list" class="radio">
      <input wicket:id="radio" type="radio" name="radioChoice"/>
      <label wicket:for="radio"></label>
    </li>
    

    【讨论】:

      【解决方案2】:

      问题是您将 AttributeAppender 添加到 wicket:container。这个元素只呈现它的主体,即标签的文本,但标签本身没有被呈现,因此它的属性也没有被呈现。

      您可以将标记更改为:

      <label wicket:for="radio" wicket:id="labelName"></label>
      

      这样,AttributeAppender 会将新属性添加到&lt;label&gt; 元素中。

      【讨论】:

      • 很遗憾,我无法更改 html。我试过了,但它给了我另一个错误,所以我应该用 wicket:container 保持这种格式
      • 出现 Internet 故障消息,而不是包含特定列表的页面
      • 请创建一个演示问题的迷你应用程序并在 GitHub/GitLab/BitBucket/... 上分享它,我们会看看!
      猜你喜欢
      • 2018-11-27
      • 2019-03-29
      • 2021-11-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-09-23
      • 2021-09-09
      • 1970-01-01
      相关资源
      最近更新 更多