Problem with inserting a string containing html table rows (fetched by jquery ajax) into an html table - strange element jumble?
I'm trying to load a web page content little by little as the user scrolls since the content is huge, and the user is usually only interested in the first part(s) of it.
The page consists of a big table. Everytime the user scrolls to the bottom of the page, I get a few more rows using ajax and append it to the table. Or, I want it to work that way...
My scrollingevent works as it should. The ajax request is sent as it's supposed to, and I get the data as intended. The problem starts when I want to append this data to the table.
Let's say my answer to the ajax request (called 'data' in my script) is a string like this:
'<tr class="tasklist" id="r11">
<td class="titlecell">Some kind of string title</td>
<td>Some kind of string</td>
</tr>
<tr class="tasklist" id="r12">
<td class="titlecell">Some other kind of string title</td>
<td>Some other kind of string</td>
</tr>'
Then I try to use .after(data) to append those rows to my table. Only on web page, those rows fore some strange reason are inserted in the table like this:
'<tr>
<td class="titlecell"></td>
<td></td>
</tr>
<tr class="tasklist" id="r11"></tr>
<tr>
<td class="titlecell"></td>
<td></td>
</tr>
<tr class="tasklist" id="r12"></tr>'
No cell content at all, and cells are inserted in extra rows, leaving the "original" rows empty. Not really what I wa开发者_如何学Gonted right? But why? Any of you web oracles who can put me back on my track again by explaining what I'm doing wrong or what a better (working) solution should look like, I'd be really really grateful!
This is my javascript function as it looks right now:
SPECIAL.loadMyTaskList = function() {
// to avoid multiple simultaneous function calls
if ($('div #loader').html().trim() == '') {
var last = $("#mytasklist .tasklist:last").attr("id");
last = last.substring(1); // row id is on format r[number]
$('div #loader').html('<img src="images/loading.gif" alt="Wait..."> Loading more tasks');
$.get('includes/mytasklist.php?from='+last+'&nbr=10&ajax=1', function(data) {
if (data=='')
// all data is already loaded
$(window).unbind('scroll');
else
// append to table #mytasklist, after
// the last row (with class tasklist)
$("#mytasklist .tasklist:last").after(data);
// if browser window is so large, that the first
// post(s) of data fits without creating a scrollbar
if (!BASIC.pageHasScrollbar())
SPECIAL.loadMyTaskList();
$('div #loader').empty();
});
}
}
try this code, instead of after
use append
-
$("#mytasklist").append(data);
if you are having tbody
in your table then try this-
$("#mytasklist > tbody").append(data);
I finaly found the answer to the problem myself. It wasn't a problem with the code above. The problem was that my jquery file was corrupt (but still valid code, so firebug didn't catch it). I suspect that a not so very thought through search-n-replace was the culprit
精彩评论