如何从handsontable中删除空行和列?

时间:2015-06-08 12:12:37

标签: javascript jquery

我是Handsontable的新手,在我的应用程序按钮(表编辑器)上单击,我打开(TableEditor.html)最初有10行和10列的handsontable,可能会发生用户可能会使用某些行和列并且剩下的都是空的,使用我的代码它也会给出空单元格:(,我想在点击按钮后加载'并在加载到json之前从handsontable删除空行​​和列? 我的代码如下:

TableEditor.html

 <html>
    <head>
    <script src="http://handsontable.com/dist/handsontable.full.js"></script>
    <link rel="stylesheet" media="screen" href="http://handsontable.com/dist/handsontable.full.css">
    <script type="text/javascript" src="http://code.jquery.com/jquery-1.10.1.min.js"></script>
    </head>
    <body>
    <div id="hot"></div>
    <input type="button" name="load" id="load" value="Load">

    <script>
    var data = [[]];

              var container = document.getElementById('hot');
              var hot = new Handsontable(container,
                {
                  data: data,
                  rowHeaders: true,
                  colHeaders: true,
                  minSpareRows: 0,
                  colHeaders: true,
                  contextMenu: true,
                  minRows: 10,
                  minCols:10,  
                  mergeCells: true,
                  contextMenu: true,
                  removeRowPlugin: true

                }); 


               Handsontable.Dom.addEvent(load, 'click', function() {


                   for(var i=0; i<hot.countRows();i++)
                   {
                      //alert('Done...'+hot.isEmptyRow(i)+'-- '+i);
                       if(hot.isEmptyRow(i))
                       {
                           hot.alter('remove_row', i); 
                           //alert('Removed...'+i);
                       }
                   }  
                    for(var j=0; j<=hot.countCols();j++)
                   {
                        alert('Done...'+hot.isEmptyCol(j)+'-- '+j);
                       if(hot.isEmptyCol(j))
                       {
                           hot.alter('remove_col', j) ; 
                           alert('Removed...'+j);
                       }
                   }  


                  $.ajax('json/save.json', 'GET', JSON.stringify({"data": hot.getData()}), function (res) {
                      alert('Done...');
                    var response = JSON.parse(res.response);
                    alert('Done...');
                    if (response.result === 'ok') {
                      alert('Data saved');
                    }
                    else {
                      alert('Save error');
                    }
                  });
                }); 



    </script>
    </body>
    </html>

它提供的数据如下:

B,C ,,,,,,,,, C,B ,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,, ,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,, ,,,,,,

1 个答案:

答案 0 :(得分:0)

我做了类似的事情:

var gridData = hot1.getData();
var cleanedGridData = {};

$.each( gridData, function( rowKey, object) {
    if (!hot1.isEmptyRow(rowKey)) cleanedGridData[rowKey] = object;
});