我想使用datatable插件来处理大量数据。现在我想在数据表中实现"服务器端处理" server side scripting
我没有使用ajax,因此我无法理解如何通过此场景中的ajax文件获取表格中的记录。这是我的HTML和javascript:
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Untitled Document</title>
<link rel="stylesheet" type="text/css" href="css/jquery.dataTables.css">
<link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/1.10.16/css/jquery.dataTables.min.css">
<link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/scroller/1.4.3/css/scroller.dataTables.min.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() {
$('#example').DataTable( {
"processing": true,
"serverSide": true,
"ajax": "server_processing.php"
} );
} );
</script>
</head>
<body>
<body>
<div id="wrapper">
<table id="example">
<thead>
<tr>
<th>Id</th>
<th>Name</th>
<th>Position</th>
<th>Description</th>
</tr>
</thead>
</table>
</div>
</body>
</html>
在这里我制作了server_side脚本文件,无法理解该怎么做:
<?php
$link = mysqli_connect("localhost", "root", "", "phwdata");
$query = "select * from persons";
$query2 = mysqli_query($link,$query);
while($fetch= mysqli_fetch_assoc($query2)){;
$id = $fetch['id'];
$name = $fetch['name'];
$position = $fetch['position'];
$description = $fetch['description'];
}
?>
任何帮助都将受到高度赞赏。
答案 0 :(得分:0)
将服务器端代码更改为此并尝试:检查服务器端的示例转到Here并单击服务器端脚本并查看。
这将提供样本数据。
<?php
$link = mysqli_connect("localhost", "root", "", "phwdata");
$query = "select * from persons";
$query2 = mysqli_query($link,$query);
$num_rows=mysqli_num_rows($query2);
$columns=array("draw"=>1,"recordsTotal"=>$num_rows,"recordsFiltered"=>$num_rows);
while($fetch= mysqli_fetch_assoc($query2)){;
$columns["data"][]=array($fetch['id'],$fetch['name'],$fetch['position'],$fetch['description']);
}
echo json_encode($columns);
为了您的分页工作,请将服务器端代码更改为:
$table = 'persons';
// Table's primary key
$primaryKey = 'id';
$columns = array(
array( 'db' => 'id', 'dt' => 0 ),
array( 'db' => 'name', 'dt' => 1 ),
array( 'db' => 'position', 'dt' => 2 ),
array( 'db' => 'description', 'dt' => 3 ),
);
// SQL server connection information
$sql_details = array(
'user' => 'root',
'pass' => '',
'db' => 'phwdata',
'host' => 'localhost'
);
require( 'ssp.class.php' );// https://github.com/DataTables/DataTables/blob/master/examples/server_side/scripts/ssp.class.php
echo json_encode(
SSP::simple( $_GET, $sql_details, $table, $primaryKey, $columns )
);