Skip to content

Instantly share code, notes, and snippets.

@beppe9000
Created June 6, 2017 12:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save beppe9000/39c42466794d739d22529cf3a520b077 to your computer and use it in GitHub Desktop.
Save beppe9000/39c42466794d739d22529cf3a520b077 to your computer and use it in GitHub Desktop.
2-column mode table from JSON associative array
function writeTable(tbody, ark, title) {
var td_style = 'padding:5px 10px;border:1px solid black';
var tble_style = 'border:1px solid black';
tbody.empty();
$('<tr/>').appendTo(tbody).append('<th colspan="4"><b>'+title+'</b></td>');
var tr = $('<tr/>').appendTo(tbody);
ark.forEach(function(itm,idx){
console.log("ITM"+idx+": NN="+itm.nn+" BB="+itm.bb);
if (idx % 2 === 0) tr = $('<tr/>').appendTo(tbody);
tr.append('<td style="'+td_style+'">' + itm.nn + '</td>');
tr.append('<td style="'+td_style+'">' + itm.bb + '</td>');
});}
var ark = JSON.parse('[{"nn":"BLU","bb":0},{"nn":"VERDI","bb":-6},{"nn":"NERO","bb":0},{"nn":"VIOLA CALDI","bb":-54},{"nn":"GIALLI","bb":0},{"nn":"GIALLI CALDI","bb":0},{"nn":"GIALLI PRECOCI","bb":-124},{"nn":"GIALLI PRECOCI CALDI","bb":-147},{"nn":"VERDI PRECOCI","bb":-97},{"nn":"ROSSI","bb":0},{"nn":"MINI VERDI","bb":2},{"nn":"DOPPI","bb":-9}]');
writeTable( $('#my-table'), ark, "THIS IS A 4-COLUMN HEADER TITLE")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment