Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use getCellMeta in afterChange at Handsontable?

I'm using handsontable js plugin. I want to use getCellMeta function in afterChange hook but not working. I when use function out afterChange hook, function is working. But not working in afterChange hook.

var container = document.getElementById('t1'),
  options = document.querySelectorAll('.options input'),
  table,
  hot; 

hot = new Handsontable(container, {    
  autoWrapRow: true,
  startRows: 81,
  startCols: 206,
  autoColumnSize : true,  
  stretchH: 'all', 
  afterChange : function(change,source) { 
      if (source === 'loadData') {
        return;
      }   
      var test = this.getCellMeta(change[0],change[1]); // not working, not return "id" meta
      console.log(test);  
  }
});

$.ajax({
  url: 'path',
  type: 'GET',
  dataType: 'json',
  success: function (res) { 
    var data = [], row, pc = 0; 
    for (var i = 0, ilen =  hot.countRows(); i < ilen; i++)
    {
      row = []; 
      for (var ii = 0; ii<hot.countCols(); ii++)
      {   
        hot.setCellMeta(i,ii,'id',res[pc].id);
        row[ii] =   res[pc].price;
        if(pc < (res.length-1)) {

        pc++;
        }
      } 
      data[i] = row;
    }  
    hot.loadData(data);
  }
}); 

var test = this.getCellMeta(0,0); // is working, return "id" meta
console.log(test);  

Output console log i tried out afterChange; enter image description here

Output console log use in afterChange; enter image description here

How to get cell meta after change?

Thanks.

like image 411
Hüseyin ASLIM Avatar asked Dec 25 '17 04:12

Hüseyin ASLIM


People also ask

Why did handsontable introduce a cell type?

If the validator function is not defined, then a cell value is always valid. Manually defining those functions for cells or columns would be tedious, so to simplify the configuration, Handsontable introduced cell types.

What are the associated functions in handsontable?

With every cell in the Handsontable there are 3 associated functions: Each of those functions are responsible for a different cell behavior. You can define them separately or use a cell type to define all three at once. Handsontable does not display the values stored in the data source directly.

How do I return mixed data from handsontable?

Returns a data type defined in the Handsontable settings under the type key ( Options#type ). If there are cells with different types in the selected range, it returns 'mixed'. Note: If data is reordered, sorted or trimmed, the currently visible order will be used. From visual row index. From visual column index. To visual row index.

How do I define multiple values in handsontable?

You can define them separately or use a cell type to define all three at once. Handsontable does not display the values stored in the data source directly.


1 Answers

You're almost there, there's just a small mistake in your callback: the doc for afterChange specifies that first argument (changes) of the callback is:

a 2D array containing information about each of the edited cells [[row, prop, oldVal, newVal], ...].

So, 2 important details:

  • To get the "meta" of row/col of affected cell (assuming there is just one), you need to call hot.getCellMeta(change[0][0],change[0][1]) for example
  • On hot and not this because the afterChange callback function is invoked from a different context (ie on a different object), so this is not the right target for the call, see How does the "this" keyword work?

Example that reads the whole array of changes:

var hot = new Handsontable(container, {
  /* rest of init... */
  afterChange : function(changes,source) {
      console.log("Changes:", changes, source);
      if (changes) {
          changes.forEach(function(change) {
              var test = hot.getCellMeta(change[0],change[1]);
              console.log(test.id, test); // 'id' is the property you've added earlier with setMeta         
          });
      }
  }
});

See demo fiddle, open JS console, make any change(s) in the table.

like image 185
Hugues M. Avatar answered Oct 25 '22 23:10

Hugues M.