SaveAsCsv.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. define( function() {
  2. "use strict";
  3. function SaveAsCsv()
  4. {
  5. };
  6. SaveAsCsv.prototype.draw = function( oControlHost )
  7. {
  8. oControlHost.container.innerHTML =
  9. '<style>' +
  10. '.MySaveButton' +
  11. '{' +
  12. 'background-color: white;' +
  13. 'border: 2px solid #4178BE;' +
  14. 'color: #4178be;' +
  15. 'font-size: 14px;' +
  16. 'height: 32px;' +
  17. 'width: 120px;' +
  18. 'margin-left: 20px;' +
  19. 'cursor: pointer;' +
  20. '} ' +
  21. '.MySaveButton:hover' +
  22. '{' +
  23. 'background-color: #4178BE;' +
  24. 'color: white;' +
  25. '}' +
  26. '</style>' +
  27. '<button class="MySaveButton btn1">Save CSV</button>';
  28. // oControlHost.container.firstChild.onclick = this.doSave.bind( this, oControlHost );
  29. oControlHost.container.querySelector( ".btn1" ).onclick = this.doSave.bind( this, oControlHost );
  30. };
  31. SaveAsCsv.prototype.doSave = function( oControlHost )
  32. {
  33. var bIsIE = ( ( ( navigator.userAgent.toLowerCase().search(/trident\/([0-9]+\.[0-9]+)/) != -1 ) ? parseFloat( RegExp.$1 ) : 0 ) >= 7.0 );
  34. if ( bIsIE )
  35. {
  36. alert( "This example does not support IE" );
  37. return;
  38. }
  39. var o = oControlHost.configuration;
  40. var oControl = ( o && o["Control name"] ) ? oControlHost.page.getControlByName( o["Control name"] ) : oControlHost.control;
  41. var oDataStore = ( o && o["Data store name"] ) ? [oControl.getDataStore( o["Data store name"] )] : oControl.dataStores[0];
  42. var sFileName = ( o ? o["File name"] : "" ) || "FileName";
  43. if ( !sFileName.match( /\.csv$/i ) )
  44. {
  45. sFileName += ".csv";
  46. }
  47. var elAnchor = document.createElement( "A" );
  48. elAnchor.style.display = "none";
  49. elAnchor.download = sFileName;
  50. elAnchor.href = "data:text/csv," + encodeURIComponent( this.f_getDataStoreAsCsv( oDataStore ) );
  51. document.body.appendChild( elAnchor );
  52. elAnchor.click();
  53. elAnchor.parentNode.removeChild( elAnchor );
  54. };
  55. SaveAsCsv.prototype.f_getDataStoreAsCsv = function( oDataStore )
  56. {
  57. var a = [];
  58. var aNames = oDataStore.columnNames;
  59. var iColumnsCount = aNames.length;
  60. for ( var i = 0; i < iColumnsCount; i++ )
  61. {
  62. if ( i > 0 )
  63. {
  64. a.push( "," );
  65. }
  66. this.pushCsvField( a, aNames[i] );
  67. }
  68. a.push( "\r\n" );
  69. var iRowCount = oDataStore.rowCount;
  70. for ( var iRow = 0; iRow < iRowCount; iRow++ )
  71. {
  72. for ( var iColumn = 0; iColumn < iColumnsCount; iColumn++ )
  73. {
  74. if ( iColumn > 0 )
  75. {
  76. a.push( "," );
  77. }
  78. this.pushCsvField( a, oDataStore.getCellValue( iRow, iColumn ) );
  79. }
  80. a.push( "\r\n" );
  81. }
  82. return a.join( "" );
  83. };
  84. SaveAsCsv.prototype.pushCsvField = function( a, sValue )
  85. {
  86. if ( typeof sValue != "string" )
  87. {
  88. a.push( sValue );
  89. return;
  90. }
  91. a.push( '"' + ( ( sValue.indexOf( '"' ) == -1 ) ? sValue : sValue.replace( /"/g, '""' ) ) + '"' );
  92. };
  93. return SaveAsCsv;
  94. });