php - 服务器端处理的DataTable上的"Column Index"

标签 php jquery indexing datatables server-side

有谁知道如何将列索引添加到服务器端处理的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/

相关文章:

php - 如何在 PHP 中测试连通性?

javascript - Ajax注册失败不回显结果

javascript - 使用PHP更改某些页面内容

javascript - 对于小于 30,000 的值,成本计算表单返回 NaN

jQuery 在所有浏览器中的 SlideUp() 上闪烁...附有示例页面

c# - RavenDb 检查索引是否存在

mysql - 向 MySQL 表添加索引

php - mysql select 在 for 循环内时检查 mysql 结果

javascript - 缩放内容以始终适合窗口

mysql - 使用 MySQL 唯一索引来防止重复,而不是重复搜索?