LoginSignup
0
0

More than 5 years have passed since last update.

jsライブラリHandsontableにてソート後に実データにアクセスする方法

Last updated at Posted at 2019-02-21

ちょっとしたことですが、はまったのでメモを共有します。
有用な人は少ないと思いますが、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便利なので使い方などノウハウ共有したいです。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0