我是新手在Jquery,我正在创建一个自定义按钮,以导出从数据库中的csv格式的数据只选定的行,问题是我不知道如何获得数据的ID,以便根据选定的ID,我可以使ajax调用PHP脚本导出选定的记录。我该怎么做呢?
我的数据表截图是,

代码是,
$('.dataTables-example').DataTable({
pageLength: 25,
"order": [[0, "desc"]],
"stateSave": true,
responsive: true,
dom: '<"html5buttons"B>lTfgitp',
buttons: [
'selectAll',
'selectNone',
{
text: 'Export Selected Rows',
action: function ( e, dt, node, config ) {
console.log(dt.row());
}, exportOptions: {
modifier: {
selected: true
}
}
}
],
select: true,
});因为我的代码的结果是,

发布于 2018-06-01 15:17:17
假设id是每行第一个单元格的文本,单击按钮时,按类搜索所选行(如果使用DataTables select插件,则所选行将具有selected类):
function onButtonClick() {
var id = $('#myTable')
.find('tbody')
.find('tr.selected')
.find('td')
.eq(0) // Get first cell
.text() // Get the text of cell
.trim(); // Trim the text
// Call your method providing the id
}HTML
<table id="myTable"></table>jQuery
// Your data
var data = [
{ ID: 0, ... },
{ ID: 1, ... },
{ ID: 2, ... },
....
];
// Initialize the plugin
$('#myTable').DataTable({
dom: '...',
data: data,
columns: [
{
data: 'ID',
title: 'ID'
},
...
],
buttons: [...]
});然后,单击按钮即可检索所选行的数据,如下所示:
var rowData = $('#myTable').DataTable().rows('.selected').data();您可以查看documentation
发布于 2018-06-01 17:00:14
你可以在你的导出按钮点击里面做下面的选择。它使用jquery each函数获取所选tr (具有selected类)的所有ids。
var data = {
'selected_ids': []
};
$('tbody tr.selected').each(function() {
data['selected_ids'][data['selected_ids'].length] = $(this).find('td:eq(0)').html();
});
console.log(data); // var data holds the selected ids. You can pass it in your ajax data as it is.https://stackoverflow.com/questions/50636760
复制相似问题