| 1 |
define([ |
| 2 |
"dojo/_base/declare", |
| 3 |
"dojo/_base/lang", |
| 4 |
"dojo/_base/array", |
| 5 |
"./_ExportWriter", |
| 6 |
"../Exporter" |
| 7 |
], function(declare, lang, array, _ExportWriter, Exporter){ |
| 8 |
|
| 9 |
Exporter.registerWriter("csv", "dojox.grid.enhanced.plugins.exporter.CSVWriter"); |
| 10 |
|
| 11 |
return declare("dojox.grid.enhanced.plugins.exporter.CSVWriter", _ExportWriter, { |
| 12 |
|
| 13 |
|
| 14 |
_separator: ',', |
| 15 |
|
| 16 |
_newline: "\r\n", |
| 17 |
|
| 18 |
constructor: function(writerArgs){ |
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
if(writerArgs){ |
| 25 |
this._separator = writerArgs.separator ? writerArgs.separator : this._separator; |
| 26 |
this._newline = writerArgs.newline ? writerArgs.newline : this._newline; |
| 27 |
} |
| 28 |
this._headers = []; |
| 29 |
this._dataRows = []; |
| 30 |
}, |
| 31 |
|
| 32 |
_formatCSVCell: function(cellValue){ |
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
if(cellValue === null || cellValue === undefined){ |
| 43 |
return ''; |
| 44 |
} |
| 45 |
var result = String(cellValue).replace(/"/g, '""'); |
| 46 |
if(result.indexOf(this._separator) >= 0 || result.search(/[" \t\r\n]/) >= 0){ |
| 47 |
result = '"' + result + '"'; |
| 48 |
} |
| 49 |
return result; |
| 50 |
}, |
| 51 |
|
| 52 |
beforeContentRow: function(arg_obj){ |
| 53 |
|
| 54 |
|
| 55 |
var row = [], |
| 56 |
func = lang.hitch(this, this._formatCSVCell); |
| 57 |
array.forEach(arg_obj.grid.layout.cells, function(cell){ |
| 58 |
|
| 59 |
if(!cell.hidden && array.indexOf(arg_obj.spCols,cell.index) < 0){ |
| 60 |
|
| 61 |
row.push(func(this._getExportDataForCell(arg_obj.rowIndex, arg_obj.row, cell, arg_obj.grid))); |
| 62 |
} |
| 63 |
}, this); |
| 64 |
this._dataRows.push(row); |
| 65 |
|
| 66 |
return false; |
| 67 |
}, |
| 68 |
|
| 69 |
handleCell: function(arg_obj){ |
| 70 |
|
| 71 |
|
| 72 |
var cell = arg_obj.cell; |
| 73 |
if(arg_obj.isHeader && !cell.hidden && array.indexOf(arg_obj.spCols,cell.index) < 0){ |
| 74 |
this._headers.push(cell.name || cell.field); |
| 75 |
} |
| 76 |
}, |
| 77 |
|
| 78 |
toString: function(){ |
| 79 |
|
| 80 |
|
| 81 |
var result = this._headers.join(this._separator); |
| 82 |
for(var i = this._dataRows.length - 1; i >= 0; --i){ |
| 83 |
this._dataRows[i] = this._dataRows[i].join(this._separator); |
| 84 |
} |
| 85 |
return result + this._newline + this._dataRows.join(this._newline); |
| 86 |
} |
| 87 |
}); |
| 88 |
}); |