[英]How can I create server-side processing datatables?
我试图根据此手册创建服务器端处理DataTable:
https://datatables.net/examples/server_side/simple.html
但是我没有从数据库中得到任何结果。
index.php:
<link rel="stylesheet" href="https://cdn.datatables.net/1.10.15/css/jquery.dataTables.min.css">
<table id="example" class="display" width="100%" cellspacing="0">
<thead>
<tr>
<th>id</th>
</tr>
</thead>
<tfoot>
<tr>
<th>id</th>
</tr>
</tfoot>
</table>
<script>
$(document).ready(function() {
$('#example').DataTable( {
"processing": true,
"serverSide": true,
"ajax": "server.php"
} );
} );
</script>
<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
<script src="https://cdn.datatables.net/1.10.15/js/jquery.dataTables.min.js"></script>
server.php:
<?php
// DB table to use
$table = 'elephants';
// Table's primary key
$primaryKey = 'id';
// Array of database columns which should be read and sent back to DataTables.
// The `db` parameter represents the column name in the database, while the `dt`
// parameter represents the DataTables column identifier. In this case simple
// indexes
$columns = array(
array( 'db' => 'id', 'dt' => 0 ),
);
// SQL server connection information
$sql_details = array(
'user' => 'root',
'pass' => 'root',
'db' => 'animals',
'host' => 'localhost'
);
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* If you just want to use the basic configuration for DataTables with PHP
* server-side, there is no need to edit below this line.
*/
require( 'ssp.class.php' );
echo json_encode(
SSP::simple( $_GET, $sql_details, $table, $primaryKey, $columns )
);
和ssp.class.php:
https://github.com/DataTables/DataTables/blob/master/examples/server_side/scripts/ssp.class.php
我不知道我做错了什么,但是我的结果仅仅是
ID
ID
您正在初始化代码,然后再添加相关的JS库,
尝试一下,
<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
<script src="https://cdn.datatables.net/1.10.15/js/jquery.dataTables.min.js"></script>
<script>
// Initialise after adding Jquery and Datatable plugin
$(document).ready(function() {
$('#example').DataTable( {
"processing": true,
"serverSide": true,
"ajax": "server.php"
});
});
</script>
尝试在$columns
数组中添加field
参数,例如,
$columns = array(
array( 'db' => 'id', 'dt' => 0, 'id' )
);
同样,您的数据表中有2列,但从服务器端仅返回1列。 因此,要么从HTML表中删除第二列,要么在$columns
数组中再添加1 $columns
。
阅读有关SSP的更多信息
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.