我是 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/