# Converter with parameter

81 2 3

How to parse additional parameters to a converter attribute? Per default a new TypeConverter instance is created.

Is it possible to call the method coerceToUi with further parameters or is it possible to use a pre-instantiated class?

What is the optimal approach for this requirement?

delete retag edit

robertpic71
1275 1

First, there is no way to extend the TypeConverter directly.

Here are my work-a-rounds:

1. use the object instead of the object.attribute
my real world example:
It want to show "customer.sales" but the sales should have another colour when the is > 10 % higher than the last years sales.
The get the whole information i need the customer so i use

<rows>
<row self="@{each=customer}">
<label value="@{customer.number}"/>
..
<label value="@{customer, converter='com.at.od.converters.sales'}"/>
...


Now i get the customer inside my TypeConverter and have no problem to evaluate the color.

2. use custom-attributes to communicate with the TypeConverter
See this side for some examples with "static" parameters.

I should be possible to setup i.e. a converterprogram in the attribute.
<label value="@{customer, converter='com.at.od.converters.spring'}">
<custom-attributes convert="\${springconverter}"/></label>
I've not tested this, but it should work.

Note that the timing for all kinds of listbinding (listbox, rows, combobox...) is important. The single-row/item is used as template for the databinding.
If you need the attribute in all items you have to set this before the binder renders and copy the template.

3. use databinding for custom-attributes
Since ZK 5RC it is possible to use databinding for custom-attributus. So you could pass all your parameters inside custom-attributes and the last one render the item.

/Robert

[hide preview]