DataGrid column renderer: get property value from another column

Hey all,

what I try to do is creating an (editable) column, that is bound to a property, render its value with a prefix found in another column (another property of the entity) when not in editable mode.

To be a bit more concrete. It’s an editable datagrid, the entity has a “currency” and an “amount” property. I want the “amount” column render like “EUR 123” when it is currently not edited.

The problem is, in the presentation function I don’t have access to other entity properties:

dataGrid.getColumnNN("amount")
    .setRenderer(dataGrid.createRenderer(DataGrid.TextRenderer.class), value -> {
            // No access to entity here .. :(
            return value;
        });

Add a non-persistent attribute to the entity, place the required code to the setter and getter and use it in the DataGrid or on the form.

Hi

Not sure a Renderer is the solution here. You actually don’t really want to render but rather build a value from two attributes.

I would suggest a custom column generator. In which you have access to the whole entity.

Renderer is more used for cosmetic stuff.

Regards
Michael

@Rimas Thanks for the direction. Until now I tried some things, but still no luck getting the whole thing correctly handled. Struggling with the editable part of the cell, but I still have some ideas that I can try based on your suggestion.

@michael.renaud Actually in this context, it is more a cosmetic task. Like you suggest, I was thinking of combining the currency and the amount into an embeddable entity CurrencyAmount, but I have got three “amounts” (base, vat, total) and it doesn’t feel right to store a currency for all three amounts, when only one is required. The column generator would be a good solution, but generated columns can’t be edited directly.

If you’re sure that generated column cannot be edited, you might try the following, which is along the way of what I think @rimas was proposing.

I might stand corrected on that, did not try it myself.

class CurrencyAmount {
  String isoCode;
  double amount;
  public CurrencyAmount(isoCode, amount) {
   [...]
  }
}

class YourClassWithAmounts {

  String ccyCode;
  double amount;

  [...]

  @MetaProperty("amountWithCcy)"
  public CurrencyAmount getAmountWithCurrency() {
      return new CurrencyAmount(ccyCode, amount);
  }

  @MetaProperty("amountWithCcy)"
  public void setAmountWithCurrency(CurrencyAmount amountWithCcy) {
      setCcyCode(amountWithCcy.getIsoCode());
      setAmount(amountWithCcy.getAmount());
  }

}

This should produce a read/write metaproperty ‘amountWithCcy’ bindable as any other entity attribute.

Then in your renderer you can build e.g a CurrencyField

Michael

}

1 Like

@michael.renaud Thanks for your input. I solved it otherwise now, by changing the requirement. That’s a good thing in this case, because the requirement wasn’t good as it was. Thank you anyways, I still got smarter with this thread and your answer. :slight_smile: