开发者

Changing how the attributes are presented in Wicket Datatable

开发者 https://www.devze.com 2023-04-09 04:02 出处:网络
I\'m developing a new application in Wicket and have run into a small problem. I\'m using a Wicket DataTable, but I want some of the attributes in the dable to be presented different from their \"act

I'm developing a new application in Wicket and have run into a small problem.

I'm using a Wicket DataTable, but I want some of the attributes in the dable to be presented different from their "actual" values. For example, I have a Date that is presented as "2011-09-01 00:00", but I want it to be presented as "2011-09-01". How do I do that?

I don't really want to change to POJO or the Date object (maybe override something, somewhere?)开发者_运维技巧.

Thanks in advance!

Olle


You could create a custom IColumn implementation, that formats the value:

class FormatedPropertyColumn<T> extends PropertyColumn<T> {

    private final Format format;

    public FormatedPropertyColumn(IModel<String> displayModel, String sortProperty, String propertyExpression, Format format) {
        super(displayModel, sortProperty, propertyExpression);
        this.format = format;
    }

    public FormatedPropertyColumn(IModel<String> displayModel, String propertyExpression, Format format) {
        super(displayModel, propertyExpression);
        this.format = format;
    }

    @Override
    protected IModel<?> createLabelModel(IModel<T> rowModel) {
        final IModel<?> originalModel = super.createLabelModel(rowModel);
        return new AbstractReadOnlyModel<String>() {
            @Override
            public String getObject() {
                Object value = originalModel.getObject();
                return (value != null) ? format.format(value) : null;
            }
        };
    }
}

Then you pass the desired format when you instantiate it.

List<IColumn> columns = Arrays.asList(
    new FormatedPropertyColumn<POJO>(Model.of("Date"), "date", new SimpleDateFormat("yyyy-MM-dd"))
);


By using a Converter configured in your application you will be able to format date the way you want for example.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号