Search code examples
javascriptslickgrid

Set callback for class method


For Slickgrid,

Normally you can set callback dateFormatter like this in columns variables.

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();
}

Now I made the class which contained the member and method related to handling table.

Then, I want to catch dateFormatter callback in class method. How can set callback??

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();
   }
}    

Solution

  • You are trying to pass a function reference as a template literal string.

    Change

    formatter: `this.dateFormatter`
    

    To

    formatter: this.dateFormatter