Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/71.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Jquery 可手动删除多行_Jquery_Handsontable - Fatal编程技术网

Jquery 可手动删除多行

Jquery 可手动删除多行,jquery,handsontable,Jquery,Handsontable,我是个新手。我正在尝试使用“getSelected”和“alter”方法删除多个选定的表行(删除行)。然而,在下面的代码中,我得到了Firebug中未定义的错误“selection”和Chrome中的“UncaughtTypeError:无法读取未定义的属性“0”。选择哪一行或选择多少行并不重要。我仍然得到错误,并且没有删除任何行。请问我做错了什么 $(document).ready(function () { var myData = [ ["", "Kia"

我是个新手。我正在尝试使用“getSelected”和“alter”方法删除多个选定的表行(删除行)。然而,在下面的代码中,我得到了Firebug中未定义的错误“selection”和Chrome中的“UncaughtTypeError:无法读取未定义的属性“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();
  });
这是我的小提琴

谢谢。

您的“选择”变量在onClick处理程序的范围内不可见

尝试将“var selection=…”移动到处理程序函数中。 类似于

    $('.deletebutton').on('click', function () {
        var selection = $edit.handsontable('getSelected');
        $edit.handsontable('alter', 'remove_row', selection[0], selection[2]);
    });

目前,
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引用的功能非常强大。然而,幸运的是,我们可以使用

选后结束(r:编号,c:编号,r2:编号,c2:编号)

选择一个或多个单元格后(鼠标向上)激发的回调。

参数:
r
选择开始行
c
选择开始列
r2
选择结束行
c2
选择结束列

据报道,

alter('remove_row',index:Number,amount:Number(可选),source:String(可选))


删除给定索引处的行。默认金额等于1

这意味着为了使用
alter('remove_row')
,我们只需要提供索引


这是我为得到想要的结果而做的一个例子

注意:

由于存在错误,我们需要在
afterSelectionEnd事件中添加一些逻辑

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...');   
    }
});
var myData=[
[、“起亚”、“日产”、“丰田”、“本田”],
["2008", 10, 11, 12, 13],
["2009", 20, 11, 14, 13],
["2010", 30, 15, 12, 13]
];
//声明行变量
var row1=null,
row2=null;
var$container=$(“#示例网格”);
$container.handsontable({
数据:myData,
startRows:5,
startCols:5,
minSpareCols:0,
行数:0,
行标题:对,
colHeaders:是的,
上下文菜单:正确,
选择后结束:功能(x1、y1、x2、y2){
//添加此项是因为存在错误
如果((x1-y2)|(x1>x2&&y1>=y2)){
row1=x2;
如果(x2==0)
row2=parseInt(x1+1);
其他的
row2=x1;
}
否则如果(x1y2){
row1=x1;
row2=x2;
}
否则如果(x1>x2&&y1

希望这有帮助,如果你还需要什么,请告诉我

只需在构造函数的选项中添加
outside clickdeselects:false
,就可以得到getSelected()的正确结果


这是现场演示

解决的问题。我需要在on处理程序中移动getSelected:$('.deletebutton')。on('click',function(){var selection=$edit.handsontable('getSelected');if(selection){$edit.handsontable('alter','remove_row',selection[0],selection[2]);});