Dynamic creation of large html table in javascript performance

前端 未结 7 1341
轻奢々
轻奢々 2020-12-02 23:30

I have an application which is used for data analysis and I\'m having a few performance issues with the creation of the table. The data is extracted from documents and it is

7条回答
  •  不思量自难忘°
    2020-12-03 00:14

    You can do couple of things to increase the performance:

    1. your rows variable is getting bigger and bigger so, don't store the html in one variable. solution can be $.each() function and each function you append the element into DOM. But this is minor adjustment.
    2. Html generating is good, but you can try DOM creating and appending. Like $('').
    3. And finally, this will solve your problem for sure : use multiple ajax call in the first ajax call collect how many data is available and fetch approximately 1,000 or may be more data. And use other calls to collect remaining data. If you want, you can use synchronous call or Asynchronous calls wisely.

    But try to avoid storing the value. Your DOM size will be huge but it should work on moder browsers and forget about IE6.

    @fuel37 : Example

    function outputDocumentNew(data, doc_id) {
        //Variable DOM's
        var rowSample = $('').addClass('row-class');
        var colSample = $('').addClass('col-class');
        var spanSample = $('').addClass('span-class');
        var inputButtonSample = $('').addClass('input-class');
    
        //DOM Container 
        var container = $('#documentRows');
        container.empty().append('
    '); //Static part var head = '\ \ ID\ Status\ Name\ Actions\ Origin\ \ '; container.append(head); var body = $(''); container.append(body); //Dynamic part $.each(data, function (index, value) { var _this = this; //DOM Manupulation var row = rowSample.clone(); //Actions var inpFailOne = inputButtonSample.clone().val('F').attr('rev', _this.id).addClass('failOne').click(function (e) { //do something when click the button. }); var inpPromoteOne = inputButtonSample.clone().val('P').attr('rev', _this.id).addClass('promoteOne').click(function (e) { //do something when click the button. }); row .append(colSample.clone().append(_this.id)) .append(colSample.clone().append(spanSample.colne().addClass('status').append(_this.status))) .append(colSample.clone().append(spanSample.colne().addClass('name').append(_this.name))) .append(colSample.clone().append(inpFailOne).append(inpPromoteOne)) .append(colSample.clone().append(_this.origin)); body.append(row); }); }

    in this process you need to create & maintain id's or classes for manipulation. You have the control to bind events and manipulate each elements there.

提交回复
热议问题