Fast way to dynamically fill table with data from JSON in JavaScript

前端 未结 3 1239
轻奢々
轻奢々 2020-12-12 22:52

I am experimenting with jQuery, JSON etc. and came across following task. I have a loader script on the server which returns table data in JSON format. When received the JSO

3条回答
  •  一个人的身影
    2020-12-12 23:25

    It's probably faster to build the HTML for the entire row and add the row to the table rather than adding each individual element one at a time. You could also extend that to building all of the HTML in a string, then adding it all at one go, which would be even faster. In an MVC environment, I'd probably return a partial view (HTML) that has the table since that would maintain separation of concerns. I would avoid building HTML on the server (in a controller), though, as that would mean that you'd have to change code whenever you wanted to change how the table was displayed. If you're using PHP or another scripting language, then filter as appropriate for your environment.

    var html = '';
    for (var key=0, size=data.length; key'
                 + data[key][1]
                 + ''
                 + data[key][2]
                 + '';
    }
    
    $('#dataTable').append(html);
    

提交回复
热议问题