-->

Handsontable Save formula values

2020-04-27 07:36发布

问题:

I want integrate handsontable to my site, but a got a problem, when im trying to save data created by RuleJS (formula support), script give me a formula back like (=$A1*$B1). Im trying to get result of simple math multiply price and currency. How to save table values, not from sourse?

The workig code is on jsfiddle: http://jsfiddle.net/zetwin/qhjuuk5j/4/

document.addEventListener("DOMContentLoaded", function() {

  var people = [ 
                {base_price :'99', price:'13', unit:'=A1*B1'},
                {base_price :'89.99', price:'13.43', unit:'=A2*B2'}
                ],
example1 = document.getElementById('example1'),
settings1;

  settings1 = {
    data: people,
    formulas: true,
    rowHeaders: true,
    colHeaders: true,
    colHeaders: [
    'Price', 'Currency', 'USD'],
    columns: [
      {data: 'base_price'},
      {data: 'price'},
      {data: 'unit'}
    ]
  };

  var hot = new Handsontable(example1, settings1);

$('.push').click(function () {
    data = hot.getData();
    console.log(data);
    //$('.result').text(data);
});

});

Thank you!

回答1:

An object is a property Handsontable plugin.matrix.data - a list of cells, which are calculated according to the formulas in the table. Each cell has a property value - this is the displayed result. To get all of the data will have to use the function: Handsontable.plugin.helper.cellValue or Handsontable.plugin.helper.cellRangeValue.

For example for your jsfiddle:

to get from the top-right of the cell:

data = hot.plugin.helper.cellValue ('C1');
console.log (data);

48168.89000000001 obtaining values ​​of all cells (a list):

data = hot.plugin.helper.cellRangeValue ('A1', 'C2');
console.log (data);

Array [6]: 0: 625.57 1: 656.95 2: 77 3: 77 4: 48168.89000000001 5: 50585.15 length: 6

I think that is a better method than cellRangeValue not. But on this basis we can write something more comfortable. See. Handsontable.plugin.helper.cellRangeValue