Solved

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

Posted on 2012-03-25
3
666 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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
jQuery delay animation 3 29
How can i  prevent default  Submision? 6 27
DataTables + iCheck + pagination Issue 2 22
Javascript Form Change 5 19
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
This article discusses how to create an extensible mechanism for linked drop downs.
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…

772 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