DataTables服务器端处理:将两个DB列连接到一个DT中

问题描述 投票:0回答:2

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

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

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

php mysql datatable server-side
2个回答
0
投票

对不起,我迟到了,

实际上,在演示spsss.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 ] }
        ],
    } );
} );

“ Targets :: 11表示我想在11列中添加一些内容。


0
投票

您也可以在服务器端进行操作,然后在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 )

);
© www.soinside.com 2019 - 2024. All rights reserved.