`this`里面的格式化函数

时间:2016-06-16 11:16:22

标签: javascript sapui5 this

有没有办法在格式化程序函数中使用this运算符?我的意思是this,我对使用格式化程序的组件的引用。例如,我得到了这段代码:

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 "";
      }
    }),
  })
]

当我想使用showId访问属性this.getProperty("showId)时,我得到一个例外,this不存在此函数。我知道如何将this绑定到事件函数,但是当像这样调用函数时,我不知道如何处理它;)

1 个答案:

答案 0 :(得分:2)

使用以下语法将this绑定到函数:

formatter : function(text, id) {
    if (text != null && id != null) {
        if(this.getProperty("showId)){
            return text + " ( " + id + " )";
        }else{
            return text;
        }
    }
    return "";
}.bind(this)
相关问题