Solved

jquery .each looping through JSON to create table rows and elements

Posted on 2012-03-25
3
667 Views
Last Modified: 2012-03-26
I'm currently looping through JSON data returned from $.getJSON to create a table row and its elements, as follows:

$.each(json.DATA, function(i,col) {
 $('#joblist tbody').append('<tr id="' + col[1] + '"><td title="This is a ' + col[2] + ' office job; the code for the job team is ' + col[1] + '"><input type="checkbox" class="' + col[4] + ' ' + col[5] + '" name="remove" value="' + col[1] + '" />' + col[0] + ': ' + col[3] + '</td></tr>');
}

My question is, is there a better (faster) way to do this?
0
Comment
Question by:ShanghaiD
3 Comments
 
LVL 20

Expert Comment

by:Proculopsis
ID: 37764506
Probalby not faster but different (jsfiddle).
0
 
LVL 7

Accepted Solution

by:
Lalit Chandra earned 500 total points
ID: 37764779
yes. you can do it in a more faster way.
As you might know that Every DOM Manipulation  hinders the performance. So, we should take care of it in the script to maintain the speed of the script.

in your code, you are manipulating the dom many times.and all of the time same DOM element is modified.So, this time you can tune your code to perform better than it is doing right now.

What can you is, just append all the string to a single string variable in the loop,then after completition of the loop,simply update the DOM with the resultant string.

What i want to say is , simply replace your statement

$.each(json.DATA, function(i,col) {
 $('#joblist tbody').append('<tr id="' + col[1] + '"><td title="This is a ' + col[2] + ' office job; the code for the job team is ' + col[1] + '"><input type="checkbox" class="' + col[4] + ' ' + col[5] + '" name="remove" value="' + col[1] + '" />' + col[0] + ': ' + col[3] + '</td></tr>');
}


WITH the following statements

var strHTML="";
$.each(json.DATA, function(i,col) {
 strHTML=strHTML +('<tr id="' + col[1] + '"><td title="This is a ' + col[2] + ' office job; the code for the job team is ' + col[1] + '"><input type="checkbox" class="' + col[4] + ' ' + col[5] + '" name="remove" value="' + col[1] + '" />' + col[0] + ': ' + col[3] + '</td></tr>');
}
 $('#joblist tbody').append(strHTML);

So, in my code,   in the loop, i simple append all the html string to a variable,then at the end i simple update the DOM element.So, in that code, DOM manupulation is done atmost one single time.It does't depend on the data or the loop.

So, if you measure the performence, the second code will be perform better as the amount t of data in the JSON will increase.

Hope you understand that.
0
 

Author Closing Comment

by:ShanghaiD
ID: 37765041
Your answer is very clear and, yes, it is faster than my old code.
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article shows how to create and access 2-dimensional arrays in JavaScript.  It includes a tutorial in case you are just trying to "get your head wrapped around" the concept and we'll also look at some useful tips for more advanced programmers. …
The task A number given should be formatted for easy reading by separating digits into triads. Format must be made inline via JavaScript, i.e., frameworks / functions are not welcome. So let’s take a number like this “12345678.91¿ and format i…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question