Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Changing how the attributes are presented in Wicket Datatable


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

like image 339
bumaklion Avatar asked Dec 16 '22 08:12

bumaklion


2 Answers

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"))
);
like image 78
tetsuo Avatar answered Feb 20 '23 01:02

tetsuo


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

like image 44
Cedric Gatay Avatar answered Feb 20 '23 03:02

Cedric Gatay