views:

49

answers:

4
<table width="100%" border="0" cellspacing="0" cellpadding="0" class="list">
              <tr>
                <th><a href="#" class="sortby">Full Name</a></th>
                <th><a href="#" class="sortby">City</a></th>
                <th><a href="#" class="sortby">Country</a></th>
                <th><a href="#" class="sortby">Status</a></th>
                <th><a href="#" class="sortby">Education</a></th>
                <th><a href="#" class="sortby">Tasks</a></th>
              </tr>
    <div class="dynamicData">
              <tr>
                <td>Firstname Lastname</a></td>
                <td>Los Angeles</td>
                <td>USA</td>
                <td>Married</td>
                <td>High School</td>
                <td>4</td>
              </tr>
              </tr>
              <tr>
                <td>Firstname Lastname</a></td>
                <td>Los Angeles</td>
                <td>USA</td>
                <td>Married</td>
                <td>High School</td>
                <td>4</td>
           </tr>
    </div> 
</table>

The idea is to update table rows when clicking on link with clasl "sortby" which is part of th table tag. I tried inserting div but this doesn't work. Separating this in two tables is not good solution because witdh in both tables cell are not following each other. Any other solution?

Thanks

+1  A: 

Indeed, you cannot have divs inside a table. Is the idea that you make an AJAX request, which returns some table rows, and you want to insert those rows into your table? If so, how about this:

$.get('...', function(data) {
    $('.list tr:not(:has(.sortby))').remove().after(data);
});

In other words, find and remove the rows you don't want, and then insert the stuff you do want.

VoteyDisciple
"Is the idea that you make an AJAX request, which returns some table rows, and you want to insert those rows into your table?" - That's correct. What is .list referring to?
ile
I thought IE was not cool with adding/removing TRs directly from tables?
Ryley
.list is just the table itself, as shown in the markup. I've never had any trouble with IE adding or removing table rows.
VoteyDisciple
Yeah, definitely does not add successfully in IE7: http://jsfiddle.net/mdGJK/
Ryley
See also: http://stackoverflow.com/questions/812693/cant-dynamically-add-rows-to-a-table-in-ie
Ryley
+2  A: 

Put the trs that need updating in a tbody tag and put your header in a thead tag.

You can add/remove trs to the tbody using standard jQuery.

I suggest you look in to the piles and piles of existing jQuery plugins that do this (edit: that do this type of thing - sorting of dynamic data). Unfortunately it's offline at the moment, so I can't provide a link!

EDIT:

trs = rows of data, i.e.:

        <tr>
            <td>Firstname Lastname</td>
            <td>Los Angeles</td>
            <td>USA</td>
            <td>Married</td>
            <td>High School</td>
            <td>4</td>
       </tr>

I assumed that is the type of thing your AJAX request will be returning.

Ryley
Ok, I'll give it a try, this sounds pretty simple. Thanks
ile
It works nicely, thank you!
ile
+1  A: 

For sorting you might try shuffling the rows directly:

Move row 0 below row 1:

$("table.list tr").eq(0).insertAfter($("table.list tr").eq(1))

Inserting a new row at the end would look like this:

$("<tr>Content</tr>").insertAfter($("table.list tr:last"))

Removing a row:

$("table.list tr").eq(1).remove()

If you're doing more than a few changes at a time you should consider building a new table outside of the DOM using jQuery. Then replace the old table in one move. The fewer modifications to the DOM the better.

Tsvetomir Tsonev