问题描述
我是Handsontable的新手.我正在尝试使用' getSelected '和' alter '(remove_row)方法删除多个选定的表行.但是,在下面的代码中,我收到了Firebug中未定义的错误选择"和Chrome中的未捕获的TypeError:无法读取未定义的属性'0'".选择哪一行或多少行都没有关系.我仍然收到错误,没有行被删除.请问我做错了什么?
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();
});
这是我的小提琴 http://jsfiddle.net/EfhqJ/48/.
谢谢.
推荐答案
按照目前的情况,getSelected()
不返回任何内容...
As it currently stands, getSelected()
returns nothing...
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()];
//}
}
这是一个很大的问题,因为 handsontable 引用了很多功能.但是,我们可以幸运地使用 afterSelectionEnd事件.
which is a huge problem since handsontable references that function quite a bit. However, we can fortunately use the afterSelectionEnd event.
根据 API ,
这意味着要使用alter('remove_row')
,我们只需要提供索引即可.
This means in order to use alter('remove_row')
, we only need to provide the index.
这是我制作的工作演示.
注意:
由于 bug ,我们需要在afterSelectionEnd event
.
JAVASCRIPT:
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...');
}
});
希望这会有所帮助,让我知道您是否还有其他需求!
Hope this helps and let me know if you need anything else!
这篇关于可手动删除多行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!