无法使用HandsOnTable删除行

aut*_*tic 7 jquery handsontable

我无法删除最新版本的行.我正在使用版本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));
}
Run Code Online (Sandbox Code Playgroud)

Handsontable上有一个类似的问题删除了多行,但这并没有解决我的目的.此链接上的小提琴不适用于所提供的解决方案.

任何帮助,将不胜感激.

Dom*_*Dom 3

就目前而言,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()]; 
     //} 
     }
Run Code Online (Sandbox Code Playgroud)

这是一个很大的问题,因为Handontable 的引用功能相当多。不过,幸运的是我们可以使用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...');   
    }
});
Run Code Online (Sandbox Code Playgroud)

希望这对您有所帮助,如果您还需要任何其他信息,请告诉我!