So I have this csv file (the csv file might sit on server side preferably , but not decided yet). Most probably the user will be presented the options of selecting from these csv files from a drop-down menu. Then after selcting a particular file, user clicks "enter" , then this selected csv file should be read in javascript as an array. So how to do this ?? :-
My question is on the lines of How to pass variables and data from PHP to JavaScript?
___csv_file____
Class, Subclass, Company, Product,
Chocolate, Wafer chocolate, Nestle, KitKat,
Chocolate, White chocolate, Nestle, Milkybar,
Chocolate, White chocolate, Nestle, Milkybar,
Chocolate, Caramel chocolate, Nestle, BarOne,
Chocolate, Milk chocolate, Nestle, Nestle Milk chocolate,
Chocolate, Milk chocolate, Nestle, Nestle Milk chocolate,
Chocolate, Milk chocolate, Nestle, Nestle Milk chocolate,
Chocolate, Milk chocolate, Nestle, Nestle Milk chocolate,
Chocolate, Milk chocolate, Cadbury, Dairy milk,
In order to read it manually this is what has been done. But as you can see it has all the csv_data hardcoded, but I'd like to get it automated (ie get generated by reading from a csv file )
root = {
"name": "Chocolate", "tag":"class",
"children": [
{
"name": "Wafer", "tag":"subclass",
"children": [
{
"name": "Nestle", "tag":"company",
"children": [
{"name": "KitKat", "tag":"product"}
]
}
]
},
{
"name": "White", "tag":"subclass",
"children": [
{
"name": "Nestle", "tag":"company",
"children": [
{"name": "Milkybar", "tag":"product"}
]
}
]
},
{
"name": "Caramel", "tag":"subclass",
"children": [
{
"name": "Nestle", "tag":"company",
"children": [
{"name": "BarOne", "tag":"product"}
]
}
]
},
{
"name": "Milk", "tag":"subclass",
"children": [
{
"name": "Nestle", "tag":"company",
"children": [
{"name": "Nestle Milk", "tag":"product"}
]
}, {
"name": "Cadbury", "tag":"company",
"children": [
{"name": "Dairy Milk", "tag":"product"}
]
}
]
}
]
};
The problem now is that I want the javascript to just read this csv file from local location. But hows it possible to read a CSV file into such a complex dimensional array in JAVASCRIPT ?
Possible setup:
PHP web server (or NodeJS server - my personal favourite)
Possible workflow:
First off on the server create a PHP file giving it a relevant name like candyData.php in which you'd have:
//code to read your data.csv
//code which stores the data from the csv into an object $candyData
$json = json_encode($candyData);
return $json
Then on the client side with Javascript and I'm going to also use JQuery:
jQuery.ajax({
url: "http://example.mysite.com/rest/candyData.php",
type: "GET",
success: function (response) {
var candyData = response;
//Here you'd put all the code you currently have so you can visualize the data
},
error: function (response) {
console.log("failed");
}
});
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.