2017-04-07 15 views
1

我有一項功能可以讓用戶導出數據庫信息。但是,如果用戶選擇導出所有選項,則需要1分鐘或更長時間才能下載.csv文件。我只包括if語句的一部分,我在這裏提取所有數據。如何使用javascript加快導出數據功能

那就是:

function exportTheData() { 
     //get the data for data array 
     if(exportVolumeData == 1) { 

      for(j=0; j<plantData1.length; j++) { 

       i = plantData["MergeKey_lvl00"].indexOf(plantData1["MergeKey_lvl00"][j]); 

       data.push(plantData["PlantName"][i]); 

       if(statesExport == 1) { 
        countyindex = counties["CountyId"].indexOf(plantData["LocationId"][i]); 
        stateid = counties["StateId"][countyindex]; 
        statename = states["StateName"][states["StateId"].indexOf(stateid)]; 

        data.push(statename); 
       } 
       if(countyExport == 1) { 
        countyindex = counties["CountyId"].indexOf(plantData["LocationId"][i]); 
        countyname = counties["CountyName"][countyindex]; 

        data.push(countyname); 
       } 
       if(basinsExport == 1) { 
        countyindex = counties["CountyId"].indexOf(plantData["LocationId"][i]); 
        subbasinid = counties["SubBasinId"][countyindex]; 
        subbasinindex = basinSub["SubBasinId"].indexOf(subbasinid); 
        basinid = basinSub["BasinId"][subbasinindex]; 
        basinindex = basin["BasinId"].indexOf(basinid); 
        basinname = basin["BasinName"][basinindex]; 

        data.push(basinname); 
       }   
       if(subBasinsExport == 1) { 
        countyindex = counties["CountyId"].indexOf(plantData["LocationId"][i]); 
        subbasinid = counties["SubBasinId"][countyindex]; 
        subbasinindex = basinSub["SubBasinId"].indexOf(subbasinid); 
        subbasinname = basinSub["SubBasinName"][subbasinindex]; 

        data.push(subbasinname); 
       }   
       if(paddsExport == 1) { 
        countyindex = counties["CountyId"].indexOf(plantData["LocationId"][i]); 
        subpaddid = counties["SubPaddId"][countyindex]; 
        subpaddindex = paddSub["SubPaddId"].indexOf(subpaddid); 
        paddid = paddSub["PaddId"][subpaddindex]; 
        paddindex = padd["PaddId"].indexOf(paddid); 
        paddname = padd["PaddName"][paddindex]; 

        data.push(paddname); 
       }   
       if(subPaddsExport == 1) { 
        countyindex = counties["CountyId"].indexOf(plantData["LocationId"][i]); 
        subpaddid = counties["SubPaddId"][countyindex]; 
        subpaddname = paddSub["SubPaddName"][paddSub["SubPaddId"].indexOf(subpaddid)]; 

        data.push(subpaddname); 
       }   
       if(fullNameExport == 1) { 
        companyindex = getCompanyInfo["MergeKey_lvl00"].indexOf(plantData["OperatorId"][i]); 
        fullname = getCompanyInfo["FullName"][companyindex]; 

        data.push(fullname); 
       }   
       if(shortNameExport == 1) { 
        companyindex = getCompanyInfo["MergeKey_lvl00"].indexOf(plantData["OperatorId"][i]); 
        shortname = getCompanyInfo["ShortName"][companyindex]; 

        data.push(shortname); 
       }   
       if(tickerExport == 1) { 
        companyindex = getCompanyInfo["MergeKey_lvl00"].indexOf(plantData["OperatorId"][i]); 
        ticker = getCompanyInfo["Ticker"][companyindex]; 

        data.push(ticker); 
       } 

       volumeindex = plantData1["MergeKey_lvl00"].indexOf(plantData["MergeKey_lvl00"][i]); 
       startdate = plantData1["MonthStartDate"][volumeindex]; 
       volumetypeindex = plantData2["VolumeTypeId"].indexOf(plantData1["VolumeTypeId"][j]); 
       volumetype = plantData2["VolumeType"][volumetypeindex]; 
       volumeunit = plantData2["Unit"][volumetypeindex]; 
       volume = plantData1["Volume"][volumeindex]; 

       data.push(startdate); 
       data.push(volumetype); 
       data.push(volumeunit); 
       data.push(volume); 
      } 

     /* * Convert our data to CSV string */ 
     var CSVString = prepCSVRow(titles, titles.length, ''); 
     CSVString = prepCSVRow(data, titles.length, CSVString); 
     /* * Make CSV downloadable*/ 
     var downloadLink = document.createElement("a"); 
     var blob = new Blob(["\ufeff", CSVString]); 
     var url = URL.createObjectURL(blob); 
     downloadLink.href = url; 
     downloadLink.download = "data.csv"; 
     /** Actually download CSV */ 
     document.body.appendChild(downloadLink); 
     downloadLink.click(); 
     document.body.removeChild(downloadLink); 
    } 

這裏是我的數據數組轉換爲csv字符串:

 function prepCSVRow(arr, columnCount, initial) { 
     var row = ''; // this will hold data 
     var delimeter = ','; // data slice separator, in excel it's `;`, in usual CSv it's `,` 
     var newLine = '\r\n'; // newline separator for CSV row 
    /* * Convert [1,2,3,4] into [[1,2], [3,4]] while count is 2 
     * @param _arr {Array} - the actual array to split 
     * @param _count {Number} - the amount to split 
     * return {Array} - splitted array */ 
     function splitArray(_arr, _count) { 
     var splitted = []; 
     var result = []; 
     _arr.forEach(function(item, idx) { 
      if ((idx + 1) % _count === 0) { 
      splitted.push('"' + item + '"'); 
      result.push(splitted); 
      splitted = []; 
      } else { 
      splitted.push('"' + item + '"'); 
      } 
     }); 
     return result; 
     } 
     var plainArr = splitArray(arr, columnCount); 
     // don't know how to explain this 
     // you just have to like follow the code 
     // and you understand, it's pretty simple 
     // it converts `['a', 'b', 'c']` to `a,b,c` string 
     plainArr.forEach(function(arrItem) { 
     arrItem.forEach(function(item, idx) { 
      row += item + ((idx + 1) === arrItem.length ? '' : delimeter); 
     }); 
     row += newLine; 
     }); 
     return initial + row; 
    } 

我如何能加快這你知道嗎?我們在數據庫中有超過6000行的數據。謝謝!

+2

http://papaparse.com/ – mkaatman

+0

考慮使用承諾(*異步JS *)?或者使用[Node-cluster](https://nodejs.org/api/cluster.html) –

+0

進行多處理您是否嘗試過http://codereview.stackexchange.com/? –

回答

1

將data.push ..更改爲data [data.length] = ..使功能更快。另外,我爲縣級索引和公司索引創建了變量,而不是在同一個函數中多次調用它。

而且,清理該函數的最後一部分確實幫助:

/* * Convert our data to CSV string */ 
var CSVString = prepCSVRow(titles, titles.length); 
CSVString += prepCSVRow(data, titles.length); 

/* * Make CSV downloadable*/ 
var downloadLink = document.createElement('a'), 
    blob   = new Blob(['\ufeff', CSVString]); 

downloadLink.href  = URL.createObjectURL(blob); 
downloadLink.download = 'data.csv'; 

/** Actually download CSV */ 
document.body.appendChild(downloadLink); 
downloadLink.click(); 
document.body.removeChild(downloadLink);