简体   繁体   English

DataTable JS插入问题

[英]DataTable JS inserting issue

We have used DataTable.js for binding the grid. 我们使用DataTable.js来绑定网格。 We have around thousands of records so initially we are binding 500 records afterwards we are inserting them one by one. 我们有大约数千条记录,所以最初我们绑定了500条记录,之后我们逐一插入它们。 But screen is frozen while inserting one by one. 但是一个接一个地插入屏幕就冻结了。 We have written JavaScript code for inserting it one by one. 我们编写了用于逐个插入的JavaScript代码。

function bindLoadOverView() {
  $.ajax({
      type: "POST",
      contentType: "application/json; charset=utf-8",
      dataType: 'json',
      url: "../Loads/board.aspx/bindView",
      success: function(data1) {
        var aData = data1.d;
        for (var i = 500; i < aData.length; i++) {
          table.row.add([

            null,
            aData[i].row1,
            aData[i].row2
          ]).draw(false);
        }
      })
  });
}

First of all, set deferRender to true in your dataTable initialisation. 首先,在dataTable初始化中将deferRender设置为true This will prevent dataTables from executing some of the slow DOM operations while they are not needed. 这将阻止dataTable在不需要时执行某些慢速DOM操作。 Also please skip the draw() after each add() , this is totally redundant. 另外,请在每次add()之后跳过draw() add() ,这完全是多余的。

To prevent blocking of your page / other scripts you could insert the rows using an interval . 要防止阻止页面/其他脚本,可以使用interval插入行。 I understand that you just want to insert the remaining rows in the background silently. 我知道你只想静静地在后台插入剩余的行。 Using an interval will give other threads space for processing while the rows is inserted. 在插入行时,使用间隔将为其他线程提供处理空间。 Example : 示例:

var index = 500,
    max =  aData.length,
    insert = window.setInterval(function() {

      table.row.add([
         null,
         aData[i].row1,
         aData[i].row2
      ])

      index++
      if (index == max) window.clearInterval(insert)

    }, 20) 

After that you can draw() the table, if that for some reason is needed or necessary at all. 之后,您可以draw()表格,如果由于某种原因需要或必要的话。

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

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