2017-10-16 15 views
1

私は複数の列のデータを1つの列にレンダリングするにはどうすればよいですか?

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 DataTableの私のjqueryのに二つの異なるカラムから値をレンダリングする:

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

しかし、私の結果は次のとおりです。

The id is 12 and the name is undefined 
+1

'data [0]'ではなく 'row [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, 
       },  

     ] 

data[0]の代わりにrow["id"]を使用してください。

+0

これは動作しています! – Jarla

関連する問題