Javascript 将数据标题属性添加到我的<;td>;在数据表中
我使用datatables为我的所有表赋予jquery datatables魔力, 我通过在td中添加数据标题来创建响应表。如何将数据标题添加到我的所有td中,使其看起来像这样Javascript 将数据标题属性添加到我的<;td>;在数据表中,javascript,jquery,datatable,Javascript,Jquery,Datatable,我使用datatables为我的所有表赋予jquery datatables魔力, 我通过在td中添加数据标题来创建响应表。如何将数据标题添加到我的所有td中,使其看起来像这样 <td data-title="Fruit">Apple</td> <td data-title="Good or bad">They are delicious</td> { "draw":"1", "recordsTotal":2, "recordsFiltered"
<td data-title="Fruit">Apple</td>
<td data-title="Good or bad">They are delicious</td>
{
"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"
]
]
}
我的返回json如下所示
<td data-title="Fruit">Apple</td>
<td data-title="Good or bad">They are delicious</td>
{
"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"
]
]
}
您可以使用datatables
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");
});
}
});
});
您可以使用datatables
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");
});
}
});
});