4

赤、黄、緑のステータスを表示する必要がある列を持つ DataTable を持つ Apache Wicket ページがあります。列の内容が赤の場合、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;
        }
    }));
}

セルに動的 CSS を含むテーブル

4

1 に答える 1

4

まず最初に、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)));
    }
...
}

列のラベルとして作成される内部コンポーネントを変更する必要があります。

最初の方法: 独自のコンポーネントを作成します (ここに AttributeModifier を追加する代わりに、独自の作成メカニズム css クラスまたはスタイルをコンポーネントに含めることもできます):

@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";
    }



}));

}

注:メソッド「createLabelModel」は、代わ​​りに「getDataModel」を使用するのではなく、Wicket 6 で廃止されました。

于 2013-11-09T12:49:28.337 に答える