2017-10-16 27 views
1

我想从两个不同列中的值呈现到我的jQuery的数据表如何将多列中的数据呈现为一列?

https://cdn.datatables.net/1.10.16/js/jquery.dataTables.min.js 
https://cdn.datatables.net/1.10.16/css/jquery.dataTables.min.css 

$(document).ready(function(){ 

    var table = $('#table').DataTable({ 

     "ajax": { 
       "url": "data.json", 
       "dataSrc": "", 
      }, 

     "columnDefs": [ 
      { 
       "render": function (data, type, row) { 
        var result = 'The id is ' + data[0] + ' and the name is ' + data[1]; 
         return result; 
        }, 
        "targets": 0, 
       },  

     ], 

     "columns": [ 
       { 
        "data": "id" 
       }, 
       { 
        "data": "name" 
       } 
      ] 
    }); 

}); 

data.json:

[{ 
    "id": "12", 
    "name": "Laura" 
}] 

但我的结果是:

The id is 12 and the name is undefined 
+1

它应该是'row [0]'而不是'data [0]'。 – markpsmith

+0

@markpsmith但是然后我的结果是:该ID是未定义的,名称未定义 – Jarla

+0

它应该是'var result ='该id是'+ row [“id”] +',名称是'+ row [“name “];' – juntapao

回答

2

请更改以下:

"columnDefs": [ 
      { 
       "render": function (data, type, row) { 
        var result = 'The id is ' + row["id"] + ' and the name is ' + row["name"]; 
        console.log(result); 
         return result; 
        }, 
        "targets": 0, 
       },  

     ] 

使用row["id"]而不是data[0];

+0

这工作! – Jarla

相关问题