1

I have jQuery code that adds a response to the last <tr> of a table like so:

var table = '<td>' + result.table.product + '</td><td>' + result.table.code + '</td><td>' + result.table.value + '</td><td>' + result.table.time + '</td>';
$('#poo tr:last').after(table);

And here is my table:

<?php

$sql="SELECT * FROM wp_scloyalty WHERE userid = '$user_id' ORDER BY date ASC";
$result=mysql_query($sql);
?> 
<table id="poo" style="margin:10px 0px 0px 0px;" width="100%" border="0" cellpadding="0" cellspacing="0">
<tr>
    <td><strong>Product</strong></td>
    <td><strong>Code</strong></td>
    <td><strong>Value</strong></td>
    <td><strong>Date Redeemed</strong></td>
</tr>
<?php while($rows=mysql_fetch_array($result)){ ?>  
<tr class="currentpoints">
    <td><? echo $rows['product']; ?></td>
    <td><? echo $rows['code']; ?></td>
    <td><? echo $rows['value']; ?></td>
    <td><? echo $rows['date']; ?></td>
</tr>  
<? } ?>
<tr class="currentpoints">
    <td id="producttd"></td>
    <td id="codetd"></td>
    <td id="valuetd"></td>
    <td id="datetd"></td>
</tr>  
</table>

My problem is that when I add more than one entry it tries to fit them all into the last tr. Is there a way to add a tr or something after the last tr has been populated?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
JamesG
  • 2,018
  • 2
  • 28
  • 57

4 Answers4

2

There is a way to insert a whole new row, but you have to remember the implicitly inserted <tbody element, so you have to insert the new row as a child of this element.

var table = '<tr><td>' + result.table.product + '</td><td>' + result.table.code + '</td><td>' + result.table.value + '</td><td>' + result.table.time + '</td></tr>';

$('#poo > tbody').append( table );
Sirko
  • 72,589
  • 19
  • 149
  • 183
2

Shouldn't you add the <tr> tag when appending the row to the table?

var table = '<tr><td>' + result.table.product + '</td><td>' + result.table.code + '</td><td>' + result.table.value + '</td><td>' + result.table.time + '</td></tr>';
$('#poo tr:last').after(table);
Davide
  • 2,329
  • 1
  • 14
  • 12
0

You should keep track if the last tr has peen populated

//the last tr
var $lastTr = $('#poo tr:last');
var table = '<td>' + result.table.product + '</td><td>' + result.table.code + '</td><td>' + result.table.value + '</td><td>' + result.table.time + '</td>';
//check if it has the class populated
if($lastTr.hasClass('populated')){
   //if it has it create a new row
   var tr = $('<tr/>');
   tr.append(table);
   $lastTr.after(tr);
 }else{
   //use the last row
   $lastTr.html(table);
   $lastTr.addClass('populated');
 }
bpeterson76
  • 12,918
  • 5
  • 49
  • 82
Nicola Peluchetti
  • 76,206
  • 31
  • 145
  • 192
0

This should work:

function test(product, code, value, time)
{
    var table = '<tr><td>' + product + '</td><td>' + code + '</td><td>' + value + '</td><td>'+ time +  '</td></tr>';

    $('#poo > tbody').append(table);
}

test('1','1','1','1');
test('2','2','2','2');

JSFiddle output: http://jsfiddle.net/pzewX/

Daan Timmer
  • 14,771
  • 6
  • 34
  • 66