9

I'm trying to get the filled data using the div id but it is throwing $container.data(...) is undefined.

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

I have tried the following code to get the filled data:

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);
*/  


}

But it is throwing error, instead I tried with another option:

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

This is working. But I have multiple tables so I need to get the data by the particular table using the div id, what is the problem in my code?

mkHun
  • 5,507
  • 1
  • 25
  • 65

2 Answers2

4

Finally got the answer

 $('#app .ht_master tbody td').each(function(){
    var text = $(this).text();
 });
bhansa
  • 6,166
  • 2
  • 23
  • 42
mkHun
  • 5,507
  • 1
  • 25
  • 65
0

use .handsontable() instead of .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

Kumar_Vikas
  • 809
  • 7
  • 16