0

Hi Guys the export works but i dont have a heading :/

i dont know what is my Problem Information: i'm getting my data from a database it's a Json Array

Can someone help me?

My Code:

 var csvContent = "data:text/csv;charset=utf-8,";
 $("#csv").click(function(){
 // Iterating through all the objects
 data.forEach(function (infoArray, index) {
 // Fetching all keys of a single object
 var _keys = Object.keys(infoArray);
 var dataString = [];
//Test for my heading 
 // var heading = ["Timestamp ; ToolVersion ; MonitorType ; SerialNumber; MonitorRevision ; TestScript ; TestCase ;TestCaseVersion ;TestScope; Duration; Result; ClickCount ;MorbalWaitAverage; MorbalWaitMin ; MorbalWaitMax ; TimingProblems ; AbortedRetries ; OperationRetries ; AcknowledgeAverage ; IncreasedUpdateTime ; FalseScrolls "];
 //dataString.push(heading);
 if(index==0){
 [].forEach.call(_keys, function(inst, i){
 dataString.push(inst);
 });
 dataString = dataString.join(";");
 csvContent += index < data[0].length ? dataString + "\n" : dataString;
 dataString = [];
 } else{
 [].forEach.call(_keys, function(inst, i){
 dataString.push(infoArray[inst]);
 });
 dataString = dataString.join(";");
 csvContent += index < data.length ? dataString + "\n" : dataString;
 }
 });
 var encodedUri = encodeURI(csvContent);
 var link = document.createElement("a");
 link.setAttribute("href", encodedUri);
 link.setAttribute("download", "my_data.csv");
 link.click(); 
 });
asked May 9, 2016 at 12:05
4
  • //dataString.push(heading); is commented out, so you're not using the headers at all in this piece of code. Commented May 9, 2016 at 12:10
  • no the heading is in my data.. that was only a test Commented May 9, 2016 at 12:16
  • 1
    Then you have to specify how you want the headers to appear in the csv file.You'll want to add each header either when pushing to the dataString, or later when building up the csvContent; Commented May 9, 2016 at 12:19
  • perfectly when i'm pushing the dataString Commented May 9, 2016 at 12:23

1 Answer 1

1

This works:

 //--------------------------- CSV EXPORT --------------------------------
 var csvContent = "data:text/csv;charset=utf-16,";
 $("#csv").click(function()
 {
 var _keys = Object.keys(data[0]);
 dataString = "_"+_keys.join(";");
 csvContent += dataString + "\n";
 // Iterating through all the objects
 data.forEach(function (infoArray, index) {
 var dataString = [];
 [].forEach.call(_keys, function(inst, i){
 dataString.push(infoArray[inst]);
 });
 dataString = dataString.join(";");
 csvContent += index < data.length ? dataString + "\n" : dataString;
 });
 var encodedUri = encodeURI(csvContent);
 var link = document.createElement("a");
 link.setAttribute("href", encodedUri);
 link.setAttribute("download", "my_data.csv");
 link.click(); 
 });
answered May 9, 2016 at 15:13
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.