Abhijeet Panwar
Abhijeet Panwar

Reputation: 1867

How to generate proper csv file format using javascript

I have written a JavaScript code which should parse a table and prints values in a div in csv format.

My code :

 for (i = 1; i < table.rows.length; i += 1)
    { 
           row = table.rows[i];
           for (j = 1; j < row.cells.length; j += 1) 
           {
               cell = row.cells[j];
               var heatmapval=(cell.innerHTML).trim();     
               if(heatmapval>25)
               {
                   heatmapval=25;
               }       
               var arr = new Array(3);
               arr[0] = i-1;
               arr[1] = j-1;
               arr[2] = heatmapval;
               $("#csv").append(arr.join(",")+"<br>");

           }
        }

Which results perfect as it prints in the same form in div. I am using highcharts for heatmaps which reads csv values and should create heatmaps for the values.(Note: I have added the link for fiddle in comments).But it can't read my code generated csv form values in div.

As I have added a break tag to go to next line,probably because of that heatmaps can't read those values .I have tried it with a space instead of break but even that does't work.

Upvotes: 0

Views: 168

Answers (1)

Think Different
Think Different

Reputation: 2815

Change

 $("#csv").append(arr.join(",")+"<br>");

TO

 $("#csv").append(arr.join(",")+"\r\n");

<br> is a html tag where as \r\n is end of line and next line characters.

Upvotes: 2

Related Questions