CSVWriter.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. define("dojox/grid/enhanced/plugins/exporter/CSVWriter", [
  2. "dojo/_base/declare",
  3. "dojo/_base/array",
  4. "./_ExportWriter",
  5. "../Exporter"
  6. ], function(declare, array, _ExportWriter, Exporter){
  7. Exporter.registerWriter("csv", "dojox.grid.enhanced.plugins.exporter.CSVWriter");
  8. return declare("dojox.grid.enhanced.plugins.exporter.CSVWriter", _ExportWriter, {
  9. // summary:
  10. // Export grid to CSV format.
  11. _separator: ',',
  12. _newline: "\r\n",
  13. constructor: function(/* object? */writerArgs){
  14. // summary:
  15. // CSV default separator is ','.
  16. // But we can also use our own.
  17. // writerArgs: object?
  18. // {separator:'...'}
  19. if(writerArgs){
  20. this._separator = writerArgs.separator ? writerArgs.separator : this._separator;
  21. this._newline = writerArgs.newline ? writerArgs.newline : this._newline;
  22. }
  23. this._headers = [];
  24. this._dataRows = [];
  25. },
  26. _formatCSVCell: function(/* string */cellValue){
  27. // summary:
  28. // Format cell value to follow CSV standard.
  29. // See: http://en.wikipedia.org/wiki/Comma-separated_values
  30. // tags:
  31. // private
  32. // cellValue: string
  33. // The value in a cell.
  34. // returns:
  35. // The formatted content of a cell
  36. if(cellValue === null || cellValue === undefined){
  37. return '';
  38. }
  39. var result = String(cellValue).replace(/"/g, '""');
  40. if(result.indexOf(this._separator) >= 0 || result.search(/[" \t\r\n]/) >= 0){
  41. result = '"' + result + '"';
  42. }
  43. return result; //String
  44. },
  45. beforeContentRow: function(/* object */arg_obj){
  46. // summary:
  47. // Overrided from _ExportWriter
  48. var row = [],
  49. func = this._formatCSVCell;
  50. array.forEach(arg_obj.grid.layout.cells, function(cell){
  51. //We are not interested in indirect selectors and row indexes.
  52. if(!cell.hidden && array.indexOf(arg_obj.spCols,cell.index) < 0){
  53. //We only need data here, not html
  54. row.push(func(this._getExportDataForCell(arg_obj.rowIndex, arg_obj.row, cell, arg_obj.grid)));
  55. }
  56. }, this);
  57. this._dataRows.push(row);
  58. //We do not need to go into the row.
  59. return false; //Boolean
  60. },
  61. handleCell: function(/* object */arg_obj){
  62. // summary:
  63. // Overrided from _ExportWriter
  64. var cell = arg_obj.cell;
  65. if(arg_obj.isHeader && !cell.hidden && array.indexOf(arg_obj.spCols,cell.index) < 0){
  66. this._headers.push(cell.name || cell.field);
  67. }
  68. },
  69. toString: function(){
  70. // summary:
  71. // Overrided from _ExportWriter
  72. var result = this._headers.join(this._separator);
  73. for(var i = this._dataRows.length - 1; i >= 0; --i){
  74. this._dataRows[i] = this._dataRows[i].join(this._separator);
  75. }
  76. return result + this._newline + this._dataRows.join(this._newline); //String
  77. }
  78. });
  79. });