Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 19 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,20 @@ function formatContainer(container) {
container.className = 'container';
return container;
}
/**
* Formats Root UL element - can be overwritten in options
* @param: UL element
* @return: formatted UL element
*/
function formatRootUl(ul) {
ul.className += ' ul-root-item';
return ul;
}

/**
* Formats UL element - can be overwritten in options
* @param: UL element
* @return: formatted LI element
* @return: formatted UL element
*/
function formatUl(ul) {
ul.className = 'ul-item';
Expand Down Expand Up @@ -60,7 +70,8 @@ var _options = {
formatUl: formatUl,
formatLi: formatLi,
formatProperty: formatProperty,
formatValue: formatValue
formatValue: formatValue,
formatRootUl:formatRootUl
};

function JSON2HTMLList(json, options) {
Expand Down Expand Up @@ -137,8 +148,13 @@ function JSON2HTMLList(json, options) {
}
}


walk(json, container);

if(container.children.length > 0){
container.children[0] = _options.formatRootUl(container.children[0])
}


return container;

Expand Down
13 changes: 12 additions & 1 deletion sample.html
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,17 @@ <h2>Output</h2>

(function() {

var options = {
formatUl: function(ul) {
ul.className = 'my-ul-class';
return ul;
},
formatRootUl:function(ul){
ul.className = ul.className+" ul-root"
return ul;
}
}

var myJSON = {
"glossary": {
"title": "example glossary",
Expand All @@ -57,7 +68,7 @@ <h2>Output</h2>

document.getElementById('input').appendChild(document.createTextNode(JSON.stringify(myJSON, null, 4)));

document.getElementById('output').appendChild(JSON2HTMLList(myJSON));
document.getElementById('output').appendChild(JSON2HTMLList(myJSON, options));

})();

Expand Down