设置类方法的回调

时间:2018-11-08 10:32:42

标签: javascript slickgrid

对于Slickgrid,

通常,您可以在dateFormatter变量中像这样设置回调columns

var columns = [
   {id: "finish", name: "Finish", field: "finish", 
    formatter: dateFormatter, // path callback name to table 
    sortable: true }
  ];

function dateFormatter(row, cell, value, columnDef, dataContext) {
    return value.getMonth() + '/' + value.getDate() + '/' + value.getFullYear();
}

现在,我制作了包含与处理表有关的成员和方法的类。

然后,我想在类方法中捕获dateFormatter回调。如何设置回调?

class Table{
   constructor(){
      this.columns = [
   { id: "finish", name: "Finish", 
     field: "finish", 
     formatter: `this.dateFormatter`}, // it doesn’t work
  ];
      this.data = new Array();
      this.dataView = new Data.DataView();
      this.grid; 
   }
   makeGrid(gridName){
      this.grid = new Grid(gridName,
      this.dataView, 
      this.columns,
      this.options);
   }
   dateFormatter(row, cell, value, columnDef, dataContext) { // want to catch here.
        return value.getMonth() + '/' + value.getDate() + '/' + value.getFullYear();
   }
}    

2 个答案:

答案 0 :(得分:1)

您正在尝试将函数引用作为模板文字字符串传递。

更改

formatter: `this.dateFormatter`

收件人

formatter: this.dateFormatter

答案 1 :(得分:1)

在您的构造函数中:

this.columns = [
   { id: "finish", name: "Finish", 
     field: "finish", 
     formatter: this.dateFormatter} // no need for back-ticks
  ];

不需要back ticks中的this.dateFormatter,它将转换为字符串。

相关问题