简体   繁体   English

附加<tr>使用 JQuery AJAX 调用逐步转换为 HTML 表(Django 框架)

[英]Append <tr> to HTML table progressively with a JQuery AJAX call (Django framework)

I am new to Javascript, Jquery and Ajax request and I have an issue with the execution of my scripts.我是 Javascript、Jquery 和 Ajax 请求的新手,我的脚本执行有问题。

Basically I have a very long list to display on my home page (1200+ items).基本上我有一个很长的列表要显示在我的主页上(1200 多个项目)。 I used to load the page normally but since my list would take a long time to load (and since it's right in the middle of the page) there would be an enormous delay before all my HTML elements would show.我曾经正常加载页面,但由于我的列表需要很长时间才能加载(并且因为它就在页面中间),所以在我的所有 HTML 元素显示之前会有一个巨大的延迟。 For example my footer would be the last item to load and it would appear almost a full second after my navbar.例如,我的页脚将是最后一个加载的项目,它会出现在我的导航栏之后几乎整整一秒。

I decided to use AJAX requests to load my HTML first, get my navbar and my footer done and only then fetch my list to show it in the middle of my page.我决定首先使用 AJAX 请求加载我的 HTML,完成我的导航栏和页脚,然后才获取我的列表以将其显示在我的页面中间。

The first problem I encounter is that while my script is being processed, nothing appears.我遇到的第一个问题是,在处理我的脚本时,什么也没有出现。 This means that even if my first list item's "tr" is ready, it will only show when the 1200th's "tr" is also ready.这意味着即使我的第一个列表项的“tr”已准备好,它也只会在第 1200 个“tr”也准备好时显示。 I would need the table to be populated progressively.我需要逐步填充表格。 I can see my "tr"s being generated in my console but they are only applied in my HTML after they are all done (after 12'500ms).我可以看到我的“tr”正在我的控制台中生成,但它们仅在完成后(在 12'500 毫秒之后)应用到我的 HTML 中。

The second problem I have is that while this same script is being processed, my page is unresponsive to my requests.我遇到的第二个问题是,在处理相同的脚本时,我的页面对我的请求没有响应。 For example I can't "inspect" or click any button on the page until after the script is done.例如,在脚本完成之前,我无法“检查”或单击页面上的任何按钮。 This is concerning because even if the first problem is fixed I still have an unresponsive page for around 12'500 ms.这是令人担忧的,因为即使第一个问题得到解决,我仍然有一个大约 12'500 毫秒的无响应页面。

The code I use is the following:我使用的代码如下:

$(document).ready(function(){
    console.log( "Document Ready" );
    $.ajax({
      url: "update_list/",
      type: 'get',
      success:function(response) {
        buildTable(response)
      }
    });
});

The url provided in the previous code snippet links to this view in my Django backend: (The lists items I want to fetch are company stocks)前面的代码片段中提供的 url 链接到我的 Django 后端中的此视图:(我要获取的列表项是公司股票)

def update_list(request, *args, **kwargs):
    if request.is_ajax():
        stocks = StockListView().get_queryset().values('symbol', 'company_name', 'market_cap')
        return JsonResponse({"stocks_serialized": list(stocks)})

And then this is the script to populate my HTML table with:然后这是用于填充我的 HTML 表的脚本:

function buildTable(data){
  var table = document.getElementById('tableId')
  const values = Object.values(data);
  //this step is because I get a dictionary with an array as its first value. We only need the array for the next part.
  const stock_list = values[0]
  for (var i = 0; i < stock_list.length; i++){
    var row = `<tr>
                  <td>${stock_list[i].symbol}</td>
                  <td>${stock_list[i].company_name}</td>
                  <td>${stock_list[i].market_cap}</td>
              </tr>`
              table.innerHTML += row
    console.log( row )
  }
}

Would anybody know how to fix these issues?有人知道如何解决这些问题吗? Do I need to change my strategy to AJAX request only small increments of my list on a scrolldown trigger?我是否需要将我的策略更改为 AJAX 仅在向下滚动触发器上请求我的列表的小增量? Only around 20 elements of my list appear at the time, the rest is only accessible through scroll down in my table.当时我的列表中只有大约 20 个元素出现,其余的只能通过在我的表格中向下滚动来访问。

Thanks everybody for your help!感谢大家的帮助!

I found the solution to my problem on Jquery's website:我在 Jquery 的网站上找到了解决我的问题的方法: https://www.tutorialrepublic.com/faq/how-to-add-remove-table-rows-dynamically-using-jquery.php https://www.tutorialrepublic.com/faq/how-to-add-remove-table-rows-dynamically-using-jquery.php

function buildTable(data){
  const values = Object.values(data);
  const stock_list = values[0]
  for (var i = 0; i < stock_list.length; i++){
        //We first declare the variables for each <td> we need
        var symbol = stock_list[i].symbol
        var comanyName = stock_list[i].company_name
        var market_cap = stock_list[i].market_cap_rounded
        //We then append them to a string in html fashion
        added_row = '<tr>'
        + '<td>' + symbol +  '</td>'
        + '<td>' + comanyName +  '</td>'
        + '<td>' + market_cap +  '</td>'
        + '</tr>'
        //To finally append this long string to your table through it's ID:
        $('#tableId').append(added_row)
        };
  };

This made my home page much faster and solved both my problems.这使我的主页更快,并解决了我的两个问题。

What seemed to slow down the page a lot was this step:这一步似乎大大减慢了页面速度:

table.InnnerHtml += row

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

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