提问人:Warren Clarin 提问时间:9/30/2017 更新时间:4/6/2020 访问量:1216
DataTables服务器端处理:将两个DB列合并为一个DT
DataTables Server-side Processing: Joining two DB Column into One DT
问:
我是 DataTables 服务器端处理的新手。 如何使用服务器端脚本将两个数据库列 (db) 联接/合并到一个数据表列 (dt) 中?我试过了:
$columns = array(
array( 'db' => 'id', 'dt' => 'id' ),
array( 'db' => array('firstname', 'lastname'),'dt' => 'priest' )
);
而且它不起作用。什么是正确的?谢谢! 我正在使用 DataTables-1.10.16。
答:
0赞
Emre Tufekci
7/23/2018
#1
对不起,回答晚了,
实际上,在演示spss.php类中,您无法合并它们,但我们有一个解决方案, 这是使用“列渲染”
转到服务器端处理文件和 写一个类似的东西(16 只是假设如果你有 5 列,你应该写 6)
array( 'db' => 'database column', 'dt' => 16 )
然后去找客户端;
$(document).ready(function() {
$('#example').DataTable( {
"columnDefs": [
{
// The `data` parameter refers to the data for the cell (defined by the
// `data` option, which defaults to the column being worked with, in
"render": function ( data, type, row ) {
return data +' ('+ row[16]+')';
},
"targets": 11
},
//This is makes db 16 row nonvisible
{ "visible": false, "targets": [ 16 ] }
],
} );
} );
“目标”:11 表示我想在 11 中添加一些东西。列。
1赞
Labros kar
4/6/2020
#2
你也可以在服务器端这样做,然后在 js 中隐藏你不需要的列。 例如,假设您有一个包含 3 列的表:id、name 和 link。 要将链接合并到名称中,请执行以下操作:
在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].'">'.$d.'</a>';
}
),
array( 'db' => 'link', 'dt' => 2 )
);
评论