将复选框的值导出到 jQuery 数据表中的 excel
export value of checkboxes to excel in jQuery datatables
我正在使用 jQuery Datatables pluging to view some data and I have in my table two columns containing checkboxes, I've tried to export the data from the table to an XSLX file using excel and excelHtml5 和 excelFlash 按钮,但文件中有两个空列,我还在我的项目中包含了 JSZip 插件,但没有用。如何在我的文件中将这些复选框的值作为布尔值获取。
SOLUTION
您正在使用 DataTables 1.10.8。在此版本(1.10.7 及更早版本)之前,有带有 fnCellRender
选项的 TableTools 可以帮助您执行所需的操作。自 1.10.8 起,TableTools 扩展已替换为 Buttons 扩展。
使用按钮扩展,您可以在执行排序时使用 exportOptions
and tell DataTables that you want the the data used for sorting (orthogonal: 'sort'
). Then you need to define render
函数和 return 适当的数据 (type === 'sort'
)。
作为副作用,这将使您的复选框列也可排序。
var table = $('#example1').DataTable({
dom : 'Bfrtlip',
buttons: [
{
extend: 'excel',
exportOptions: {
orthogonal: 'sort'
}
}
],
columnDefs: [{
targets:[0,5],
render: function(data, type, row, meta){
if(type === 'sort'){
var $input = $(data).find('input[type="checkbox"]').addBack();
data = ($input.prop('checked')) ? "1" : "0";
}
return data;
}
}]
});
DEMO
有关代码和演示,请参阅 this jsFiddle。
我正在使用 jQuery Datatables pluging to view some data and I have in my table two columns containing checkboxes, I've tried to export the data from the table to an XSLX file using excel and excelHtml5 和 excelFlash 按钮,但文件中有两个空列,我还在我的项目中包含了 JSZip 插件,但没有用。如何在我的文件中将这些复选框的值作为布尔值获取。
SOLUTION
您正在使用 DataTables 1.10.8。在此版本(1.10.7 及更早版本)之前,有带有 fnCellRender
选项的 TableTools 可以帮助您执行所需的操作。自 1.10.8 起,TableTools 扩展已替换为 Buttons 扩展。
使用按钮扩展,您可以在执行排序时使用 exportOptions
and tell DataTables that you want the the data used for sorting (orthogonal: 'sort'
). Then you need to define render
函数和 return 适当的数据 (type === 'sort'
)。
作为副作用,这将使您的复选框列也可排序。
var table = $('#example1').DataTable({
dom : 'Bfrtlip',
buttons: [
{
extend: 'excel',
exportOptions: {
orthogonal: 'sort'
}
}
],
columnDefs: [{
targets:[0,5],
render: function(data, type, row, meta){
if(type === 'sort'){
var $input = $(data).find('input[type="checkbox"]').addBack();
data = ($input.prop('checked')) ? "1" : "0";
}
return data;
}
}]
});
DEMO
有关代码和演示,请参阅 this jsFiddle。