membersound
membersound

Reputation: 86807

How to format table values in vaadin?

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

Answers (3)

Roland Krüger
Roland Krüger

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:

  • It is not typesafe. Since you only get a Property<?> as a parameter you first have to check for the concrete type of the property value.
  • You have to inherit from Table only to adapt the formatting of one or more columns. Class inheritance is definitely the wrong approach for adapting a class's behavior for one particular use case. If you have more than one such cases, you'll end up implementing a bunch of Table subclasses which later can't be easily interchanged.
  • You hard-wire conversion code (BigDecimal to String) to the concrete implementation of some UI component. That's bad for re-use. What if you need that conversion in some other place, say when you display a BigDecimal on a Label? You'd have to duplicate this code or somehow extract it into a separate class or method.

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

Andr&#233; Stannek
Andr&#233; Stannek

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

hmjd
hmjd

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

Related Questions