aspose file tools*
The moose likes HTML, CSS and JavaScript and the fly likes Filling a table with ajax response Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Bookmark "Filling a table with ajax response" Watch "Filling a table with ajax response" New topic
Author

Filling a table with ajax response

leroy tsruya
Ranch Hand

Joined: Sep 24, 2009
Posts: 57
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

Joined: Jan 10, 2002
Posts: 61773
    
  67

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.

[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
leroy tsruya
Ranch Hand

Joined: Sep 24, 2009
Posts: 57
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

Joined: Jan 10, 2002
Posts: 61773
    
  67

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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Filling a table with ajax response