ちょっとしたことですが、はまったのでメモを共有します。
有用な人は少ないと思いますが、2時間前の自分は助かったと思います。
超便利ライブラリhandsontableにて、afterChangeコールバックなどで受け取れるchangesパラメータのindexは、見た目上のindexであって実データのindexではありません。
そのため、ソートした後に、afterChangeでindexをみて、実データに追記する等の場合、ずれてしまいます。
バージョン2まではsortIndexメソッドで近いことができていたようですが3.0.0からremoveされています(参考:https://handsontable.com/blog/articles/2018/5/handsontable-3-0-0-is-now-available)
対策としてtoPhysicalRowメソッドを使うとchangesのindexから実データのindexに変換できます。
afterChangeコールバックの関数例です。
afterChange = function(changes, source) {
// changesは [[row, prop, oldVal, newVal], ...]
if(changes){
var target_row = "";
for(var i = 0; i < changes.length; i++){
target_row = hot.toPhysicalRow(changes[i][0]);
original_items[target_row].someColumne = someMessage;
}
hot.render();
}
}
反対にtoVisualRow
メソッドもありますね。
公式ドキュメント)
https://handsontable.com/docs/6.2.2/Core.html#toPhysicalRow
handsontable便利なので使い方などノウハウ共有したいです。