I am new to Handsontable. I am trying to delete multiple selected table rows using 'getSelected' and 'alter' methods (remove_row). However, with my code below I am getting the error "selection" not defined in Firebug and "Uncaught TypeError: Cannot read property '0' of undefined " in Chrome. It doesn't matter which row I select or how many. I still get the error and no rows are deleted. What am I doing wrong please?
$(document).ready(function () {
var myData = [
["", "Kia", "Nissan", "Toyota", "Honda"],
["2008", 10, 11, 12, 13],
["2009", 20, 11, 14, 13],
["2010", 30, 15, 12, 13]
];
$("#exampleGrid").handsontable({
data: myData,
startRows: 5,
startCols: 5,
//minSpareCols: 1, //always keep at least 1 spare row at the right
//minSpareRows: 1, //always keep at least 1 spare row at the bottom,
rowHeaders: true,
colHeaders: true,
contextMenu: true,
currentRowClassName: 'currentRow',
currentColClassName: 'currentCol'
});
$edit = $('#exampleGrid');
function editRows() {
$('#addtop').on('click', function () {
$edit.handsontable('alter', 'insert_row', 0);
});
$('#addbottom').on('click', function () {
$edit.handsontable('alter', 'insert_row');
});
var selection = $edit.handsontable('getSelected');
$('.deletebutton').on('click', function () {
$edit.handsontable('alter', 'remove_row', selection[0], selection[2]);
});
}
editRows();
});
Here is my fiddle http://jsfiddle.net/EfhqJ/48/.
Thanks.
you just need to add
outsideClickDeselects: false
to your options of constructor, then you will get the correct result of getSelected().and here is the live demo http://jsfiddle.net/czz82kL5/2/
your "selection" variable is not visible within the scope of the onClick handler.
Try moving "var selection = ..." into the handler function. Something like...
As it currently stands,
getSelected()
returns nothing...which is a huge problem since handsontable references that function quite a bit. However, we can fortunately use the afterSelectionEnd event.
According to the API,
This means in order to use
alter('remove_row')
, we only need to provide the index.Here is a working demo I made to get the desired result.
NOTE:
Due to a bug, we need to add some logic in the
afterSelectionEnd event
.JAVASCRIPT:
Hope this helps and let me know if you need anything else!