“列索引”在服务器端处理的DataTable [英] "Column Index" on a server-side processed DataTable

查看:194
本文介绍了“列索引”在服务器端处理的DataTable的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Does anyone know how to add a column index to a server-side processed DataTable? Basically like http://www.datatables.net/examples/api/counter_columns.html, but this example builds the index by client, which is not supported by using the common server-side version.

作者Allan提供了三个提示,但实际上我不明白:

The author Allan gave three hints, but actually I don't get it:


  • 修改服务器上的数据(理想的解决方案)

  • 修改从服务器返回的数据

  • 编辑绘制回调函数以考虑页面启动位置。

  • Modify the data at the server (the ideal solution)
  • Modify the data as it comes back from the server
  • Edit the draw callback function to take into account the page start position.

我跌跌撞撞 - 我不知道如何开始以及如何做到这一点。你能帮我一下吗?那太棒了!

I stumble and stumble - and I don't know how to start and how to do it. Maybe YOU could help me out? That would be awesome!

推荐答案

我指的是这里的simple.html示例。

I am referring to the simple.html example here.

在server_processing.php中用以下代码替换最后一行:

In server_processing.php replace the last lines with:

    /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
     * 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);

这将为返回的数组生成一个行id。此外,您需要将$ colums数组中的数字移动1,因为id插入位置0。

This will generate an row-id to the array that is returned. Also you need to shift the number in the $colums array by 1 since the id is inserted at position 0.

$columns = array(
    array( 'db' => 'first_name', 'dt' => 1 ),
    array( 'db' => 'last_name',  'dt' => 2 ),
    array( 'db' => 'position',   'dt' => 3 ),
    etc ...

最后你需要在你的html中添加一个额外的id列:

Finally you need to add an additional id column to your html:

    <thead>
        <tr>
            <th>ID</th>
            <th>First name</th>
            <th>Last name</th>
            etc ...

这就是Allan对修改从服务器返回的数据。这有一些缺点。当使用服务器端处理排序时,过滤和搜索发生在生成的SQL查询中,从数据库中获取数据。在你的数据库中似乎没有增量的id字段:没有为你排序ID,回来一年!

That is what Allan meant with "Modify the data as it comes back from the server". And this has some drawbacks. When using server sided processing sorting, filtering and searching happens in the genrated sql query that fetches the data from your db. Since you don't seem to have an incremental id field in you db: No sorting the ID for you, come back one year!

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

This brings us to Allans suggestion Nr.1 "Modify the data at the server (the ideal solution)" which basically means: Give your db an incrementing id and use it as a simple field. This can be done with a simple update query. Of course if you don't want to sort after row id this answer will do.

这篇关于“列索引”在服务器端处理的DataTable的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆