ChilBud
ChilBud

Reputation: 195

Adding an attribute to html element using Wicket

I need to add an attribute display-name to <td> with values as wicket id.

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

Expected result is

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

I have tried using below java code, however unable to achieve the desired result, i.e unable to append the attribute "display-name" to <td>. I have initially used SimpleAttributeModifier as it is deprecated, used 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;

}

Upvotes: 0

Views: 1832

Answers (1)

martin-g
martin-g

Reputation: 17533

You are adding the AttributeModifier to the ListView but you really need to add it to its ListItems.

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

Upvotes: 4

Related Questions