我在这里指的是 simple.html 示例。
在 server_processing.php 中,将最后几行替换为:
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* 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' );
$result=SSP::simple( $_GET, $sql_details, $table, $primaryKey, $columns );
$start=$_REQUEST['start'];
$idx=0;
foreach($result['data'] as &$res){
$res[0]=(string)$start;
$start++;
$idx++;
}
echo json_encode($result);
这将为返回的数组生成一个 row-id。此外,您还需要将 $colums 数组中的数字移动 1,因为 id 插入到位置 0 处。
$columns = array(
array( 'db' => 'first_name', 'dt' => 1 ),
array( 'db' => 'last_name', 'dt' => 2 ),
array( 'db' => 'position', 'dt' => 3 ),
etc ...
最后,您需要在 html 中添加一个额外的 id 列:
<thead>
<tr>
<th>ID</th>
<th>First name</th>
<th>Last name</th>
etc ...
这就是艾伦所说的“修改从服务器返回的数据“。这有一些缺点。当使用服务器端处理排序时,过滤和搜索发生在生成的 SQL 查询中,该查询从数据库中获取数据。因为您的数据库中似乎没有增量 id 字段:不给你整理ID,一年回来!
这给我们带来了艾伦的建议 Nr.1“在服务器修改数据(理想的解决方案)“这基本上意味着:给你的数据库一个递增的id并将其用作一个简单的字段。这可以通过一个简单的更新查询来完成。当然,如果你不想在行id之后排序,这个答案就可以了。