123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104 |
- define( function() {
- "use strict";
- function SaveAsCsv()
- {
- };
- SaveAsCsv.prototype.draw = function( oControlHost )
- {
- oControlHost.container.innerHTML =
- '<style>' +
- '.MySaveButton' +
- '{' +
- 'background-color: white;' +
- 'border: 2px solid #4178BE;' +
- 'color: #4178be;' +
- 'font-size: 14px;' +
- 'height: 32px;' +
- 'width: 120px;' +
- 'margin-left: 20px;' +
- 'cursor: pointer;' +
- '} ' +
- '.MySaveButton:hover' +
- '{' +
- 'background-color: #4178BE;' +
- 'color: white;' +
- '}' +
- '</style>' +
- '<button class="MySaveButton btn1">Save CSV</button>';
-
-
- // oControlHost.container.firstChild.onclick = this.doSave.bind( this, oControlHost );
- oControlHost.container.querySelector( ".btn1" ).onclick = this.doSave.bind( this, oControlHost );
- };
- SaveAsCsv.prototype.doSave = function( oControlHost )
- {
- var bIsIE = ( ( ( navigator.userAgent.toLowerCase().search(/trident\/([0-9]+\.[0-9]+)/) != -1 ) ? parseFloat( RegExp.$1 ) : 0 ) >= 7.0 );
- if ( bIsIE )
- {
- alert( "This example does not support IE" );
- return;
- }
- var o = oControlHost.configuration;
- var oControl = ( o && o["Control name"] ) ? oControlHost.page.getControlByName( o["Control name"] ) : oControlHost.control;
- var oDataStore = ( o && o["Data store name"] ) ? [oControl.getDataStore( o["Data store name"] )] : oControl.dataStores[0];
- var sFileName = ( o ? o["File name"] : "" ) || "FileName";
- if ( !sFileName.match( /\.csv$/i ) )
- {
- sFileName += ".csv";
- }
- var elAnchor = document.createElement( "A" );
- elAnchor.style.display = "none";
- elAnchor.download = sFileName;
- elAnchor.href = "data:text/csv," + encodeURIComponent( this.f_getDataStoreAsCsv( oDataStore ) );
- document.body.appendChild( elAnchor );
- elAnchor.click();
- elAnchor.parentNode.removeChild( elAnchor );
- };
- SaveAsCsv.prototype.f_getDataStoreAsCsv = function( oDataStore )
- {
- var a = [];
- var aNames = oDataStore.columnNames;
- var iColumnsCount = aNames.length;
- for ( var i = 0; i < iColumnsCount; i++ )
- {
- if ( i > 0 )
- {
- a.push( "," );
- }
- this.pushCsvField( a, aNames[i] );
- }
- a.push( "\r\n" );
- var iRowCount = oDataStore.rowCount;
- for ( var iRow = 0; iRow < iRowCount; iRow++ )
- {
- for ( var iColumn = 0; iColumn < iColumnsCount; iColumn++ )
- {
- if ( iColumn > 0 )
- {
- a.push( "," );
- }
- this.pushCsvField( a, oDataStore.getCellValue( iRow, iColumn ) );
- }
- a.push( "\r\n" );
- }
- return a.join( "" );
- };
- SaveAsCsv.prototype.pushCsvField = function( a, sValue )
- {
- if ( typeof sValue != "string" )
- {
- a.push( sValue );
- return;
- }
- a.push( '"' + ( ( sValue.indexOf( '"' ) == -1 ) ? sValue : sValue.replace( /"/g, '""' ) ) + '"' );
- };
- return SaveAsCsv;
- });
|