jquery 数据表除第一页外不分页
jquery datatable not pagiging except first page
我有一个带有服务器端处理的数据table。
数据来自 table 预期,但它没有按数据 table 分页。我在调试中看到数据库 aaData 的总记录数 14 将显示为 5,
所以我预计会有 3 页..但只有 1 页显示和页面中的 5 行。
这是我的表格 html:
<table style="text-align: center" class="table table-striped table-bordered table-hover" id="usersTable">
<thead>
<tr>
<th>
User Name
</th>
<th>
Account
</th>
<th>
Enable
</th>
<th>
Remark
</th>
</tr>
</thead>
<tbody>
</tbody>
</table>
和数据table def:
var tableObject = $("#usersTable").dataTable({
"bProcessing": true,
"bServerSide": true,
"sAjaxSource": "../../Controller/UserManagementControllercopy.php5",
"aoColumns": [
{ "mDataProp": "0", "sWidth": "40%", "bSearchable": false },
{ "mDataProp": "1", "sWidth": "20%"},
{ "mDataProp": "3", "sWidth": "20%" },
{ "mDataProp": "2", "sWidth":"20%" }
],
"fnServerData": function (sSource, aoData, fnCallback){
$.ajax({
"dataType": "json",
"contentType": "application/json; charset=utf-8",
"type": "GET",
"url": sSource,
"data": aoData,
"success": function(result){
fnCallback(result);
},
error: function (xhr, textStatus, error){
debugger
if (typeof console == "object") {
console.log(xhr.status + "," + xhr.responseText + "," + textStatus + "," + error);
}
}});
},
"oLanguage": {
"sLengthMenu": '<select>' +
'<option value="5">5</option>' +
'<option value="10">10</option>' +
'<option value="20">20</option>' +
'<option value="30">30</option>' +
'<option value="40">40</option>' +
'<option value="50">50</option>' +
'</select> Show'
},
"fnDrawCallback": function(){
},
"aaSorting": [
[1, 'asc']
],
"aLengthMenu": [
[5, 15, 20, -1],
[5, 15, 20, "All"] ],
"iDisplayLength": 5
});
和服务器代码:
$aColumns = array( 'USERNAME', 'ACCOUNT', 'REMARK', 'ENABLE');
$sQuery = " SELECT USERNAME,ACCOUNT,REMARK,ENABLE FROM users LIMIT ".$_GET['iDisplayStart'].", ".$_GET['iDisplayLength'].";";
$dbObject = Connection::getConnection();
$request = $dbObject->dbh->prepare($sQuery);
if ($request->execute())
{
$resultData["Data"] = $request->fetchAll(PDO::FETCH_ASSOC);
$sQuery = "SELECT COUNT(USERNAME) FROM users";
$request = $dbObject->dbh->prepare($sQuery);
$request->execute();
$iTotal = $request->fetchColumn(0);
$output = array(
"sEcho" => intval($_GET['sEcho']),
"iTotalRecords" => intval($iTotal),
"iTotalDisplayRecords" => intval($_GET['iDisplayLength']),
"aaData" => array()
);
for ( $j=0 ; $j<count($resultData["Data"]) ; $j++ )
{
$aRow = $resultData["Data"][$j];
$row = array();
$output["Success"]=true;
echo json_encode($output);
}
这里从 get 数据请求返回的数据table 看起来是这样的:
(chrome 开发者控制台)
您为 json 数组中的 iTotalDisplayRecords 返回了错误的值。它应该是过滤后的总记录数(即应用过滤后的记录总数 - 而不仅仅是此结果集中返回的记录数)
看到 documentation 并参考这个
https://datatables.net/forums/discussion/512/clarification-of-itotalrecords-and-itotaldisplayrecords
这是我的表格 html:
<table style="text-align: center" class="table table-striped table-bordered table-hover" id="usersTable">
<thead>
<tr>
<th>
User Name
</th>
<th>
Account
</th>
<th>
Enable
</th>
<th>
Remark
</th>
</tr>
</thead>
<tbody>
</tbody>
</table>
和数据table def:
var tableObject = $("#usersTable").dataTable({
"bProcessing": true,
"bServerSide": true,
"sAjaxSource": "../../Controller/UserManagementControllercopy.php5",
"aoColumns": [
{ "mDataProp": "0", "sWidth": "40%", "bSearchable": false },
{ "mDataProp": "1", "sWidth": "20%"},
{ "mDataProp": "3", "sWidth": "20%" },
{ "mDataProp": "2", "sWidth":"20%" }
],
"fnServerData": function (sSource, aoData, fnCallback){
$.ajax({
"dataType": "json",
"contentType": "application/json; charset=utf-8",
"type": "GET",
"url": sSource,
"data": aoData,
"success": function(result){
fnCallback(result);
},
error: function (xhr, textStatus, error){
debugger
if (typeof console == "object") {
console.log(xhr.status + "," + xhr.responseText + "," + textStatus + "," + error);
}
}});
},
"oLanguage": {
"sLengthMenu": '<select>' +
'<option value="5">5</option>' +
'<option value="10">10</option>' +
'<option value="20">20</option>' +
'<option value="30">30</option>' +
'<option value="40">40</option>' +
'<option value="50">50</option>' +
'</select> Show'
},
"fnDrawCallback": function(){
},
"aaSorting": [
[1, 'asc']
],
"aLengthMenu": [
[5, 15, 20, -1],
[5, 15, 20, "All"] ],
"iDisplayLength": 5
});
和服务器代码:
$aColumns = array( 'USERNAME', 'ACCOUNT', 'REMARK', 'ENABLE');
$sQuery = " SELECT USERNAME,ACCOUNT,REMARK,ENABLE FROM users LIMIT ".$_GET['iDisplayStart'].", ".$_GET['iDisplayLength'].";";
$dbObject = Connection::getConnection();
$request = $dbObject->dbh->prepare($sQuery);
if ($request->execute())
{
$resultData["Data"] = $request->fetchAll(PDO::FETCH_ASSOC);
$sQuery = "SELECT COUNT(USERNAME) FROM users";
$request = $dbObject->dbh->prepare($sQuery);
$request->execute();
$iTotal = $request->fetchColumn(0);
$output = array(
"sEcho" => intval($_GET['sEcho']),
"iTotalRecords" => intval($iTotal),
"iTotalDisplayRecords" => intval($_GET['iDisplayLength']),
"aaData" => array()
);
for ( $j=0 ; $j<count($resultData["Data"]) ; $j++ )
{
$aRow = $resultData["Data"][$j];
$row = array();
$output["Success"]=true;
echo json_encode($output);
}
这里从 get 数据请求返回的数据table 看起来是这样的: (chrome 开发者控制台)
您为 json 数组中的 iTotalDisplayRecords 返回了错误的值。它应该是过滤后的总记录数(即应用过滤后的记录总数 - 而不仅仅是此结果集中返回的记录数) 看到 documentation 并参考这个 https://datatables.net/forums/discussion/512/clarification-of-itotalrecords-and-itotaldisplayrecords