Created
November 17, 2016 15:57
-
-
Save durgesh97025/6861e37d090d6e93b9ac1f2b7f9e66b9 to your computer and use it in GitHub Desktop.
Create Json to HTML Table plugin and example.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var rextension = { | |
CreateTableFromJson: function (options){ | |
/* | |
var options ={ | |
data: results, | |
container: "#jsontotable", | |
FieldNotToPopulate: ["id","uri","etag","type","Id","TermGuid","type","Label","WssId"] | |
}; | |
*/ | |
console.log(options); | |
var data = options.data; | |
var firstObj = data[0]; | |
var columns = []; | |
var rows = []; | |
var htmlForTable = ''; | |
FindColumn(firstObj); | |
for(var index in data){ | |
FindRow(data[index]); | |
} | |
$(options.container).html(''); | |
//Creating Columns | |
AppendHtml('<table border="1" class="table table-bordered table-hover">'); | |
AppendHtml("<tr>"); | |
for(var index in columns){ | |
AppendHtml('<th>'+ columns[index] +'</th>'); | |
} | |
AppendHtml("</tr>"); | |
//End of Creating Columns | |
//Creating Rows | |
for(var index in rows){ | |
AppendHtml("<tr>"); | |
var row = rows[index]; | |
for(var i in row){ | |
AppendHtml('<td>'+ row[i] +'</td>'); | |
} | |
AppendHtml("</tr>"); | |
} | |
//End of Creating Rows | |
AppendHtml("</table>"); | |
$(options.container).append(htmlForTable); | |
function FindColumn(obj){ | |
for(var key in obj){ | |
if (obj[key].toString() != "[object Object]"){ | |
if ($.inArray(key, options.FieldNotToPopulate) > -1){ | |
//Field not to populate | |
} | |
else{ | |
columns.push(key); | |
} | |
} | |
else{ | |
FindColumn(obj[key]); | |
} | |
} | |
} | |
function FindRow(obj,row){ | |
if (!row){ | |
row = []; | |
} | |
for(var key in obj){ | |
if (obj[key].toString() != "[object Object]"){ | |
if ($.inArray(key, options.FieldNotToPopulate) > -1){ | |
//Field not to populate | |
} | |
else{ | |
row.push(obj[key]); | |
} | |
} | |
else{ | |
FindRow(obj[key],row); | |
} | |
} | |
rows.push(row); | |
} | |
function AppendHtml(html){ | |
htmlForTable = htmlForTable + html; | |
} | |
} | |
} | |
$.extend(rextension); | |
/* | |
Example for Above Usage | |
function CallRest(url,callback){ | |
$.ajax({ | |
url: url, | |
method: "GET", | |
headers: { "Accept": "application/json; odata=verbose"}, | |
success: function (data) { | |
var currentLength = data.d.results.length; | |
totalLength = totalLength + currentLength; | |
console.log(url); | |
console.log(totalLength-currentLength +" + "+currentLength+ " = "+totalLength); | |
console.log(data); | |
if($("#jsontotable").length == 0){ | |
$("#layoutsTable").after('<div id="jsontotable" class="jsontotable"></div>'); | |
} | |
var results = data.d.results; | |
var options ={ | |
data: results, | |
container: "#jsontotable", | |
FieldNotToPopulate: ["id","uri","etag","type","Id","TermGuid","type","Label","WssId","ID"] | |
}; | |
$.CreateTableFromJson(options); | |
callback(); | |
}, | |
error: function (data, errorCode, errorMessage) { | |
console.log(data); | |
} | |
}); | |
} | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment