Jquery - DataTables [tableTools]:仅导出可见行

时间:2014-05-28 12:49:37

标签: jquery datatables jquery-datatables tabletools

我刚开始使用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" ]]
    } ) ;...

谢谢。

4 个答案:

答案 0 :(得分:19)

我使用了这个解决方案并且有效。试试这个:

<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>

答案 1 :(得分:3)

您可以将页面选择设置为当前页面以进行特定导出。

参考:http://datatables.net/docs/DataTables/1.9.4/#$

{ "sExtends": "xls", "mColumns": "visible", "oSelectorOpts": { page: "current" } }

答案 2 :(得分:3)

如果您使用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

------------------更新--------------------------- < / p>

在较新版本的datatable中 - datatableTools已停用

请使用按钮扩展

buttons: [
        {
            extend: 'copyHtml5',
            exportOptions: {
                columns: [ 0, ':visible' ]
            }
        },
        {
            extend: 'excelHtml5',
            exportOptions: {
                columns: ':visible'
            }
        },
        {
            extend: 'pdfHtml5',
            exportOptions: {
                columns: [ 0, 1, 2, 5 ]
            }
        },
    ]

答案 3 :(得分:2)

您可以通过在保存之前选择所有可见行来实现该行为,然后在保存完成后取消选择它们。

$(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);
    });
} );