我刚开始使用jQuery DataTables。
使用DataTables的tableTools,是否可以只导出可见行而不是所有行?例如,如果分页设置为10,我预计只会导出10行。搜索结果也是如此。
这是代码的一部分:
$(document).ready(function() {
var table = $('#example').DataTable({
"pagingType": "full_numbers",
"iDisplayLength" : 10,
dom: 'T<"clear">lfrtip',
"oTableTools": {
"aButtons": [
{ "sExtends": "copy", "mColumns": "visible", "bSelectedOnly": true },
{ "sExtends": "xls", "mColumns": "visible" },
{ "sExtends": "print", "mColumns": "visible" }
], "sRowSelect": "multi"},
"order": [[ 0, "asc" ]]
} ) ;...
谢谢。
您可以通过在保存之前选择所有可见行来实现该行为,然后在保存完成后取消选择它们。
$(document).ready(function() {
$('#example').DataTable( {
dom: 'T<"clear">lfrtip',
"oTableTools": {
"sRowSelect": "multi",
"aButtons": [
{
"sExtends": "csv",
"bSelectedOnly": true,
"fnComplete": function ( nButton, oConfig, oFlash, sFlash ) {
var oTT = TableTools.fnGetInstance( 'example' );
var nRow = $('#example tbody tr');
oTT.fnDeselect(nRow);
}
}
]
}
} );
$('a.DTTT_button_csv').mousedown(function(){
var oTT = TableTools.fnGetInstance( 'example' );
var nRow = $('#example tbody tr');
oTT.fnSelect(nRow);
});
} );
我使用这个解决方案,它工作。试试这个:
<script>
$(document).ready(function() {
var table = $('#example').DataTable( {
"pagingType": "full_numbers",
"iDisplayLength": 10,
"dom": 'T<"clear">lfrtip',
"oTableTools": {
"aButtons": [
{'sExtends':'copy',
"oSelectorOpts": { filter: 'applied', order: 'current' },
},
{'sExtends':'xls',
"oSelectorOpts": { filter: 'applied', order: 'current' },
},
{'sExtends':'print',
"oSelectorOpts": { filter: 'applied', order: 'current' },
}
]
},
});
});
</script>
您可以将页面选择设置为当前页面以进行特定导出。
参考:http://datatables.net/docs/DataTables/1.9.4/#$
{ "sExtends": "xls", "mColumns": "visible", "oSelectorOpts": { page: "current" } }
如果你使用flash导出,需要提到swf路径才能工作。
$("#example").dataTable( {
"sDom": 'T<"clear">lfrtip',
"oTableTools": {
"sSwfPath": "Path to your copy_csv_xls_pdf.swf files comes with TableTools",
"aButtons": [
{
"sExtends": "copy",
"sButtonText": "Copy to clipboard",
"oSelectorOpts": { filter: "applied", order: "current" }
},
{
"sExtends": "csv",
"sButtonText": "Export to CSV",
"oSelectorOpts": { filter: "applied", order: "current" }
},
{
"sExtends": "print",
"sButtonText": "Print",
"oSelectorOpts": { filter: "applied", order: "current" }
}
]
}
} );
aButtons对象也可以使用其他几个选项。
"mColumns": [1, 2,...] - List of columns to include in export result
"sTitle": "filename" - desire filename for export file
------------------更新---------------------------
在较新版本的datatable中 - datatableTools已停用
请使用按钮扩展名
buttons: [
{
extend: 'copyHtml5',
exportOptions: {
columns: [ 0, ':visible' ]
}
},
{
extend: 'excelHtml5',
exportOptions: {
columns: ':visible'
}
},
{
extend: 'pdfHtml5',
exportOptions: {
columns: [ 0, 1, 2, 5 ]
}
},
]