【发布时间】: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