简体   繁体   English

如何在不影响性能的情况下将大量数据加载到数据表

[英]How can I load the large amount of data to datatable without affecting performance

this script is working fine when i am loading less data(300 rows).当我加载较少的数据(300 行)时,此脚本工作正常。 But when i try to load more data(3000 rows) it's taking more time and browser showing some script slow down error.但是当我尝试加载更多数据(3000 行)时,它需要更多时间并且浏览器显示一些脚本减慢错误。 Please help me out to resolve this issue请帮我解决这个问题

HTML Table as shown below HTML表格如下图

<table class="table table-bordered" id="db_new_table">
     <thead>
        <tr>
          <th> Key </th>
           <th> Value </th>
        </tr>
     </thead>
     <tbody>
         //to here loading data from js     
     </tbody>
</table>

My JS Script Looks like below我的 JS 脚本如下所示

//Intializing database
    var table_new = $('#db_new_table').DataTable({            
      "bSortClasses": false,           
      "lengthMenu": [ 500, 1000, 1500, 2000 ],
      "pageLength": 500,
      "deferLoading": coutnt,            
    });


//Loading data to datatable   
var j=1;        
$.each((this.data), function(i, key){  
  var tr = '<tr class="table_row" data-id='+j+'> <td>'+ i +'</td> <td>'+ key +'</td> </tr>'; 

  table_new.rows.add($(tr)).draw();            
  j++;
});

3000 rows is quite a lot of DOM elements for browser to process. 3000 行是相当多的 DOM 元素供浏览器处理。 You can try using virtual scroll to prevent browser slowing or crash.您可以尝试使用虚拟滚动来防止浏览器变慢或崩溃。 Virtual scroll will only render DOM element which you see in your screen and replace data when you scroll, not add more DOM element.虚拟滚动只会呈现您在屏幕中看到的 DOM 元素并在您滚动时替换数据,而不是添加更多 DOM 元素。

You can try this library for virtual scroll https://clusterize.js.org/您可以尝试使用此库进行虚拟滚动https://clusterize.js.org/

In case your request take too much time, you may got timeout problem.如果您的请求花费太多时间,您可能会遇到超时问题。 In that case you can consider use paging or endless scroll在这种情况下,您可以考虑使用分页或无限滚动

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

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