有谁知道如何将列索引添加到服务器端处理的DataTable ?基本上就像http://www.datatables.net/examples/api/counter_columns.html ,但本例是客户端建立索引,普通服务端版本不支持。
作者Allan给出了三个提示,但实际上我不明白:
- 修改服务器上的数据(理想的解决方案)
- 在服务器返回数据时修改数据
- 编辑绘图回调函数以考虑页面起始位置。
我跌跌撞撞 - 我不知道如何开始以及如何去做。你能帮我一下吗?那太棒了!
最佳答案
我在这里指的是 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 ...
这就是 Allan 所说的“在数据从服务器返回时修改数据”的意思。这有一些缺点。当使用服务器端处理排序时,过滤和搜索发生在生成的 SQL 查询中,该查询从数据库中获取数据。由于您的数据库中似乎没有增量 id 字段:不为您排序 ID,一年回来!
这给我们带来了 Allans 建议 Nr.1“修改服务器上的数据(理想的解决方案)”,这基本上意味着:为您的数据库提供一个递增的 id 并将其用作简单字段。这可以通过简单的更新查询来完成。当然,如果您不想在行 id 之后排序,这个答案就可以。
关于php - 服务器端处理的DataTable上的"Column Index",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23505230/