如何在Datatables中导出CSV数据时更改列的顺序?

时间:2016-06-21 07:27:58

标签: php csv datatables

我有TableTools文件版本,如下所示

文件:TableTools.min.js 版本:2.1.4

我的情况如下

  1. 我有一个数据表,其中包含导出到Excel / CSV(从客户端)
  2. 在数据表中,我需要0,1,2,3,4等等的列
  3. 导出到excel时,我需要的顺序为0,2,1,4,3。
  4. 表工具可以吗?如果是这样,请帮助我。

    修改 我有以下代码:

    "oTableTools": {
        "sSwfPath": "path/to/swf/copy_csv_xls_pdf.swf",
        "aButtons": [
            {
                "sExtends":    "collection",
                "sButtonText": "Export",
                "aButtons": [
                    {
                        "sExtends": "csv",
                        "sFileName": "Sample.csv",
                        "oSelectorOpts": {
                                page: 'current'
                        },
                        "mColumns": [1, 2, 3, 4, 5, 6, 7, 8, 9, 12, 13, 14, 15, 16],
                        "fnClick": function (nButton, oConfig, oFlash) {
                            //$(".DTTT_button_xls").trigger('click');
                            oTable.colReorder.order( [ 1, 2, 3, 4, 16, 6, 7, 8, 9, 12, 13, 14, 15 ] );
                        }
                    },
                    {
                        "sExtends": "xls",
                        "sFileName": "Sample.xls",
                        "oSelectorOpts": {
                            page: 'current'
                        },
                        "mColumns": [1, 2, 3, 4, 5, 6, 7, 8, 9, 12, 13, 14, 15, 16],
                    }
                ],
            }
        ]
    },
    

2 个答案:

答案 0 :(得分:0)

$("#ExportButton").on("Click", function (e) {
    table.colReorder.order( [ 0,2,1,4,3 ] );
} );

答案 1 :(得分:0)

Datatables中没有任何可用的功能可以在导出时重新排序列。

所以我必须在服务器端编写一个PHP代码并从中生成“CSV”文件。