CSVWriter.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. /*
  2. Copyright (c) 2004-2012, The Dojo Foundation All Rights Reserved.
  3. Available via Academic Free License >= 2.1 OR the modified BSD license.
  4. see: http://dojotoolkit.org/license for details
  5. */
  6. if(!dojo._hasResource["dojox.grid.enhanced.plugins.exporter.CSVWriter"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code.
  7. dojo._hasResource["dojox.grid.enhanced.plugins.exporter.CSVWriter"] = true;
  8. dojo.provide("dojox.grid.enhanced.plugins.exporter.CSVWriter");
  9. dojo.require("dojox.grid.enhanced.plugins.exporter._ExportWriter");
  10. dojox.grid.enhanced.plugins.Exporter.registerWriter("csv",
  11. "dojox.grid.enhanced.plugins.exporter.CSVWriter");
  12. dojo.declare("dojox.grid.enhanced.plugins.exporter.CSVWriter",
  13. dojox.grid.enhanced.plugins.exporter._ExportWriter, {
  14. // summary:
  15. // Export grid to CSV format.
  16. _separator: ',',
  17. _newline: "\r\n",
  18. constructor: function(/* object? */writerArgs){
  19. // summary:
  20. // CSV default separator is ','.
  21. // But we can also use our own.
  22. // writerArgs: object?
  23. // {separator:'...'}
  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. _formatCSVCell: function(/* string */cellValue){
  32. // summary:
  33. // Format cell value to follow CSV standard.
  34. // See: http://en.wikipedia.org/wiki/Comma-separated_values
  35. // tags:
  36. // private
  37. // cellValue: string
  38. // The value in a cell.
  39. // returns:
  40. // The formatted content of a cell
  41. if(cellValue === null || cellValue === undefined){
  42. return '';
  43. }
  44. var result = String(cellValue).replace(/"/g, '""');
  45. if(result.indexOf(this._separator) >= 0 || result.search(/[" \t\r\n]/) >= 0){
  46. result = '"' + result + '"';
  47. }
  48. return result; //String
  49. },
  50. beforeContentRow: function(/* object */arg_obj){
  51. // summary:
  52. // Overrided from _ExportWriter
  53. var row = [],
  54. func = this._formatCSVCell;
  55. dojo.forEach(arg_obj.grid.layout.cells, function(cell){
  56. //We are not interested in indirect selectors and row indexes.
  57. if(!cell.hidden && dojo.indexOf(arg_obj.spCols,cell.index) < 0){
  58. //We only need data here, not html
  59. row.push(func(this._getExportDataForCell(arg_obj.rowIndex, arg_obj.row, cell, arg_obj.grid)));
  60. }
  61. }, this);
  62. this._dataRows.push(row);
  63. //We do not need to go into the row.
  64. return false; //Boolean
  65. },
  66. handleCell: function(/* object */arg_obj){
  67. // summary:
  68. // Overrided from _ExportWriter
  69. var cell = arg_obj.cell;
  70. if(arg_obj.isHeader && !cell.hidden && dojo.indexOf(arg_obj.spCols,cell.index) < 0){
  71. this._headers.push(cell.name || cell.field);
  72. }
  73. },
  74. toString: function(){
  75. // summary:
  76. // Overrided from _ExportWriter
  77. var result = this._headers.join(this._separator);
  78. for(var i = this._dataRows.length - 1; i >= 0; --i){
  79. this._dataRows[i] = this._dataRows[i].join(this._separator);
  80. }
  81. return result + this._newline + this._dataRows.join(this._newline); //String
  82. }
  83. });
  84. }