javascript - Data from Table to json -
good day,
i have table data trying convert json format can print jspdf plugin problem cant seem insert proper json format existing json data.
here script convert table data json
$('.report-table tr').each(function(){ var obj = {}; obj['column'] = [{}]; obj['column']['value']=[]; $('th', this).each(function(){ //obj.column.value.push($(this).text()); obj['column']['value'] = $(this).text(); // obj.column.value.push($(this).text()); // obj['column'] = $(this).text(); }); $('td', this).each(function(){ // obj.column.value.push($(this).text()); obj['column']['value'] = $(this).text(); }); dynamic.contents.data.push(obj); });
this existing json data want appended
var dynamic = { "documenttitle": "dynamic print", "pagetitle": "dynamic print", "image": '', "pagetype": "d", "columns": 5, "contents": { "data": [ { "column": [ { "value": "jan.2007" }, { "value": "jan.2008" }, { "value": "jan.2009" }, { "value": "jan.2010" }, { "value": "period end" } ], "header": true, "fonttype": "b", "highlight": 1, "newline": 1 }, { "column": [ { "value": "-95000100" }, { "value": "85411250" }, { "value": "0" }, { "value": "55000250" }, { "value": "dynamic print 1 dynamic print 1 dynamic dynamic print 1dynamic print 1print 1" } ], "indent": 1 }, { "column": [ { "value": "95000100" }, { "value": " " }, { "value": "55000250" }, { "value": "55000250" }, { "value": "dynamic print 1 dynamic print 1 dynamic dynamic print 1dynamic print 1print 1" } ] }, { "column": [ { "value": "95000100" }, { "value": "85411250" }, { "value": "55000250" }, { "value": "55000250" }, { "value": "dynamic print 1 " } ] }, { "column": [ { "value": "95000100" }, { "value": "85411250" }, { "value": "55000250" }, { "value": "55000250" }, { "value": "dynamic print 1 " } ] }, { "hr": 1, "highlight": 1, "column": [ { "value": "95000100" }, { "value": "85411250" }, { "value": "55000250" }, { "value": "55000250" }, { "value": "dynamic print 1 " } ] }, { "column": [ { "value": "95000100" }, { "value": "85411250" }, { "value": "55000250" }, { "value": "55000250" }, { "value": "dynamic print 1 " } ] } ] } };
so problem want data format
but im getting kind of format js
can please me proper formatting. thank much! please let me know if there can make easier understand.
the problem visible @ beginning. have there 2 lines:
obj['column'] = [{}]; obj['column']['value']=[];
the first 1 makes obj['column']
array empty object @ 0-th position. @ second line access value
property of obj['column']
making object instead of array, , guess undesired result. think want make obj['column']
simple array , in array want push object property value
, this:
obj['column'] = []; obj['column'].push({value: []});
or better don't want push @ obj['column']
@ point, in .each()
methods:
obj['column'] = []; $('th', this).each(function(){ obj['column'].push({value: $(this).text()}); });
hope helps
Comments
Post a Comment