简体   繁体   中英

Adding new rows to an existing table

I have the following code which creates a new table.

var html = '<table>';
$.each( results.d, function( index, record ) {
    html += '<tr><td>' + record.ClientCode + '</td></tr>';
});
html += '</table>';
$("#divResults").append(html);

How do I change this so instead of creating a brand new table each time, it adds tr and td data to an already existing table? Where the new data always gets added after the last existing tr/td in the table?

If you already have created html table in #divResults div, you can try:

$.each( results.d, function( index, record ) {
    $('#divResults table tr:last').after('<tr><td>' + record.ClientCode + '</td></tr>');
});

Example

var html;
$.each( results.d, function( index, record ) {
    html += '<tr><td>' + record.ClientCode + '</td></tr>';
});
$("table").append(html);

You can either do it the first time the way you have then all other times this way. Or you can start out with an empty <table> when the page loads.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM