【问题标题】:Faster way to propagate huge data in datatable jQuery在数据表 jQuery 中传播海量数据的更快方法
【发布时间】:2015-12-05 02:39:04
【问题描述】:

我有一个客户模块,我有这个客户列表。我正在使用 Laravel 5 和这个包 http://datatables.net/。我正在使用它,因为它具有内置的搜索和排序功能。因此,当我加载页面时,我发出 ajax 请求以查询我的customers 表中的所有记录,然后使用datatables 包/插件在表中传播所有这些记录。问题是当我有太多记录时,比如 20,000 条。页面不会响应。我认为这需要太多的处理。所以这是我的 jquery 代码:

$.ajax({
url: "api/customer/all", 
type: 'GET',
success: function(result){
var myObj = $.parseJSON(result);
//console.log(myObj);
    $.each(myObj, function(key,value) {
        var t = $('#CustomerList').DataTable();
        t.row.add( [
            value.id,   
            value.firstname,
            value.lastname,
            value.gender,
            value.phone_num,
            value.postcode,
            value.country,
            "<a class='btn btn-small btn-info' href='<?php echo URL::to('customer').'/';?>"+value.id+"/edit'><span class='glyphicon glyphicon glyphicon-edit' aria-hidden='true'></span></a>",
            "<form method='POST' action='<?php echo URL::to('customer').'/';?>"+value.id+"' accept-charset='UTF-8' class='pull-left' >"+
            "<input name='_method' type='hidden' value='DELETE'>"+
            "<button type='submit' class='btn btn-warning'><span class='glyphicon glyphicon-trash' aria-hidden='true'></span></button>"+"</form>",
        ] ).draw();

    });
}});

然后是路线

Route::get('api/customer/all', 'CustomerController@apiGetCustomers');

控制器功能

//$customers = Customer::orderBy('id', 'desc')->get();
$query = "SELECT * FROM customers ORDER BY id ASC;";  
$data = DB::connection('pgsql')->select($query);

return json_encode($data);

你看我之前也使用 Eloquent 来获取数据,但它处理大量数据很糟糕,所以我改用 Query Builder。我认为缓慢的是 jquery 因为我可以快速获取所有数据。如何更快地传播它?我正在使用 Postgre 9.3 SQL。

【问题讨论】:

  • 你为什么不使用某种分页? Datatables 插件原生支持它
  • @A.Wolff 是的,我正在使用分页。问题是数据表中所有记录的传播速度很慢。
  • the propagating of all records in the datatable is slow是什么意思???使用分页,您应该一次只向用户显示少量数据,而不是 20000。因此,在每个新表格页面上,您应该重新发送请求以仅显示相关数据,而不是全部
  • @A.Wolff 我明白你在说什么。您的意思是每页我将查询限制为 100,然后由于数据表在分页中具有 bulti,当他们选择第 2 页时,我将再次查询下一个 100?是这样吗?

标签: php jquery ajax postgresql datatables


【解决方案1】:

Dom 操作可能非常缓慢。您可能希望按间隔添加行,这可能对用户体验更好。因此,也许可以使用 setTimeout() 并使用记录数和等待毫秒数。可能值得一试。

(function loadDataTable() {

    var data,
      curIndex = 0,
      t = $('#CustomerList').DataTable(),
      AMOUNT_ROWS = 100;

    function addMoreRows() {

      var i, value, limit = curIndex + AMOUNT_ROWS;

      if (limit > data.length) limit = data.length;

      for (i = curIndex; i < limit; i++) {

        value = data[i];

        t.row.add([
          value.id,
          value.firstname,
          value.lastname,
          value.gender,
          value.phone_num,
          value.postcode,
          value.country,
          "<a class='btn btn-small btn-info' href='<?php echo URL::to('customer').'/';?>" + value.id + "/edit'><span class='glyphicon glyphicon glyphicon-edit' aria-hidden='true'></span></a>",
          "<form method='POST' action='<?php echo URL::to('customer').'/';?>" + value.id + "' accept-charset='UTF-8' class='pull-left' >" +
          "<input name='_method' type='hidden' value='DELETE'>" +
          "<button type='submit' class='btn btn-warning'><span class='glyphicon glyphicon-trash' aria-hidden='true'></span></button>" + "</form>"
        ]).draw();

        curIndex = i + 1;

        if (curIndex != data.length) setTimeout(addMoreRows, 200);  // give browser time to process other things

      }
    }

    $.ajax({
      url: "api/customer/all",
      type: 'GET',
      success: function(result) {
        data = $.parseJSON(result);
        addMoreRows();
      }
    });
  }

})();

【讨论】:

  • 另外,我可能会补充一点,您正在我认为的每一行上创建一个表单元素......这可能不是最好的方法。当您要提交数据并使用ajax时,使用一个函数并使用参数调用该函数。我大概有 10 年没用过表单元素了。
  • 我要做的第一件事就是删除最后一个单元格,然后看看性能如何......我敢打赌那是你的问题区域。如果是这样,请找到一种方法来延迟该单元格的呈现...例如,可能是 onShown 事件或其他什么,然后仅在用户实际查看该行时才呈现该单元格。
  • 您好,感谢您的回答,但我无法完全理解。可以举个例子吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-05-24
  • 2010-12-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多