如何获取Apache Wicket PropertyColumn数据以根据内容动态更改CSS类?

时间:2013-11-09 02:01:27

标签: css apache properties conditional wicket

我有一个Apache Wicket页面,其中有一个DataTable,其中一列需要显示红色,黄色,绿色等状态。如果列的内容为红色,我想将CSS类更改为红色状态,如果是黄色黄色状态,则为绿色状态。我似乎无法从可点击的属性列中获取数据。你如何获得PropertyColumn中的数据,或者在DataTable中有另一种方法吗?谢谢!

更新

谢谢你,马丁。这就是我想出的:

@Override
public void populateItem(Item<ICellPopulator<T>> cellItem, String componentId, final IModel<T> rowModel) {
    Label label = new Label(componentId, getDataModel(rowModel));
    cellItem.add(label);
    LOGGER.debug("populateItem: label DefaultModelObject: {}", (String) label.getDefaultModelObject());

    label.add(new AttributeModifier("class", new AbstractReadOnlyModel<String>() {
        private static final long serialVersionUID = 1L;

        ProcessingTime  processingTime = (ProcessingTime) rowModel.getObject();
        @Override
        public String getObject() {
            String cssClass = null;
            if (StringUtils.equals("Red", processingTime.getStatus())) {
                cssClass = "red-status";
            } else if (StringUtils.equals("Yellow", processingTime.getStatus())) {
                cssClass = "yellow-status";
            } else if (StringUtils.equals("Green", processingTime.getStatus())) {
                cssClass = "green-status";
            } else {
                cssClass = "process-status";
            }
            return cssClass;
        }
    }));
}

Table with dynamic css in cells

1 个答案:

答案 0 :(得分:3)

首先,看一下PropertyColumn的populateItem,实现如何,在Wicket 6中(与其他版本类似):

public class PropertyColumn<T, S> extends AbstractColumn<T, S> implements IExportableColumn<T, S, Object>
...
    @Override
    public void populateItem(final Item<ICellPopulator<T>> item, final String componentId,
        final IModel<T> rowModel)
    {
        item.add(new Label(componentId, createLabelModel(rowModel)));
    }
...
}

您必须修改作为列标签创建的内部组件。

第一种方法:创建自己的组件(您的组件也可以包含自己的创建css类或样式的机制,而不是在这里添加AttributeModifier):

@Override
public void populateItem(final Item<ICellPopulator<T>> item, final String componentId,
    final IModel<T> rowModel)
    {
    super.populateItem(item, componentId, rowModel);
    MarkupContainer c = item.get(componentId);
    c.add(new AttributeModifier("class", new AbstractReadonlyModel<String>() {

    private static final long serialVersionUID = 1L;

    @Override
    public String getObject() {
        // some logic how to which css you want to apply
        return "MY-CSS-CLASS";
    }



}));

}

或者您可以让Wicket自己创建Label组件,只需添加一个AttributeModifier:

@Override
public void populateItem(final Item<ICellPopulator<T>> item, final String componentId, final IModel<T> rowModel) 
    {
    super.populateItem(item, componentId, rowModel);
    Label l = new Label(componentId, createLabelModel(rowModel));
    item.add(l);
    l.add(new AttributeModifier("class", new AbstractReadonlyModel<String>() {

    private static final long serialVersionUID = 1L;

    @Override
    public String getObject() {
        // some logic how to which css you want to apply
        return "MY-CSS-CLASS";
    }



}));

}

注意:在Wicket 6中弃用了'createLabelModel'方法,而不是使用'getDataModel'。