我无法使用最新版本删除行。我使用的是0.9.9版本。
这就是我要做的:
var $container = $("#add-table");
$container.handsontable(options);
var handsontable = $container.data('handsontable');
var data = handsontable.getData();
if(data.length > 1 && handsontable.isEmptyRow(data.length-1)) {
handsontable.alter('remove_row', parseInt(data.length-1));
}在Handsontable delete multiple rows上也有类似的问题,但这并不能解决我的目的。此链接上的小提琴与所提供的解决方案不起作用。
任何帮助都将不胜感激。
发布于 2013-07-12 13:52:38
按照目前的情况,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()];
//}
}这是一个巨大的问题,因为handsontable引用了相当多的函数。不过,幸运的是,我们可以使用afterSelectionEnd event。
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'),我们只需要提供索引。
这是我为获得想要的结果而制作的working demo。
备注:
由于有一个bug,我们需要在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...');
}
});希望这对你有所帮助,如果你还需要什么,请告诉我!
发布于 2013-07-10 10:04:16
所以我猜你想删除最后一行的空行?
确保没有将minSpareRows设置为大于0(默认值)的值。如果minSpareRows大于零,则不能使用alter('remove_row')删除额外的行。
我使用此数据来模拟空的最后一行:
var myData = [
["", "Kia", "Nissan", "Toyota", "Honda"],
["2008", 10, 11, 12, 13],
["2009", 20, 11, 14, 13],
["2010", 30, 15, 12, 13],
["",,,]
];检出示例.When u单击del链接它会删除最后一行空的内容。希望这能对你有所帮助
Working Example
https://stackoverflow.com/questions/17474011
复制相似问题