Skip to content
Permalink
Branch: master
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
50 lines (43 sloc) 1.1 KB

jQuery: Build table from JSON data

Given data like:

{% codeblock lang:javascript %} var data = { "GUEST" : { 'visits' : 1734070, 'visits_pct' : 74, 'users' : 1, 'net_pv' : 3432781, 'users_pct' : 0, 'pv_pct' : 13 }, 'Logged In' : { 'visits' : 4240, 'visits_pct' : 0, 'users' : 177, 'net_pv' : 188112, 'users_pct' : 0, 'pv_pct' : 0 } } {% endcodeblock %}

... a little jQuery ditty to make a table out of the data:

{% codeblock lang:javascript %} $.each(data,function(rowLabel,v) { if (! header) { $table.append(''); var $thead = $('thead tr',$table); $thead.append(''); $.each(v,function(headerLabel) { $thead.append('' + headerLabel + ''); }); header = true; } $table.append(''); var $tr = $('tr:last',$table); $tr.append('' + rowLabel + ''); $.each(v,function(j,cellData) { $tr.append('' + cellData + ''); }) }); {% endcodeblock %}

I'll come back and do something more interesting with this eventually, just wanted to jot it down for now.

You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.