• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Filling a table with ajax response

 
leroy tsruya
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a html table that I want to fill dynamically with a data I got from an ajax call using JSON.
The table already exits in the page, so the server response doesn't contain the full table, but only data to fill the table.
Is that the right thing to do? my JSON actually contains an array, and I'll use Javascript code to fill the correct data in each row.
I wanted to know if that is the correct way to do it. I googled and didn't find too much info regarding that, so I thought to ask if you guys can direct me to some good links\tutorials about it.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65124
92
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
leroy tsruya wrote:Is that the right thing to do?
While it's not wrong, it's not the easiest approach. It's actually much easier to return a fully formatted HTML fragment that contains the table and replace the existing table in the DOM. This is especially attractive if you are using JSP or PHP on the backend that makes it easy to format HTML fragments.
 
leroy tsruya
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Wow, that I am honored to get an answer from you, Bear.
I have a copy of your awesome JQuery in action book. Just wanted to mention hat it is an excellent book and highly recommended for anyone wants to learn JQuery.
I learned a lot from reading it, and I frequently use it on my projects.

Back to the topic, is it better to create a new table every time I want to fill the table with new data (I have a select that each option should fill the table with different info). And what i do is just remove the first table, and put the new table instead?
I actually thought about it as it is much easier, but when I thought of 'filling' the table, it seemed right to have one table and just keep replacing the content only.

Thanks Bear!
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65124
92
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
leroy tsruya wrote:Back to the topic, is it better to create a new table every time I want to fill the table with new data (I have a select that each option should fill the table with different info). And what i do is just remove the first table, and put the new table instead?

As usual, there's never the "one right way", but as much as I love jQuery and enjoy writing client-side code with it, I never do anything on the client that's easier done on the server.

What I'd do is to put the table in a div, and then use the .load() method on it to fetch the entire table from the server. That will completely replace the contents each time.

If you have the 2nd edition of jQuery in Action (thanks for the praise on the book!) the DVD Ambassador example at the end of chapter 4, even if it focuses on filtering result sets, does something similar.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic