php - DataTables服务器端处理: Joining two DB Column into One DT

标签 php mysql datatable server-side

我是 DataTables 服务器端处理的新手。 如何使用服务器端脚本将两个数据库列 (db) 加入/组合到一个数据表列 (dt) 中?我尝试过:

$columns = array(
    array( 'db' => 'id', 'dt' => 'id' ),
    array( 'db' => array('firstname', 'lastname'),'dt' => 'priest' )
);

而且它不起作用。什么是正确的做法?谢谢你! 我正在使用 DataTables-1.10.16。

最佳答案

您也可以在服务器端执行此操作,然后在 js 中隐藏您不需要的列。 例如,假设您有一个包含 3 列的表:id、名称和链接。 要将链接合并到名称中,请执行以下操作:

在 html(页眉和页脚)中显示两列(我们将在 JavaScript 中动态隐藏该列):

<th>#</th><!--this is column 0 with id-->
<th>name</th><!--this is column 1 with name including a tag-->
<th>link</th><!--this is column 2 with link-->

在 JavaScript 中:

$(document).ready(function() {
     $('#table_id').DataTable( 
        "processing": true,
        "serverSide": true,
        "ajax": {
            "url": "ajax.php"
        },
        "order": [[ 0, "asc" ]],//ordering by the id (usefull)
        {"columnDefs": [
            { "visible": false,  "targets": [ 0 ] },//hiding the id
            { "visible": false,  "targets": [ 2 ] }//hiding the link column
        ]
     });
});

在 ajax php 脚本中还描述了这两列:

$columns = array(
    array( 'db' => 'id',  'dt' => 0),
    array( 
        'db' => 'name',
        'dt' => 1,
        'formatter' => function( $d, $row ) {
            return '<a href="'.$row[2].'" target="_blank">'.$d.'</a>';
        }
    ),
    array( 'db' => 'link',   'dt' => 2 )

);

关于php - DataTables服务器端处理: Joining two DB Column into One DT,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46500878/

相关文章:

jquery - 如何在 Bootstrap 数据表插件上对日期格式 d/m/y 进行排序?

PHP:从数组中选择 child

php - 有没有办法将 "obfuscatd"文本插入 MySql,这样我就无法读取我的用户存储在其中的内容?

php - 忽略 null(空)值 mysql SELECT AVG

javascript - 如何使用将响应发送回ajax并单独处理每个响应

jsf - 如何在 h :dataTable alternate color? 中生成行

mysql - 这个查询在做什么?

PHP-需要第一个表中的前3个json数据和第二个表中的所有数据

php - 拆分多选

asp.net - 将数据表解析为 XML 文档