I have the following function that takes JSON and turns it into table rows:
function addDataFromJson(json) {
var data = json.data;
var rows = '';
$('.uiGridContent tbody').html('');
for (var i = 0; i < data.length; i++) // for each row
{
var columns = '';
for (var b = 0; b < Object.keys(data[i]).length; b++) // for each column
{
var content = data[i][Object.keys(data[i])[b]];
columns += '<td>' + content + '</td>';
}
var row = columns;
row = '<tr>\
<td><input type="checkbox"></td>\
<td>\
<div class="dropdown">\
<a class="buildMenu" data-toggle="dropdown"><span class="arrow"><span class="icon action small">Actions</span></span></a>\
</div>\
</td>\
' + columns + '\
</tr>';
rows =+ row;
}
$(rows).appendTo('.uiGridContent tbody');
}
The JSON looks like this (for those curious):
"data": [{
"id": 1,
"name": "<a href=\"#\">Sheffield University</a>",
"departments": ["Software", "Recruitment", "Consulting"],
"locations": ["Sheffield", "Rotherham", "London", "New York"]
}, {
"id": 2,
"name": "<a href=\"#\">Sheffield College</a>",
"departments": "",
"locations": ["Hillsborough", "City Centre", "Crystal Peaks"]
}, {
"id": 3,
"name": "<a href=\"#\">Sheffield High School</a>",
"departments": ["Medical", "Family", "Criminal"],
"locations": ["Sheffield", "Rotherham"]
}...
However if I pass back 1000+ records then the JavaScript struggles and it takes a few seconds to do it and causes the browser to lock up.
Any ideas on how to improve this code?
It seems to be the line where I append the rows to the page:
$(rows).appendTo('.uiGridContent tbody');
As if I comment that line out, then it's really fast...