Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 2012-03-25
3
Medium Priority
?
691 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

I've been trying to accomplish this for a while and it just struck me yesterday how to accomplish this task. I have done searches all over the internet looking for ways to email pages from my applications and finally I have done it!!! Every single s…
In Part 1 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7849-Hex-Maze.html) we covered the hexagonal maze basics -- how the cells are represented in a JavaScript array and how the maze is displayed.  In this part, we'…
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…

650 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