Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Loading Info From a CSV File

Status
Not open for further replies.

munkygomoo

Programmer
May 16, 2002
15
0
0
US
I have a csv file that looks like such :

1010241,400
1010242,200
1010243,300

How do I get JavaScript to read each line of information by itself so it can deal with it properly?

For instance, I want to make it look like this in the end:

data[1] = new dat(1010241,400);
data[2] = new dat(1010242,200);
data[3] = new dat(1010243,300);

Can anyone help me? - JGD -
 
In IE you could use an IFRAME. Although, you may need to add code to detect whether the IFRAME is loaded or not.

Code:
/* read the contents of the IFRAME and split into an array on the line break */
rows=document.frames("iframeName").document.body.innerHTML.
replace('<XMP>',').replace('<\/XMP>',').split("\n");
/*loop through the lines and split each line on the commas, then add to your data array*/
for(row=0;row<rows.length;row++){
  cols=rows[row].split(',')
  data[data.length]=new dat(cols[0],cols[1])
}
 
I tried that, but it didn't work... can I send you the files (an 8.75 kb HTML file, an 885 byte CSS file, and a 101 byte CSV file) and have you look at it? - JGD -
 
hi JGD,

the example below will take the chunk of csv text, split it into individual lines, then create dat() objects by splitting each line on the comma. hope this gives you some ideas!

[tt]
<html>
<head>
<title>csv to data</title>

<script name=&quot;javascript&quot;>
var aData = new Array();

function getData(sArgs) {
var aArgs = sArgs.split(/\s/);

for (idx = 0; idx < aArgs.length; idx++) {
aData[idx] = new dat(aArgs[idx]);
}
}
function dat() {
var a = arguments[0].split(',');
for (inx = 0; inx < a.length; inx++) {
this[inx] = a[inx];
}
}
function showData() {
for (idx = 0; idx < aData.length; idx++) {
sMsg = &quot;&quot;;
for (el in aData[idx]) {
sMsg += &quot;aData[&quot; + idx +
&quot;][&quot; + el + &quot;] = &quot; +
aData[idx][el] + &quot;\n&quot;;
}
alert(sMsg);
}
}
</script>

<style type=&quot;text/css&quot;>
</style>

<meta name=&quot;author&quot; content=&quot;?&quot;>
<meta name=&quot;keywords&quot; content=&quot;?&quot;>
<meta name=&quot;description&quot; content=&quot;?&quot;>
</head>

<body onload=&quot;&quot;>
<form name=&quot;&quot; action=&quot;&quot; method=&quot;&quot;>
<textarea name=&quot;databook&quot; rows=&quot;8&quot; cols=&quot;35&quot;>A1371001,250,0,150,0,0,200,100,0
A1231002,70,0,130,0,200,0,0,350
A1451003,0,150,0,200,150,0,120,0 </textarea>
<p />
<input type=&quot;button&quot; name=&quot;&quot; value=&quot;getData();&quot; onclick=&quot;getData(this.form.databook.value);&quot; />
<p />
<input type=&quot;button&quot; name=&quot;&quot; value=&quot;how many dat() objects?&quot; onclick=&quot;alert(aData.length);&quot; />
<p />
<input type=&quot;button&quot; name=&quot;&quot; value=&quot;show dat() objects&quot; onclick=&quot;showData();&quot; />
</form>
</body>
</html>
[/tt] =========================================================
if (!succeed) try();
-jeff
 
The TDC stuff looks really good.

Does anyone know if you can access the data in this way?

i.e. it's easy to get a table cell to display the data, but can I read the following data into a JavaScript variable:
datasrc="#tdcStaff"
datafld=FirstName

I need to read the actual data then process each piece of data individually. Is that possible?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top