我遵循这个tutorial在PHP,MSSQL和AJAX中应用数据表服务器端处理。
我根据我的要求调整了上述代码。这是我的索引页面
<!DOCTYPE html>
<html>
<title>Datatable</title>
<head>
<link rel="stylesheet" type="text/css" href="css/jquery.dataTables.css">
<script type="text/javascript" language="javascript" src="js/jquery.js"></script>
<script type="text/javascript" language="javascript" src="js/jquery.dataTables.js"></script>
<script type="text/javascript" language="javascript" >
$(document).ready(function() {
var dataTable = $('#grid').DataTable( {
"processing": true,
"serverSide": true,
"ajax":{
url :"grid-data.php",
type: "post",
error: function(){
$(".grid-error").html("");
$("#grid").append('<tbody class="employee-grid-error"><tr><th colspan="3">No data found in the server</th></tr></tbody>');
$("#grid_processing").css("display","none");
}
}
} );
} );
</script>
<style>
div.container {
margin: 0 auto;
max-width:760px;
}
div.header {
margin: 100px auto;
line-height:30px;
max-width:760px;
}
body {
background: #f7f7f7;
color: #333;
font: 90%/1.45em "Helvetica Neue",HelveticaNeue,Verdana,Arial,Helvetica,sans-serif;
}
</style>
</head>
<body>
<div class="header"><h1>DataTable demo</h1></div>
<div class="container">
<table id="grid" cellpadding="0" cellspacing="0" border="0" class="display" width="100%">
<thead>
<tr>
<th>Country</th>
<th>Territory</th>
<th>Market</th>
</tr>
</thead>
</table>
</div>
</body>
</html>
这是grid-data.php
<?php
$servername = "******";
$username = "******";
$password = "******";
$dbname = "mydatabase";
$dbDB = new PDO("odbc:Driver=ODBC Driver 13 for SQL Server;Server=******,1456;Database=mydatabase;Port=1456", $username, $password);
$requestData= $_REQUEST;
$columns = array(
0 =>'Country',
1 => 'Territory',
2=> 'Market' );
$sql = "SELECT Country, Territory, Market ";
$sql.=" FROM mydatabase.mytable";
$nRows = $dbDB->query("SELECT count(*) from mydatabase.mytable")->fetchColumn();
$totalData = $nRows;
$totalFiltered = $totalData;
$sql = "SELECT Country, Territory, Market ";
$sql.=" FROM mydatabase.mytable WHERE 1=1";
if( !empty($requestData['search']['value']) ) {
$sql.=" AND ( Country LIKE '".$requestData['search']['value']."%' ";
$sql.=" OR Territory LIKE '".$requestData['search']['value']."%' ";
$sql.=" OR Market LIKE '".$requestData['search']['value']."%' )";
}
$nRowsSearch = $dbDB->query($sql)->fetchColumn();
$totalFiltered = $nRowsSearch;
$sql.=" ORDER BY ". $columns[$requestData['order'][0]['column']]." ".$requestData['order'][0]['dir']." LIMIT ".$requestData['start']." ,".$requestData['length']." ";
$data = array();
$query = 'SELECT Country, Territory, Market FROM mydatabase.mytable WHERE 1=1';
foreach ($dbDB->query($query) as $row) {
$nestedData=array();
$nestedData[] = $row["Country"];
$nestedData[] = $row["Territory"];
$nestedData[] = $row["Market"];
$data[] = $nestedData;
}
$json_data = array(
"draw" => intval( $requestData['draw'] ),
"recordsTotal" => intval( $totalData ),
"recordsFiltered" => intval( $totalFiltered ),
"data" => $data
);
echo json_encode($json_data);
?>
数据在我的表中正确加载,但搜索和更改长度和排序等所有功能都无法正常工作。
你能告诉我在grid-data.php中缺少什么吗?非常感谢你的建议。
如果您正在寻求实现服务器端分页和更改长度,可能您应该发送您所在的页面,页面长度等。
确保将页码,页面长度传递给API,然后在服务器端写入一些逻辑,以仅选择页面长度的记录数并基于pagenumber。
如果您使用的是PHP,那么当您使用ssp.class.php
时,可以使用/examples/server_side/scripts
文件夹中提供的download DataTables library。
server_processing.php
文件夹中还有一个示例脚本/examples/server_side/scripts
,它显示了如何使用SSP类。
if(!empty($search)){
$sql ="SELECT * FROM t_penalty_list WHERE";
$sql.=" l_code LIKE '%{$search}%'";
$sql.=" OR cus_name LIKE '%{$search}%'";
$sql.=" OR cus_phone LIKE '%{$search}%'";
$sql.=" OR penalty_date LIKE '%{$search}%'";
}else{
$sql ="SELECT * FROM t_penalty_list WHERE 1=1";
if(!empty($is_status)){
if($is_status == 1){
$sql.=" AND amount_dept <= 0";
}elseif($is_status ==2){
$sql.=" AND amount_dept > 0";
}
$totalData = count($this->_penalty->selectRaw($sql));
}
}