使用JavaScript将500,000行添加到表中而不会导致页面崩溃

Neo*_*Neo 3 html javascript ajax

我有一个报告系统,用户单击该报告,执行SQL,结果显示在表中。我的问题是,单个报告为该报告带来了超过500,000行。系统需要花费一些时间通过AJAX调用来检索数据,但是当浏览器将HTML添加到页面时,它就“挂起”。

我的问题是,有没有其他方法可以将HTML添加到页面而不会导致浏览器挂起?

var HTMLPRE = '<p class="text-center"><b id="lblReportStatus">Producing report...</b><br><small>Average time: ' + parseFloat($('#hidReportID').attr('data-avg')).toFixed(2) + ' seconds</small>' +
    '<br><small>Current time: <span id="divCurrentTimeTaken">0</span></small></p>';
window.CurrentTimer = 0;
$('#reportresults').html(HTMLPRE);
// start the timer
window.ProducingReportTimer = window.setInterval(function() {
    window.CurrentTimer++;
    $('#divCurrentTimeTaken').html(window.CurrentTimer + ' seconds');
}, 1000);

$.post(window.routes['ReportWriter.ProduceReport'], FormData, function(resp, status) {
    if (status === 'success') {
        if (resp.code == '200') {
            $('#lblReportStatus').text('Generating report...<br>Please note your browser may become un-responsive.  Please wait for a few minutes if this happens.');
            ProduceReportTable(resp);
        }
    } else {
        alert("Unable to produce report.  Please contact support with the below information:\r\nStatus code" + status);
    }
}).fail(function(err, status) {
    alert("Unable to produce report.  Please contact support with the below information:\r\n" + err);
});
Run Code Online (Sandbox Code Playgroud)
function ProduceReportTable(resp){
    var ReportHTML = '<div class="row"><div class="col-xs-12"><button class="btn btn-primary" id="btnExportExcel"><i class="fa fa-file-excel"> Excel</i></a></div>' +
        '</div><div class="col-xs-12"><div class="table-responsive" style="overflow-x: auto;">' +
        '<table class="table-hover table-striped table" id="tblReport">' +
        '<thead><tr>';
    // loop through the headers first
    $(resp.headers).each(function (idx, head) {
        ReportHTML += '<th>' + head + '</th>';
    });

    ReportHTML += '</tr></thead><tbody>';
    // loop through the data

    $(resp.rows).each(function (idx, row) {
        ReportHTML += '<tr>';
        $.each(row, function() {
            ReportHTML += '<td>' + (this instanceof Window ? '' : this) + '</td>';
        });
        ReportHTML += '</tr>';
    });
    ReportHTML += '</tbody></table></div></div>';
    $('#reportresults').html(ReportHTML);
    window.clearInterval(window.ProducingReportTimer);
    /*
    $('#tblReport').dataTable({
        deferRender:    true,
        /*scrollY:        200,*//*
        scrollCollapse: true,
        scroller:       true
    });*/

    // enable the excel button
    $('#btnExportExcel').on('click', function(e){
        e.preventDefault();
        let TableSource = document.getElementById('tblReport');
        var today = new Date();
        var dd = today.getDate();
        var mm = today.getMonth()+1; //January is 0!
        var yyyy = today.getFullYear();
        if(dd<10) {
            dd = '0'+dd
        }
        if(mm<10) {
            mm = '0'+mm
        }
        today = yyyy + '-' + mm + '/' + dd;
        GenerateXLSX(TableSource, true, resp.ReportName + '-' + today + ".xlsx")
    });
}
Run Code Online (Sandbox Code Playgroud)

抱歉,如果其他地方都没有找到答案,我已经搜索过,但是找不到任何东西。

Lés*_*ter 7

不。评论已经暴露出了很好的答案。其要点是,从性能角度(任何东西都不能正确渲染这么多数据),从用户角度(没人可能一次处理那么多数据)和从安全角度考虑,返回很多行是一个坏主意。(您冒着对应用进行大规模DoS攻击的风险)。我可能会想到的唯一用例是创建报告,在这种情况下,您应该导出Excel / PDF文档而不是将其显示为HTML。

您应该实现分页和过滤选项来解决您的问题。如果您必须为此使用JavaScript,请在您的应用程序上使用jquery数据表和适当的AJAX端点。