在datatable jQuery

时间:2015-09-09 08:09:43

标签: php jquery ajax postgresql datatables

我有一个客户模块,我有这个客户列表。我正在使用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。

1 个答案:

答案 0 :(得分:1)

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

&#13;
&#13;
(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();
      }
    });
  }

})();
&#13;
&#13;
&#13;