如何使用Angular 6 + Handsontable,仅基于另一个像元值​​更改一个像元值​​?

时间:2019-08-20 09:12:08

标签: angular6 cell handsontable

当我使用afterChange钩子(Angular 6 + Handsontable)并在其中使用hot.getDataAtCell(...)时,行中的所有单元格都会更改

我尝试使用不同的钩子(beforeChange,afterChange),但没有用

      if (changes && source) {
        this.newCellCol = source[0][0];
        this.newCellRow = source[0][1] + 1;
        this.newCellData = this.tempId.getDataAtCell(source[0][0], source[0][1]);
        console.log(this.newCellData);
      }
    },
    afterChange: (changes, source) => {
      if (changes && source) {
        this.tempId.setDataAtCell(this.newCellCol, this.newCellRow, this.newCellData);
      }
    }
  };

我想也将值添加到它旁边的单元格中 但它被添加到同一行的所有单元格中 最终的解决方案是,根据一个单元格中输入的数据,将不同的计算数据插入同一行中的3个不同的单元格中

1 个答案:

答案 0 :(得分:1)

在afterChange挂钩的changes数组中,您可以访问行,prop(列),oldValue和newValue。您可以通过上述内容在确切的列中侦听更改,并且可以创建一个将被调用的自定义方法,该方法将根据以上内容触发更改到您的目标单元格中​​。

 afterChanges: (changes, source) => {
     //iterate through changes
     row = changes[iterator][0]
     col = changes[iterator][1]
     oldValue = changes[iterator][2]
     newValue = changes[iterator][3]

     //listen for changes in the desired columns
     //invoke the custom method or add a block to set Data for the target cell(s)
 }

此外,作为最佳实践,建议使用setDataAtRowProp代替setDataAtCell,因为前者具有一次为多个单元格设置数据的灵活性。

供参考:https://handsontable.com/docs/7.2.2/Hooks.html#event:afterChange                 https://handsontable.com/docs/7.2.2/Core.html#setDataAtRowProp