Go Back   CodingForums.com > :: Client side development > JavaScript programming > JavaScript frameworks

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 10-01-2012, 05:10 PM   PM User | #1
Rory Barrett
New to the CF scene

 
Join Date: Oct 2012
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Rory Barrett is an unknown quantity at this point
CsV to javascript array

Hello Colleagues

I joined this forum yesterday. I am keen to be able to read a csv file in the same folder as my html and create a javascript array with it. I have successfully used a jquery function that puts a table on the html with the csv showing however it would be useful to create an array with this data.The code I used to accomplish this is shown below.

<title>jQuery CSVToTable</title>
<link rel="stylesheet" href="css/csvtable.css" type="text/css" />
<script type="text/javascript" src="js/jquery-1.4.2.min.js"></script>
<script type="text/javascript" src="js/jquery.csvToTable.js"></script>

<script>
$(function() {
$.get('11MTC_BB_09.csv', function(data) {
$('#CSVSource').html('<pre>' + data + '</pre>');
});

$('#CSVTable').CSVToTable('11MTC_BB_09.csv', { loadingImage: 'images/loading.gif', startLine: 0 });

});

</script>




I thought I had asked this question yesterday but it has not appeared in the forums yet.

Rory Barrett
Rory Barrett is offline   Reply With Quote
Old 10-01-2012, 05:32 PM   PM User | #2
DrDOS
Senior Coder

 
Join Date: Sep 2010
Posts: 1,155
Thanks: 10
Thanked 148 Times in 148 Posts
DrDOS is infamous around these parts
Do you have a server side language that can read the .csv file?
Most can, just have it write the .js file for you. If you have .php,
the most common server side language, it can handle .csv files
very well.
DrDOS is offline   Reply With Quote
Old 10-01-2012, 05:33 PM   PM User | #3
xelawho
Senior Coder

 
xelawho's Avatar
 
Join Date: Nov 2010
Posts: 2,437
Thanks: 52
Thanked 454 Times in 452 Posts
xelawho will become famous soon enoughxelawho will become famous soon enough
actually, your question is here

but anyway. This is a simple AJAX request and there is no need to use jQuery if all you want is the data in an array. You may find it better to build your own array-to-table code, depending on what your needs are.

Here's how you'd do it in plain javascript. The items in the csv file go into an array called "deets" (although you will have to check how exactly your csv file is saved,. and that will dictate how you should split the response text). Here they are just printed out into a div, but you can do what you like with them...

Code:
<body>
<div id="results"></div>
<script type="text/javascript">
var deets=[];
function getData(callback) {
var url="11MTC_BB_09.csv"
var csvFile = window.XMLHttpRequest?new XMLHttpRequest:new ActiveXObject('Microsoft.XMLHTTP');
	csvFile.onreadystatechange = function() {
   if (csvFile.readyState == 4) {
     callback(csvFile, csvFile.status);
   }
 };
csvFile.open("GET", url, true); 
csvFile.send(null);
}


getData(function(data) {
  deets = data.responseText.split(/\n/);
  for (var i = 0; i < deets.length; i++) {
  document.getElementById("results").innerHTML+=deets[i]+"<br>"
	}
  });
</script>
</body>

Last edited by xelawho; 10-01-2012 at 05:49 PM..
xelawho is offline   Reply With Quote
Old 10-02-2012, 12:39 AM   PM User | #4
VIPStephan
The fat guy next door


 
VIPStephan's Avatar
 
Join Date: Jan 2006
Location: Halle (Saale), Germany
Posts: 7,613
Thanks: 5
Thanked 865 Times in 842 Posts
VIPStephan is a jewel in the roughVIPStephan is a jewel in the roughVIPStephan is a jewel in the rough
Quote:
Originally Posted by xelawho View Post
actually, your question is here

I’ve closed that one to not let a double conversation lead to confusion.
__________________
Don’t click this link!
VIPStephan is online now   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 11:26 AM.


Advertisement
Log in to turn off these ads.