最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

javascript - Refresh table after using jQuery .append() - Stack Overflow

matteradmin7PV0评论

The following code gets a JSON object and then spits its contents out into a <table>. The first time I do it I get my JSON content just fine. However, when I refresh, the refreshed data is stuck onto the bottom of my table. How do I refresh the data to show the new data only? I tried using .remove() but there was an obvious deleting and then a refresh of data.

    $(function() {
        $('#ReportedIssue').change(function() {
            //$('.data').remove()
            $.getJSON('/CurReport/GetUpdatedTableResults', function(json) {
                for (var i = 0; i < json.GetDocumentResults.length; i++) {
                    $('#DocumentInfoTable').append(
                        "<tr class='data'>" +
                        "<td>" + json.GetDocumentResults[i].Document.DocumentId + "</td>" +
                        "<td>" + json.GetDocumentResults[i].Document.LanguageCode + "</td>" +
                        "<td>" + json.GetDocumentResults[i].ReportedIssue + "</td>" +
                        "<td>" + json.GetDocumentResults[i].PageNumber + "</td>" +
                        "</tr>"
                    );
                };
            });
        });
    });

Thank you,

Aaron

The following code gets a JSON object and then spits its contents out into a <table>. The first time I do it I get my JSON content just fine. However, when I refresh, the refreshed data is stuck onto the bottom of my table. How do I refresh the data to show the new data only? I tried using .remove() but there was an obvious deleting and then a refresh of data.

    $(function() {
        $('#ReportedIssue').change(function() {
            //$('.data').remove()
            $.getJSON('/CurReport/GetUpdatedTableResults', function(json) {
                for (var i = 0; i < json.GetDocumentResults.length; i++) {
                    $('#DocumentInfoTable').append(
                        "<tr class='data'>" +
                        "<td>" + json.GetDocumentResults[i].Document.DocumentId + "</td>" +
                        "<td>" + json.GetDocumentResults[i].Document.LanguageCode + "</td>" +
                        "<td>" + json.GetDocumentResults[i].ReportedIssue + "</td>" +
                        "<td>" + json.GetDocumentResults[i].PageNumber + "</td>" +
                        "</tr>"
                    );
                };
            });
        });
    });

Thank you,

Aaron

Share Improve this question asked Mar 29, 2010 at 21:05 Aaron SalazarAaron Salazar 4,53710 gold badges43 silver badges55 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 5

It will be more efficient to build the HTML as follows (and of course, solves the problem you're experiencing):

$(function() {
    $('#ReportedIssue').change(function() {
        //$('.data').remove()
        $.getJSON('/CurReport/GetUpdatedTableResults', function(json) {
            var str = '';
            for (var i = 0; i < json.GetDocumentResults.length; i++) {
                str += "<tr class='data'>" +
                    "<td>" + json.GetDocumentResults[i].Document.DocumentId + "</td>" +
                    "<td>" + json.GetDocumentResults[i].Document.LanguageCode + "</td>" +
                    "<td>" + json.GetDocumentResults[i].ReportedIssue + "</td>" +
                    "<td>" + json.GetDocumentResults[i].PageNumber + "</td>" +
                    "</tr>"
            };

            $('#DocumentInfoTable').html(str);
        });
    });
});
Post a comment

comment list (0)

  1. No comments so far