我是 Handsontable 的新手,在我的应用程序中单击按钮(表编辑器)时,我打开了(TableEditor.html)最初有 10 行和 10 列的 Handsontable,用户可能会使用某些行和列其余部分留空,使用我的代码它也给出空单元格:(,我想在单击“加载”按钮后和将其加载到 json 之前从handsontable中删除空行和列? 我的代码如下:
表格编辑器.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,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,, ,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,, ,,,,,,
最佳答案
我做了这样的事情:
var gridData = hot1.getData();
var cleanedGridData = {};
$.each( gridData, function( rowKey, object) {
if (!hot1.isEmptyRow(rowKey)) cleanedGridData[rowKey] = object;
});
关于javascript - 如何从handsontable中删除空行和空列?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30708863/