Building an HTML table on the fly using jQuery

前端 未结 8 734
夕颜
夕颜 2020-12-07 18:18

Below is the code I use to build an HTML table on the fly (using JSON data received from the server).

I display an animated pleasewait (.gif) graphic while the data

相关标签:
8条回答
  • 2020-12-07 18:26

    You could insert the table into the DOM bit by bit. Honestly I'm not entirely sure if this will help with your problem, but it's worth a try. I'd do it roughly like this (untested code, could be refine some more):

    $("#result").append('<table id="myTable" cellspacing=0 cellpadding=2 border=1></table>');
    $('#myTable').append('<thead><tr></tr></thead>');
    $('#myTable').append('<tbody></tbody>');
    
    for (var i = 0; i < t.hdrs.length; i++) { 
        $('#myTable thead tr').append('<th>'+header+'</th>');
    }
    
    for (var i = 0; i < t.data.length; i++) { 
     myTr =    '<tr>';
     for (var j = 0; j < t.hdrs.length; j++) { 
      myTr += '<td>';
      if (t.data[i][t.hdrs[j]] == "") { 
       myTr += "&nbsp;" ; 
      }
      else { 
       myTr += t.data[i][t.hdrs[j]] ; 
      }
      myTr += "</td>";
      }
     myTr +=    "</tr>";
     $('#myTable tbody').append(myTr);
    }
    
    $("#PleaseWaitGraphic").addClass("hide");
    $(".rslt").removeClass("hide") ;
    
    0 讨论(0)
  • 2020-12-07 18:29

    You basically want to set up your loops so they yield to other threads every so often. Here is some example code from this article on the topic of running CPU intensive operations without freezing your UI:

    function doSomething (progressFn [, additional arguments]) {
        // Initialize a few things here...
        (function () {
            // Do a little bit of work here...
            if (continuation condition) {
                // Inform the application of the progress
                progressFn(value, total);
                // Process next chunk
                setTimeout(arguments.callee, 0);
            }
        })();
    }
    

    As far as simplifying the production of HTML in your script, if you're using jQuery, you might give my Simple Templates plug-in a try. It tidies up the process by cutting down drastically on the number of concatenations you have to do. It performs pretty well, too after I recently did some refactoring that resulted in a pretty big speed increase. Here's an example (without doing all of the work for you!):

    var t = eval('(' + request + ')') ;
    var templates = {
        tr : '<tr>#{row}</tr>',
        th : '<th>#{header}</th>',
        td : '<td>#{cell}</td>'
    };
    var table = '<table><thead><tr>';
    $.each(t.hdrs, function (key, val) {
        table += $.tmpl(templates.th, {header: val});
    });
    ...
    
    0 讨论(0)
  • 2020-12-07 18:29

    I've been using JTemplates to accomplish what you are describing. Dave Ward has an example on his blog here. The main benefit of JTemplates is that your html isn't woven into your javascript. You write a template and call two functions to have jTemplate build the html from your template and your json.

    0 讨论(0)
  • 2020-12-07 18:34

    Using innerHTML can definitely be much faster than using jQuery's HTML-to-DOM-ifier, which uses innerHTML but does a lot of processing on the inputs.

    I'd suggest checking out chain.js as a way to quickly build out tables and other repeating data structures from JavaScript objects. It's a really lightweight, smart databinding plugin for jQuery.

    0 讨论(0)
  • 2020-12-07 18:35

    What you are doing is building a string, and then parsing it all at once upon insertion. What about creating an actual table element (i.e. $("<table>")), and then adding each row to it in turn? By the time you actually insert it into the page, the DOM nodes will all have been constructed, so it shouldn't be as big a hit.

    0 讨论(0)
  • 2020-12-07 18:40

    Search the web for JavaScript and StringBuilder. Once you have a JavaScript string builder make sure you use the .append method for every concatenation. That is, you don't want to have any + concatenations. After that, search for JavaScript and replacehtml. Use this function instead of innerHTML.

    0 讨论(0)
提交回复
热议问题