I'm using handsontable to create some excel-like spreadsheets and I need to retrieve the data selected by the user to create a chart using gRaphael. But when I try to even alert the data selection parameters using this code:
var ht = $('#dataTable0').data('handsontable');
var sel = ht.getSelected();
alert(sel[0]);
I get 'undefined' written in the alert window. Can someone tell me how to fix this code?
I'm using handsontable to create some excel-like spreadsheets and I need to retrieve the data selected by the user to create a chart using gRaphael. But when I try to even alert the data selection parameters using this code:
var ht = $('#dataTable0').data('handsontable');
var sel = ht.getSelected();
alert(sel[0]);
I get 'undefined' written in the alert window. Can someone tell me how to fix this code?
Share Improve this question asked Jun 30, 2013 at 1:30 user2535621user2535621 1571 gold badge1 silver badge7 bronze badges2 Answers
Reset to default 17Your code is obsolete, which may be the reason it doesn't work as intended. The recommended way if you are using the latest version 0.9.7 is:
$('div#example1').handsontable(options);
//get the instance using jQuery wrapper
var ht = $('#example1').handsontable('getInstance');
//Return index of the currently selected cells as an array [startRow, startCol, endRow, endCol]
var sel = ht.getSelected();
//'alert' the index of the starting row of the selection
alert(sel[0]);
If you are using an older version I suggest downloading the latest version.
EDIT:
As suggested by @polras, you can also add:
outsideClickDeselects: false
to handsonetable options
first you need select rows Using ht.selectRows(start, end) or ht.selectAll(), then when you call ht.getSelected() it will return array describes your selection (rows, cells)
finally you can call ht.getData(...ht.getSelected()[0]) this will pass the array of info returned from getSelected as arguments to getData function and will return array of data selected
note: there are many scenarios in selection eg: you can select first to third rows and only first and last cell that's why getSelected return the array of 4 numbers and we pass that array to ht.getData.