我是个新手。我试图使用'getSelected‘和'alter’方法(remove_row)删除多个选定的表行。但是,在下面的代码中,我得到了在Firebug和Chrome中未定义的错误“选择”和"Uncaught :无法读取未定义属性'0‘“。无论我选择哪一行,或者选择多少行都没关系。我仍然会收到错误,不会删除任何行。请问我做错什么了?
$(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();
});这是我的小提琴http://jsfiddle.net/EfhqJ/48/。
谢谢。
发布于 2013-06-27 21:02:00
您的“选择”变量在onClick处理程序的范围内不可见。
尝试移动"var selection =.“进入处理程序函数。就像..。
$('.deletebutton').on('click', function () {
var selection = $edit.handsontable('getSelected');
$edit.handsontable('alter', 'remove_row', selection[0], selection[2]);
});发布于 2013-07-12 06:20:00
目前,getSelected()什么也不返回.
getSelected: function () {
//https://github.com/warpech/jquery-handsontable/issues/44
//cjl if (selection.isSelected()) {
//return [priv.selStart.row(), priv.selStart.col(), priv.selEnd.row(), priv.selEnd.col()];
//}
}这是一个很大的问题,因为手持式引用功能相当多。然而,幸运的是,我们可以使用afterSelectionEnd事件。
afterSelectionEnd (r: Number, c: Number, r2: Number, c2: Number)在选择一个或多个单元格后(鼠标向上)触发回调。参数:r选择起始行c选择起始列r2选择结束行c2选择端列
根据API接口的说法
alter ('remove_row', index: Number, amount: Number (Optional), source: String (Optional))删除给定索引处的行。默认数额等于1
这意味着为了使用alter('remove_row'),我们只需要提供索引。
下面是我为获得所需结果而制作的工作演示。
注:
由于错误,我们需要在afterSelectionEnd event中添加一些逻辑。
JAVASCRIPT:
var myData = [
["", "Kia", "Nissan", "Toyota", "Honda"],
["2008", 10, 11, 12, 13],
["2009", 20, 11, 14, 13],
["2010", 30, 15, 12, 13]
];
//declare row vars
var row1 = null,
row2 = null;
var $container = $("#exampleGrid");
$container.handsontable({
data: myData,
startRows: 5,
startCols: 5,
minSpareCols: 0,
minSpareRows: 0,
rowHeaders: true,
colHeaders: true,
contextMenu: true,
afterSelectionEnd: function(x1, y1, x2, y2){
//add this because of bug
if( (x1 <= x2 && y1 < y2) || (x1 < x2 && y1 <= y2) || (x1 == x2 && y1 == y2)) {
row1 = x1;
if(x1 == 0)
row2 = parseInt(x2 + 1);
else
row2 = x2;
}
else if( (x1 >= x2 && y1 > y2) || (x1 > x2 && y1 >= y2)) {
row1 = x2;
if(x2 == 0)
row2 = parseInt(x1 + 1);
else
row2 = x1;
}
else if(x1 < x2 && y1 > y2) {
row1 = x1;
row2 = x2;
}
else if(x1 > x2 && y1 < y2) {
row1 = x2;
row2 = x1;
}
}
});
//gets instance of handsontable
var instance = $container.handsontable('getInstance');
$('#delete').click(function(){
if(row1 != null){
if(row2 != null || row2 != row1 ){
instance.alter('remove_row', row1, row2);
}
else{
instance.alter('remove_row', row1);
}
row1 = null;
row2 = null;
}else{
alert('Please select a cell...');
}
});希望这能帮上忙,如果你还需要什么,请告诉我!
发布于 2016-09-16 14:12:15
只需将outsideClickDeselects: false添加到构造函数选项中,就可以得到getSelected()的正确结果。
这是现场演示http://jsfiddle.net/czz82kL5/2/
https://stackoverflow.com/questions/17092896
复制相似问题