2016-10-10 17 views
0

私はすべてのテーブルにjqueryデータテーブルの魔法を与えるためにdatatablesを使用しています。 私は自分の反応テーブルにdata-titleを追加します。彼らはこのデータテーブルの属性を​​にデータテーブルに追加

<td data-title="Fruit">Apple</td> 
<td data-title="Good or bad">They are delicious</td> 

のように...

ように見えるので、どのように私はすべての私のtdのにデータ・タイトルを追加することができます私は現在、この

$(document).ready(function() { 
    $('#contacts').DataTable({ 
     "processing": true, 
     "serverSide": true, 
     "ajax": "src/data.php?form_action=get-table", 

    }); 
}); 

と私のリターンJSONのルックスを持っていますこの

{ 
"draw":"1", 
"recordsTotal":2, 
"recordsFiltered":2, 
"data":[ 
    [ 
    "Apples", 
    "They are delicious", 
    "2016-10-10 07:47:12", 
    "New entry", 
    "1" 
    ], 
    [ 
    "Bananas", 
    "They are also delicious", 
    "2016-10-10 07:47:12", 
    "New entry", 
    "2" 
    ] 
] 
} 

答えて

2

ようにあなたは、DataTableのにcreatedRowコールバックを使用することができます。このように、

$(document).ready(function() { 
    $('#contacts').DataTable({ 
     "processing": true, 
     "serverSide": true, 
     "ajax": "src/data.php?form_action=get-table", 
     // Per-row function to iterate cells 
     "createdRow": function (row, data, rowIndex) { 
      // Per-cell function to do whatever needed with cells 
      $.each($('td', row), function (colIndex) { 
       // For example, adding data-* attributes to the cell 
       $(this).attr('data-title', "your cell title"); 
      }); 
     } 
    }); 
}); 
関連する問題