Skip to content

Instantly share code, notes, and snippets.

@edwardhotchkiss
Last active December 10, 2015 21:38
Show Gist options
  • Save edwardhotchkiss/4496770 to your computer and use it in GitHub Desktop.
Save edwardhotchkiss/4496770 to your computer and use it in GitHub Desktop.
Populate a two column table via data-id's that link to a JSON object (handles nested objects also, needs recursion)
// demo
!(function YODAWG(window, document) {
(typeof(console) !== 'undefined') ? console.log('YO DAWG, I HEARD YOU LIKED JAVASCRIPT, SO I PUT A FUNCTION AROUND A CLOSURE AND PARATHENSIS AROUND MY TYPEOF!') : null;
})(window, document);
// populate a table with a proxied JSON Object
YODAWG.prototype.populateTableViaJSON = function(tableSelector, dataToPopulate) {
var _self = this;
// clear View
$('td:last-child', tableSelector).text('');
// build out object and HTML
_.map(dataToPopulate, function(attr, index) {
var attr = index;
var data = dataToPopulate[index];
// check if attr Exists
if (typeof(dataToPopulate[attr]) === 'object') {
_.map(dataToPopulate[attr], function(attrNested, indexNested) {
var nestedAttributeRedux = attr.toString() + '.' + indexNested.toString();
var attrContainer = $('td[data-id="' + nestedAttributeRedux + '"]', tableSelector);
if (attrContainer.length === 1) {
var data = attrNested;
attrContainer.closest('td').text(data);
};
});
} else {
var attrContainer = $('td[data-id="' + attr + '"]', tableSelector);
if (attrContainer.length === 1) {
attrContainer.closest('td').text(data);
};
};
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment