简体   繁体   English

从成功函数“实时”追加Ajax结果

[英]Append Ajax results “live” from success function

Question: Why don't elements appended to the DOM from my AJAX success function appear until after the success function has returned? 问题:为什么在成功函数返回之后才会出现从我的AJAX成功函数附加到DOM的元素?

Context: I am using AJAX to get a JSON object with about 6000 inner objects that I want to use to populate a table. 上下文:我正在使用AJAX来获取一个JSON对象,其中包含大约6000个内部对象,我想用它来填充表格。 Unfortunately it takes about 10 seconds to create the table, so I'd like to give the user some visual feedback while it loads. 不幸的是,创建表需要大约10秒钟,所以我想在加载时给用户一些视觉反馈。 I thought the user would be able to see table rows "live" as I call append but they don't load until success returns. 我认为用户可以在我调用append看到“实时”表行,但是在成功返回之前它们不会加载。 When that didn't work, I tried updating the width of a Bootstrap progress bar, but the bar simply freezes during processing. 当这不起作用时,我尝试更新Bootstrap进度条的宽度,但是条形图在处理过程中只是冻结。

Goal: I would like the user to either see the table rows as they are appended or a progress bar at updates properly. 目标:我希望用户要么在附加时看到表格行,要么在更新时正确看到进度条。

Code: 码:

AJAX call: AJAX电话:

$.ajax({
        type: "GET",
        url: myUrl,
        contentType: "application/json; charset=UTF-8",
        dataType: "json",
        success: function(results){
            for(var i in results) {
                $("#bar").css("width", s / results.length + "%");
                console.log(i);
                var t_cell = $('<td class="'+attrs[i]+'">');
                t_cell.css("background-color", results[i]["val0"]);
                t_cell.append($("<span class='val'>").text(results[i]["val1"]));
                t_cell.append($("<span class='raw'>").text(results[i]["val2"]]));
                t_row.append(t_cell);
                $("#review_table > tbody").append(t_row);
            }
            $('#progress').hide();
        }
    });

HTML: HTML:

<div id="progress" class="progress progress-striped active">
  <div id="bar" class="bar" style="width: 1%;"></div>
</div>
<div id='review_div'>
    <table class='table table-condensed' id='review_table'>
        <thead></thead>
        <tbody></tbody>
    </table>
</div>

Try 尝试

$.ajax({
    type : "GET",
    url : myUrl,
    contentType : "application/json; charset=UTF-8",
    dataType : "json",
    success : function(results) {

        var i = 0;

        function process() {
            while (i < results.length) {

                console.log(results[i])
                $("#bar").css("width", s / results.length + "%");
                console.log(i);
                var t_cell = $('<td class="' + attrs[i] + '">');
                t_cell.css("background-color", results[i]["val0"]);
                t_cell.append($("<span class='val'>").text(results[i]["val1"]));
                t_cell.append($("<span class='raw'>").text(results[i]["val2"]));
                t_row.append(t_cell);
                $("#review_table > tbody").append(t_row);

                i++;
                if (i % 25 == 0) {
                    setTimeout(process, 0);
                    break;
                }
            }
        }
        process();

        $('#progress').hide();
    }
});

You could try something like this: 你可以尝试这样的事情:

for(var i in results) {
    createRow(i);
}

and

function createRow(i){
    var t_cell = $('<td class="'+attrs[i]+'">');
    t_cell.css("background-color", results[i]["val0"]);
    t_cell.append($("<span class='val'>").text(results[i]["val1"]));
    t_cell.append($("<span class='raw'>").text(results[i]["val2"]]));
    t_row.append(t_cell);
    $("#review_table > tbody").append(t_row);
}

However, I really don't think you should be rendering 6000 rows in a table though. 但是,我真的不认为你应该在表格中渲染6000行。 Try paginating your table using a library. 尝试使用库对表进行分页。 DataTables has a section on how to paginate Bootstrap tables specifically. DataTables有一节介绍如何专门对Bootstrap表进行分页。

http://www.datatables.net/blog/Twitter_Bootstrap http://www.datatables.net/blog/Twitter_Bootstrap

Rather than thinking of it as pagination you could actually write a loop that fetches your records in smaller chunks; 您可以实际编写一个循环,以较小的块取出您的记录,而不是将其视为分页; maybe try 100 records at a time. 也许一次尝试100条记录。 As you're appending your 100 records to the DOM, your loop keeps fetching and appending in the background. 当您将100条记录附加到DOM时,您的循环将继续在后台获取和追加。 You would have to be careful to keep them in the order you want them since there is no guarantee data will be returned in the order requested. 您必须小心按照您希望的顺序保留它们,因为无法保证数据将按请求的顺序返回。 I would maybe try using .after() to append the data in the right order. 我可能会尝试使用.after()以正确的顺序附加数据。 With some tinkering I think this could produce a clean and efficient user experience. 通过一些修补,我认为这可以产生干净,高效的用户体验。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM