我使用jquery datatable绑定我的json数据并在行上添加按钮。现在,如何通过按钮单击获取列表中的特定列数据?我可以获得行值但无法找到获取列值的方法。
这是我的代码:
$.ajax({
url: "SympsService.asmx/GetSymptoms",
method: "post",
dataType: "json",
data: JSON.stringify({ organ_name: "toes" }),
contentType: "application/json",
success: function (data) {
var sympList = 'GetSymptoms' ? JSON.parse(data.d) : data.d;
createDataTable('#symptomsTable', sympList);
function createDataTable(target, data) {
$(target).DataTable({
destroy: true,
paging: false, searching: false, info: false, data: data,
columnDefs: [{
targets: [-1], render: function () {
return "<button type='button'>" + ('Choose') + "</button>"
}
}],
columns: [{
'data': 'Sympt',
'title': 'toes Symptoms',},
{'data': null, 'title': 'Action' }]
});
}
$('#symptomsTable').on("click", "tbody button", function () {
var id = $(this).closest("tr").find("td:nth-child(1)").data();
//here i get every row data by button click
//but i want specific column data on list
})
}
});
答案 0 :(得分:1)
使用Datatable内置函数DataTables Column Data column()。data()
答案 1 :(得分:0)
$('#table tbody').on('click', 'td', function () {
var index = $(this).index();
var column = $('#table').column(index);
});