Changing how the attributes are presented in Wicket Datatable
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!
OlleYou 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.
精彩评论