CurrencyField conditional color

Hi,

How can I make the CurrencyField red for negative values and green for positive values?

Thanks,
Samy

Hello!

You can change style name of CurrencyField in ValueChangeEvent like this:

@Subscribe("currencyField")
protected void onCurrencyFieldValueChange(final HasValue.ValueChangeEvent<Integer> event) {
    currencyField.removeStyleName("red-text");
    currencyField.removeStyleName("green-text");

    Integer value = event.getValue();
    if (value != null && value > 0) {
        currencyField.addStyleName("green-text");
    }
    if (value != null && value < 0) {
        currencyField.addStyleName("red-text");
    }
}

Styles for color:

.jmix-currencyfield.green-text .v-textfield {
  color: green;
}

.jmix-currencyfield.red-text .v-textfield {
  color: red;
}
1 Like