由div id的handsontable .getData不起作用?

时间:2017-09-29 06:58:15

标签: javascript jquery html handsontable

我尝试使用div ID获取填充数据,但它正在抛出$container.data(...) is undefined

<!-- ... -->
    <div id="app" style="width: 500px; height: 200px;"></div>   
</div>
<button id="app_id" onclick="json()">Json</button>

我尝试了以下代码来获取填充数据:

var rowHead = colAr;
var colHead = dataObj[idname].col;
var data = [[]],
    container = document.getElementById("app"),
    selectFirst = document.getElementById('selectFirst'),
    rowHeaders = document.getElementById('rowHeaders'),
    colHeaders = document.getElementById('colHeaders'),
    hot;

hot = new Handsontable(container, {
    minRows:rowHead.length,
    minCols:colHead.length,
    startRows: 5,
    startCols: 5,
    rowHeaders: rowHead,
    colHeaders: colHead,
    contextMenu: false,
    outsideClickDeselects: false,
    removeRowPlugin: true
});
hot.loadData(data);

function json() {
    var $container = $("#app");
    var handsontable = $container.data('handsontable').getData();
    console.log(handsontable);
/*
    var htContents = hot.getData();
    var jsonObj = {};
    for (var i = 0; i < htContents.length; i++)
    {
    jsonObj[rowHead[i]] = htContents[i];
    }
    console.log(jsonObj);
*/  


}

但是它抛出错误,而是尝试了另一种选择:

var cont = hot.getData();
console.log(cont);

这很有效。但我有多个表,所以我需要使用div id获取特定表的数据,我的代码中有什么问题?

2 个答案:

答案 0 :(得分:4)

终于得到了答案

 $('#app .ht_master tbody td').each(function(){
    var text = $(this).text();
 });

答案 1 :(得分:0)

使用.handsontable()代替.loadData

<强> HTML:

<div id="app"></div>   
 <a href="javascript:void(0)" id="app_id">Json</a>

<强> JAVASCRIPT:

$(document).ready(function(){
var data = [[]],
    container = document.getElementById("app"),
    hot;

hot = {
        data:data,
    minRows:15,
    minCols:6,
    startRows: 5,
    startCols: 5,
    contextMenu: false,
    outsideClickDeselects: false,
    removeRowPlugin: true
};

$('#app').handsontable(hot);
//hot.loadData(data);

$('#app_id').on('click',function(){
debugger
var result = $('#app').data('handsontable').getData();
});
});

Follow This Fiddle