我有一个服务器端进行处理的数据表。 数据来自预期的表,但它不按数据表分页。我在调试总数记录中看到数据库aaData将显示为5, 所以我希望会有3页..但页面中只有1页显示和5行。
这是我的tabl 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>
和数据表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
});
和servercode:
$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);
}
这里如何返回来自datatable的get请求的数据: (chrome开发者控制台)
答案 0 :(得分:1)
您在json数组中为iTotalDisplayRecords返回了错误的值。它应该是过滤后的总记录(即应用过滤后的记录总数 - 而不仅仅是此结果集中返回的记录数) 请参阅documentation并参考此处 https://datatables.net/forums/discussion/512/clarification-of-itotalrecords-and-itotaldisplayrecords