當前標準的,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...');
}
});
希望這有助於讓我知道,如果你有什麼事!
你可以舉一個你的表包含的例子嗎? – Romoku
它是一個沒有數據的空表 – rizwaniqbal
然後你實際上並沒有刪除任何行。如果您不想顯示額外的行和列,請在handsontable選項中設置「minSpareRows:0」和「minSpareCols:0」。 – Romoku