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

时间:2022-01-20 14:26:06

I just started out using jQuery DataTables.

我刚开始使用jQuery DataTables。

using the tableTools of DataTables, is it possible to only export visible rows instead of all the rows? If for example the pagination was set to 10 I would expect only 10 rows to be exported. The same goes for a search result.

使用DataTables的tableTools,是否可能只导出可见行而不是所有行?例如,如果分页被设置为10,那么我预计将只导出10行。搜索结果也是如此。

Here's part of the code:

以下是部分守则:

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

Thank you.

谢谢你!

4 个解决方案

#1


2  

You can achieve that behavior by selecting all visible rows before saving, then deselecting them after saving completed.

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

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

#2


19  

I used this solution and it worked. Try this:

我用了这个解,它起作用了。试试这个:

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

#3


3  

You may set the selection of the page to current page for specific export.

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

Ref: http://datatables.net/docs/DataTables/1.9.4/#$

裁判:http://datatables.net/docs/DataTables/1.9.4/ #美元

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

#4


3  

If you are using flash to export, need to mention swf path to work.

如果您正在使用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" }
            }
        ]
    }
} );

There are few additional options also available to aButtons object.

对于aButtons对象,也几乎没有其他选项可用。

"mColumns": [1, 2,...] - List of columns to include in export result

"sTitle": "filename" - desire filename for export file

------------------Update---------------------------

更新- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

In the newer version of datatable - datatableTools is retired

在新版本的datatable - datatableTools已退休。

Please use buttons extension

请使用按钮扩展

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

#1


2  

You can achieve that behavior by selecting all visible rows before saving, then deselecting them after saving completed.

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

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

#2


19  

I used this solution and it worked. Try this:

我用了这个解,它起作用了。试试这个:

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

#3


3  

You may set the selection of the page to current page for specific export.

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

Ref: http://datatables.net/docs/DataTables/1.9.4/#$

裁判:http://datatables.net/docs/DataTables/1.9.4/ #美元

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

#4


3  

If you are using flash to export, need to mention swf path to work.

如果您正在使用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" }
            }
        ]
    }
} );

There are few additional options also available to aButtons object.

对于aButtons对象,也几乎没有其他选项可用。

"mColumns": [1, 2,...] - List of columns to include in export result

"sTitle": "filename" - desire filename for export file

------------------Update---------------------------

更新- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

In the newer version of datatable - datatableTools is retired

在新版本的datatable - datatableTools已退休。

Please use buttons extension

请使用按钮扩展

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