服务器端处理的DataTable上的“列索引”

有谁知道如何将列索引添加到服务器端处理的DataTable ? 基本上像http://www.datatables.net/examples/api/counter_columns.html ,但是此示例按客户端构建索引,使用公共服务器端版本不支持该索引。

作者艾伦给出了三个提示,但实际上我没有得到它:

  • 修改服务器上的数据(理想的解决方案)
  • 修改从服务器返回的数据
  • 编辑绘图回调函数以考虑页面开始位置。

我跌跌撞撞地跌跌撞撞 – 我不知道如何开始以及如何做到这一点。 你能帮我一下吗? 那将是真棒!

我指的是这里的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列:

    ID First name Last name etc ... 

这就是Allan所说的“ 修改数据,因为它从服务器返回 ”。 这有一些缺点。 使用服务器端处理排序时,在生成的sql查询中进行过滤和搜索,该查询从数据库中获取数据。 由于您似乎没有 db中的增量id字段: 没有为您排序ID,请回来一年!

这给我们带来了Allans的建议Nr.1“ 修改服务器上的数据(理想的解决方案) ”,这基本上意味着:给你的数据库一个递增的id并将它用作一个简单的字段。 这可以通过简单的更新查询来完成。 当然,如果你不想在行ID之后排序这个答案就行了。