Skip to content

Instantly share code, notes, and snippets.

@timkinnane
Last active January 25, 2017 10:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save timkinnane/6149785 to your computer and use it in GitHub Desktop.
Save timkinnane/6149785 to your computer and use it in GitHub Desktop.
dataTable takes an array of key/value objects and return table of elements. e.g from parsed JSON. Requires jQuery. Widefat class is used for Wordpress admin pages, but its not a WP specific function.
jQuery(document).ready(function($) {
function dataTable(data) {
var response_table = $('<table class="widefat"></table>');
var header = $('<thead><tr></tr></thead>');
for(var k in data[0]) {
header.append('<th>'+k+'</th>');
}
response_table.append(header);
tbody = $('<tbody></tbody>');
$.each(data, function() {
var row = $('<tr></tr>');
$.each(this, function(k, v) {
row.append('<td>'+v+'</td>');
});
tbody.append(row);
});
response_table.append(tbody);
return response_table;
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment