Reputation: 86807
If I have an object with BigDecimal
property, I want to display it in a Table
with a specific formatting: 2 fractions, and a "+" or "-" sign according to the amount.
Eg: +10.50
, -3.20
How can this be achieved in a vaadin table? There is a method table.setConverter(..)
, but this would actually force to convert between eg a BigDecimal.class
and a String.class
. What I'm more after is just a view formatter that just displays the object data differently.
Upvotes: 5
Views: 5596
Reputation: 904
While using Table.formatPropertValue()
for formatting table columns is a viable option, I strongly discourage from using this method when working with Vaadin 7. formatPropertValue()
is the old Vaadin 6 way of formatting Table values. This method is still available in Vaadin 7 for downward compatibility. Using this method is problematic in several ways:
Property<?>
as a parameter you first have to check for the concrete type of the property value.The last point is exactly what Vaadin 7 does for you: keep conversion logic separate from some concrete UI component. This is what the com.vaadin.data.util.converter.Converter
interface is for. So, the OP was quite right in his/her first assumption: Table.setConverter()
is the way to go with Vaadin 7. Converters are typesafe and allow for separation of concerns.
The objection that the Converter which can be set with Table.setConverter()
only converts from BigDecimal to String is not justified in this case. Table.formatPropertValue()
doesn't do anything different - it also converts to String. But this is obvious, a Table doesn't display anything other than String data in its columns. In fact, the default behaviour of Table is to call the toString()
method on Property
value types that it can't convert on its own.
For using Converters, see section 9.2.3 of the Book of Vaadin.
Upvotes: 9
Reputation: 7863
You have to write your own table class extending Table
and override formatPropertyValue(Object rowId, Object colId, Property<?> property)
.
See section 5.15.6 in the book of Vaadin
Upvotes: 0
Reputation: 122001
Override
the protected method Table.formatPropertValue()
:
public class My_table
extends Table
{
@Override
protected String formatPropertyValue(final Object a_row_id,
final Object a_col_id,
final Property<?> a_property)
{
if (a_property.getType() == BigDecimal.class
&& null != a_property.getValue())
{
return "formatted-value";
}
return super.formatPropertyValue(a_row_id, a_col_id, a_property);
}
}
See Book of Vaadin section 5.16.6. Formatting Table Columns.
Upvotes: 7