-1

Is there a way to use the this operator inside a formatter function? I mean with this, the reference to my component in which the formatter is used. For example, I got this code:

metadata: {
  properties: {
    // ...
    showId : { type : "boolean", defaultValue : true },
    // ...
  }
}
//Some view stuff ...

columns: [
  new sap.ui.table.Column({
    label: "Beschreibung ( ID )",
    filterProperty: "SHORT_TEXT",
    template: new sap.m.Text().bindProperty("text", {
      parts: [/*...*/],
      formatter: function(text, id) {
        if (text != null && id != null) {
          if(this.getProperty("showId)){
            return text + " ( " + id + " )";
          } else {
            return text;
          }
        }
        return "";
      }
    }),
  })
]

When I want to access the property showId with this.getProperty("showId) I get an exception, that this function not exists for this. I know how to bind this to a event function, but when the function is called like this, I've got no idea, how to handle this ;)

Boghyon Hoffmann
  • 17,103
  • 12
  • 72
  • 170
Chris
  • 599
  • 3
  • 11
  • 24

1 Answers1

2

Just bind this to the function using the following syntax:

formatter : function(text, id) {
    if (text != null && id != null) {
        if(this.getProperty("showId)){
            return text + " ( " + id + " )";
        }else{
            return text;
        }
    }
    return "";
}.bind(this)
nistv4n
  • 2,515
  • 2
  • 19
  • 21