使用Wicket向html元素添加属性

时间:2017-05-17 19:22:36

标签: html wicket

我需要将属性 display-name 添加到<td>,其值为wicket id。

<tbody>
  <tr wicket:id="result" class="bgalternate1">
    <td wicket:id="values">
    <span wicket:id="value">Value</span>
    </td>
  </tr>
</tbody>

预期结果是

<tbody>
  <tr class="bgalternate1">
    <td display-name="<attribute_name>"> <!--attribute is column header -->
    <span wicket:id="value">Value</span>
    </td>
  </tr>
</tbody>

我尝试使用下面的java代码,但是无法实现所需的结果,即无法追加属性&#34; display-name&#34;到<td>。我最初使用的是SimpleAttributeModifier,因为它已被弃用,使用的是AttributeModifier。

public ReportResultPanel(final String id, final IModel<AdHocReportSetup> model)
{
super(id, model);
setOutputMarkupId(true);

final IModel<Paging> pagingModel = new Model<Paging>(new Paging());
resultModel = createResultModel(model, pagingModel);
addResults(this, "result", resultModel);
}

private ListView<AdHocReportResult> addResults(final MarkupContainer parent, 
final String id,
        final IModel<ReportResultModel> model)
{
    final IModel<List<AdHocReportResult>> resultsModel = new PropertyModel<List<AdHocReportResult>>(model, "results");
    final ListView<AdHocReportResult> result = new ListView<AdHocReportResult>(id, resultsModel) {

    private static final long serialVersionUID = 1L;

    @Override
        protected void populateItem(final ListItem<AdHocReportResult> item)
        {
            addResultValues(item, "values", item.getModel());
            AdHocPage.applyParityClass(item.getIndex(), item);

            final Behavior behavior = AttributeModifier.append("display-name", "red");
            for (final Component next : item)
            {
                next.add(behavior);
            }
        }
    };
    parent.add(result);
    return result;

}

private ListView<AdHocReportResultAttribute> addResultValues(final 
MarkupContainer parent, final String id,
        final IModel<AdHocReportResult> model)
{
final IModel<List<AdHocReportResultAttribute>> attributesModel = new PropertyModel<List<AdHocReportResultAttribute>>(
            model, "attributes");

    final ListView<AdHocReportResultAttribute> result = new ListView<AdHocReportResultAttribute>(id, attributesModel) {
        private static final long serialVersionUID = 1L;

        @Override
        protected void populateItem(final ListItem<AdHocReportResultAttribute> item)
        {
            addValueLabel(item, "value", item.getModel());
        }
    };
    parent.add(result);
    return result;

}

1 个答案:

答案 0 :(得分:3)

您要将AttributeModifier添加到ListView,但您确实需要将其添加到ListItem

...
item.add(AttributeModifier.append("display-name", "red"));
addValueLabel(item, "value", item.getModel());
...