2016-10-10 52 views
0

我使用数据表为我的所有表提供了jquery datatables magic, 我通过向我的td添加数据标题来做我的响应表。我怎么可以添加数据标题我所有的TD的,使他们看起来像这样在数据表中添加数据标题属性到我的​​

<td data-title="Fruit">Apple</td> 
<td data-title="Good or bad">They are delicious</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

你可以使用数据表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"); 
      }); 
     } 
    }); 
}); 
相关问题