NullPointer NullPointer - 5 months ago 34
PHP Question

How to change order of columns while exporting CSV data in Datatables?

I have TableTools file version like below

File: TableTools.min.js
Version: 2.1.4

I have a scenario like below


  1. I have a data-table with Export to Excel / CSV (from client side)

  2. In data table I need columns in the order 0,1,2,3,4 and so on

  3. While exporting into excel I need columns in the order 0,2,1,4,3.



Is that possible in Table Tools? If so could you please help me.

Edit
I have following code:

"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],
}
],
}
]
},

Answer

There isn't any existing feature available in Datatables that can re-order columns while exporting them.

So I had to write a PHP code at server side and had generate `CSV' file from it.