I have the following div
<div class="menu">
Additionally, I have a csv file that contains a bunch of strings called data.csv. For each string in this csv file I want to append a div into this menu div. For example say the csv contains the following data:
item_id, user_name
0,user1
1,user2
2,user3
Then, I am trying to read the data into two arrays and make the HTML look as follows:
<div class="menu">
<div class="item" data-value="0">user1</div>
<div class="item" data-value="1">user2</div>
<div class="item" data-value="2">user3</div>
</div>
Here, each div inside the menu div has a data-value of the item_id and shows the text in this div as the user_name.
I have been trying approaches such as:
1.) Loading a CSV file into an HTML table using javascript
2.) https://www.sitepoint.com/community/t/displaying-specific-data-from-a-csv-file/208326/6
but I am not a frequent javascript user and am having some trouble. Any tips or tricks for doing so would be great.
Current Approach:
<script>
$(document).ready(function() {
// AJAX in the data file
$.ajax({
type: "GET",
url: "data.csv",
dataType: "text",
success: function(data) {processData(data);}
});
function processData(data) {
var lines = data.split(/\r\n|\n/);
var id = [];
var name = [];
var headings = lines[0].split(',');
for (var j=1; j<lines.length; j++) {
var values = lines[j].split(',');
id.push(values[0]);
name.push(values[1]);
}
}
})
</script>
I am using this to try and read the csv data into the id array and the name array, but am having trouble getting these to create a string that has the div with class and value and append it to the . There are about 2000 items in the csv file so I am wondering the best way to do this in javascript. I know how to do this in flask using jinja2, but am trying to learn javascript. The goal is to read the csv data into a dropdown menu.