从行值求和,“2 列”最大可操作值

Sum from row value, "2 colums" the max handsontable

我有一个手动表,我需要在其中对 cols 求和,最后的 col 是所有 cols 的总和 我可以对所有列求和,问题是列是动态的,有时 one-col 有一个 child,在这种情况下,我需要对两者的最大值求和,但我不知道该怎么做, !when is just one 只是加一。如果有一个 child 总和就是两个列的最大值。!

例如:work1 是 parent。 child 是 帮助 1,数组 cols 来自数据库

示例http://jsfiddle.net/duxL728h/

function hexRenderer(instance, td, row, col, prop, value, cellProperties) {
  Handsontable.TextCell.renderer.apply(this, arguments);
}

var hot = new Handsontable($("#exampleGrid")[0], {
  data: [
    [0, 0, 0, 0],
    [0, 0, 0, 0]
  ],
  fillHandle: false,
  minSpareCols: 0,
  minSpareRows: 0,
  columns: [{
      data: 'work.name',
      title: 'Name',
      readOnly: true,
      type: 'numeric',
      width: 100
    }, {
      data: 'work.1',
      perent: "1",
      title: 'work 1',
      type: 'numeric',
      width: 50
    }, {
      data: 'work.2',
      perent: "1|h",
      title: 'help 1',
      type: 'numeric',
      width: 50
    }, {
      data: 'work.3',
      perent: "3",
      title: 'work 2',
      type: 'numeric',
      width: 50
    }, {
      data: 'work.4',
      perent: "3|h",
      title: 'help 2',
      type: 'numeric',
      width: 50
    }, {
      data: 'work.5',
      perent: "5",
      title: 'work 3',
      type: 'numeric',
      width: 50
    }, {
      data: 'work.6',
      perent: "6",
      title: 'work 4',
      type: 'numeric',
      width: 50
    }, {
      data: 'work.7',
      perent: "6|h",
      title: 'help 4',
      type: 'numeric',
      width: 50
    }, {
      data: 'total',
      title: 'Total',
      readOnly: true,
      width: 100,
      type: {
        renderer: function(instance, td, row) {
          let cols = instance.countCols() - 1;
          let total = 0;

          for (let index = 1; index < cols; index++) {
            total += instance.getDataAtCell(row, index);
          }

          td.innerHTML = total;
        }
      }
    }


  ],
  afterChange: function(changes, source) {

    console.log("proces to save data")
  }
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<link rel="stylesheet" type="text/css" href="http://handsontable.com/demo/css/samples.css">
<script type="text/javascript" src="http://handsontable.com/dist/handsontable.full.js"></script>
<link rel="stylesheet" type="text/css" href="http://handsontable.com/dist/handsontable.full.css">

<div id="exampleGrid" class="dataTable"></div>

我通过更改数据键修复了它,以便轻松识别哪个列是父列,哪个列是子列,如下所示。

function hexRenderer(instance, td, row, col, prop, value, cellProperties) {
  Handsontable.TextCell.renderer.apply(this, arguments);
}

var hot = new Handsontable($("#exampleGrid")[0], {
  data: [
    [0, 0, 0, 0],
    [0, 0, 0, 0]
  ],
  fillHandle: false,
  minSpareCols: 0,
  minSpareRows: 0,
  columns: [{
      data: 'work.name',
      title: 'Name',
      readOnly: true,
      type: 'numeric',
      width: 100
    }, {
      data: 'work.1',
      perent: "1",
      title: 'work 1',
      type: 'numeric',
      width: 50
    }, {
      data: 'help.1',
      perent: "1|h",
      title: 'help 1',
      type: 'numeric',
      width: 50
    }, {
      data: 'work.2',
      perent: "3",
      title: 'work 2',
      type: 'numeric',
      width: 50
    }, {
      data: 'help.2',
      perent: "3|h",
      title: 'help 2',
      type: 'numeric',
      width: 50
    }, {
      data: 'work.3',
      perent: "5",
      title: 'work 3',
      type: 'numeric',
      width: 50
    }, {
      data: 'work.4',
      perent: "6",
      title: 'work 4',
      type: 'numeric',
      width: 50
    }, {
      data: 'help.4',
      perent: "6|h",
      title: 'help 4',
      type: 'numeric',
      width: 50
    }, {
      data: 'total',
      title: 'Total',
      readOnly: true,
      width: 100,
      type: {
        renderer: function(instance, td, row) {
          const cols = instance.getCellMetaAtRow(row);
          cols.shift();
          const values = cols.reduce((acc, ele) => {
            const [type, ind] = ele.prop.split('.');
            const value = instance.getDataAtCell(row, ele.col);
            if (value && (!acc[ind] || value > acc[ind])) {
              acc[ind] = value;
            }
            return acc;
          }, {});


          td.innerHTML = Object.values(values).reduce((acc, e1) => acc + e1, 0);
        }
      }
    }


  ],
  afterChange: function(changes, source) {

    // console.log("proces to save data")
  }
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<link rel="stylesheet" type="text/css" href="http://handsontable.com/demo/css/samples.css">
<script type="text/javascript" src="http://handsontable.com/dist/handsontable.full.js"></script>
<link rel="stylesheet" type="text/css" href="http://handsontable.com/dist/handsontable.full.css">

<div id="exampleGrid" class="dataTable"></div>