Page 1 of 1

Loading csv records into combobox

Posted: Fri Dec 06, 2019 8:31 am
by alex4orly
I have a new page here : http://www.ourvilla.net.au/services.html
I need to load options into the Combobox from a csv file.

I need some help with a Javascrip function to do that

The Combobox id = Combobox1
The csv file is : mobiles.csv

The csv file has 4 columns :
Mobile Folder Village Log

I want the Village column [2] to be the one loaded. below is my current code - just missing that step:

<script>

// Read service providers data file
$(document).ready(function()
{
$.ajax({
type: "GET",
url: "mobiles.csv",
dataType: "text",
success: function(data) {processData(data);}
});
});

function processData(allText)
{
var allTextLines = allText.split(/\r\n|\n/);
var lines = [];

for (var i=0; i<allTextLines.length; i++)
{
var data = allTextLines.split(',');

var MobileNo = data[0];
var Folder = data[1];
var Village = data[2];

// The number here is hard coded, in real time, this will be passed into here
if(MobileNo == "0418344556")
{
// Load Village element into the Combobox
????
}
}
}
</script>

Any help will be appreciated

Cheers

Re: Loading csv records into combobox

Posted: Fri Dec 06, 2019 12:35 pm
by WWBman
You've probably seen this but if the csv file doesn't change very often you can use the Import option.

Re: Loading csv records into combobox

Posted: Fri Dec 06, 2019 7:11 pm
by alex4orly
No, Have not seen this. Anyway, the csv file changes all the time.
I now figured out what the code should be, here it is :

if(MobileNo == "418344556")
{
// Load Village element into the Combobox
var opt = data[2];
var el = document.createElement("option");

el.textContent = opt;
el.value = opt;

ddlItems.appendChild(el);
}

It works OK, but with one problem - the if() statement doesn't compare, if I comment it out - the combobox is loaded - but with ALL the records, not just those the filter condition is intended for.

Any idea why? I placed an alert() and the MobileNo shows correctly, so why?

Thanks