我有一个大约有 30000 行的 mysql 表。我必须将所有行放在 DataTable 中,并在每次加载表格页面时加载每个段(当您单击分页时)。我看到我可以在我的 JS 中使用 deferLoading 参数,但是当我使用它时我的页面没有加载。如您所见,我必须加载图像,所以我绝对必须对内容进行少量加载...
这是我的 HTML:
<table class="table table-striped table-bordered table-hover datatable products-datatable">
<thead>
<tr>
<th></th>
<th><?=_("Product")?></th>
<th></th>
</tr>
</thead>
<tfoot>
<tr>
<th></th>
<th><?=_("Product")?></th>
<th></th>
</tr>
</tfoot>
</table>
这是我的 JS:
var table = $('.products-datatable').dataTable( {
"order": [[ 1, "asc" ]],
"processing": true,
"serverSide": true,
"deferLoading": 30000,
"ajax": {
url: location.protocol + '//' + location.hostname + '/ajax/products.php?action=list',
type: "POST"
},
"columns": [
{ "data": "image",
"orderable": false,
"width": "80px" },
{ "data": "product" },
{ "data": "action",
"orderable": false,
"width": "20px",
"sClass": "class",
}
]
});
这是我的 AJAX:
$req = $pdo->prepare('SELECT product_id, name FROM products');
if ( $req->execute() ) {
if ($req->rowCount()) {
$result['draw'] = 1;
$result['recordsTotal'] = $req->rowCount();
$result['recordsFiltered'] = 10;
$result['data'] = array();
$result['DT_RowId'][] = array();
while( $row = $req->fetch() ) {
if ($row['name']) { $name = $row['name']; } else { $name = "N/A"; }
$result['data'][] = array( "DT_RowId" => $row['product_id'],
"DT_RowClass" => 'myclass',
"image" => '<a href="' . HOSTNAME._("product").'/'.$row['product_id'] . '"><img src="' . HOSTNAME.'assets/img/products/' . $row['product_id'] . '.jpg" class="product_thumb"></a>',
"product" => '<a href="' . HOSTNAME._("product").'/'.$row['product_id'] . '">' . $name . '</a>',
"action" => "<a href=\"#\" class=\"button-delete\" id=\"" . $row['product_id'] . "\"><i class=\"fa fa-close fa-2x text-danger\"></i></a>"
);
}
}
}
$req->closeCursor();
我确定我错过了什么... :-(
最佳答案
我相信您不需要使用 deferLoading
来从服务器端处理中获益。
您当前的脚本只返回所有记录,不进行排序或过滤。您需要使用 ssp.class.php
(在 DataTables 分发包中可用)或 emran/ssp正确处理服务器上的 AJAX 请求的类。
DataTables 库将在 AJAX 请求中发送 start
和 length
参数,指示需要哪部分数据,您的服务器端处理类将为您正确处理。
请查看 server-side processing 的示例获取更多信息。
关于javascript - 数据表 : DeferLoading is not working correctly,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30198115/