我一直在做很多关于为什么我的服务器端脚本不会处理分页的研究。
我已经看过很多帖子,其中人们有同样的问题,而且这通常是由脚本提供的JSON响应引起的:
这是我服务器端脚本的json数据响应。
//set at the top of file
$requestData= $_REQUEST;
$json_data = array(
"draw" => intval( $requestData['draw'] ),
"recordsTotal" => intval( $totalData ),
"recordsFiltered" => intval( $totalFiltered ),
"data" => $Data
);
这是我对DataTable的初始化
var table = $('#table_id').DataTable({
"processing": true,
"serverSide": true,
scrollY: 500,
paging: true,
"pagingType": "full_numbers",
"iDisplayLength": "50",
"order": [[0,'asc'],[1,'asc'],[2,'asc']],
"ajax": {
url : "php/Inventory/Actions/LoadCatalog.php?Commodity="+Commodity+"&Make="+Make,
type: "post", // method , by default get
error: function(){ // error handling
}
},
"aoColumns":[
{"mData": "Commodity"},
{"mData": "Make"},
{"mData": "PartNumber"},
{"mData": "Description"},
{"mData": "Condition", "sClass": "myCenter"},
{"mData": "Qty", "sClass": "myCenter"},
{"mData": "From"}
],
"bFilter": false,
"fnDrawCallback": function (oSettings) {
//alert( 'DataTables has redrawn the table' );
console.log(oSettings.json);
LoopData();
},
error: function (xhr, error, thrown) {
alert(error);
}
});
现在我第一次装表,每件事似乎都很好...... 我的JSON回复:
draw : 1
recordsFiltered : 3059
recordsTotal : 3059
表格的页脚显示
Showing 1 to 50 of 3,059 entries
现在!如果我点击下一个按钮,它就会崩溃。
draw : 2
recordsFiltered : 3059
recordsTotal : 3059
我的页脚:
Showing 0,501 to 3,059 of 3,059 entries
此时我无法再点击下一步。 (它不会做任何事......) 我可以点击第一个和上一个按钮,它将返回到第一页,一切看起来都没问题。
要澄清一下,在我的服务器端脚本中我设置$ totalData和$ totalFiltered就像这样
//$sql = my selector statement with all the total records (3,5059)
$sql = "
SELECT `Inventory`.`Commodity`, `Inventory`.`Make`,
`Inventory`.`PartNumber`, `Inventory`.`Model`,
`Inventory`.`Condition`,SUM(`Inventory`.`Qty`) as `SumQty`,
GROUP_CONCAT(DISTINCT `Contacts`.`Name` SEPARATOR '<hr>') as `From`
FROM `Inventory`
INNER JOIN `Purchasing` ON `Inventory`.`JobNumber` =
`Purchasing`.`JobNumber`
INNER JOIN `Contacts` ON `Purchasing`.`Vendor` = `Contacts`.`id`
WHERE `Inventory`.`Saved` = '1' AND `Purchasing`.`Status` != 'Pending'
GROUP BY `Inventory`.`Commodity`, `Inventory`.`Make`,
`Inventory`.`PartNumber`, `Inventory`.`Model`, `Inventory`.`Condition`
";
$InvData=mysql_query($sql);
$totalData = mysql_num_rows($InvData);
//then again i update my sql, including my filters and WITHOUT limit set
// my filters are nothing - i dont do a search or any other kind filter so
the data will be the same | total records (3,5059)
$InvData=mysql_query($sql)
$totalFiltered = mysql_num_rows($InvData);