Excel の Window枠の固定のように、Handsontable では、列インデックスを指定して
指定インデックスより左側を固定にするオプション fixedColumnsLeft がある。
現在選択しているセルに対して、この fixedColumnsLeft の値をセットするように
コンテキストメニューを作れば、便利である。当然、解除も用意する。
コンテキストメニューが実行する callback が、以下の引数で呼ぶことを考慮すれば良い
key | contextmenu の items で定義したキー名称 |
selection | focusしているセルの開始から終わりまでの row と col |
clickEvent | マウスイベント |
selection は、
selection[0].start.row と selection[0].start.col で、選択開始セルの行と列番号
selection[0].end.row と selection[0].end.col で選択終了セルの行と列番号
初期表示は、fixedColumnsLeft = 0 であることを利用して、以下のようにする。
contextMenu: { items:{ 'row_above': { name: '1行挿入', }, 'remove_row': { name: '行削除', disabled: function(){ return hot.countRows() < 2; } }, "hsep": "---------", 'undo': { name: '戻る' }, 'fixcolumn' : { name: function(){ if ( hot.getSettings().fixedColumnsLeft==0){ return "列固定"; }else{ return "列固定解除"; } }, callback: function(key, selection, clickEvent){ if ( hot.getSettings().fixedColumnsLeft==0){ hot.getSettings().fixedColumnsLeft = selection[0].start.col; }else{ hot.getSettings().fixedColumnsLeft = 0; } hot.render(); }, }, }, },
列固定後は、コンテキストメニュー表示が「列固定解除」になるように制御する。