简体   繁体   English

格式化程序功能中的“ this”

[英]`this` inside formatter function

Is there a way to use the this operator inside a formatter function? 有没有办法在格式化程序函数中使用this运算符? I mean with this , the reference to my component in which the formatter is used. 我的意思是说, this是对使用格式化程序的组件的引用。 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 ;) 当我想访问属性showIdthis.getProperty("showId)我得到一个异常,这个函数不存在this 。我知道如何绑定this一个事件的功能,但是当函数被调用这个样子,我我不知道如何处理;)

Just bind this to the function using the following syntax: 只需绑定this使用的语法如下功能:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM