?
Solved

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

Posted on 2012-03-25
3
Medium Priority
?
683 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 2000 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

Want to be a Web Developer? Get Certified Today!

Enroll in the Certified Web Development Professional course package to learn HTML, Javascript, and PHP. Build a solid foundation to work toward your dream job!

Question has a verified solution.

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

Having worked on larger scale sites, we found out that you are bound to look at more scalable solutions to integrating widgets, code snippets or complete applications and mesh them into functional sites, in any given composition. To share some of…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
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…
Suggested Courses

752 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