Loading csv records into combobox

This section is for posting questions which are not directly related to WYSIWYG Web Builder.
Examples of off topics: web server configuration, hosting, programming related questions, third party scripts.

Note that these questions will generally not be answered by the administrators of this forum.
Post Reply
User avatar
alex4orly
 
 
Posts: 632
Joined: Mon Jan 20, 2014 8:17 am
Location: Australia
Contact:

Loading csv records into combobox

Post 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
WWBman
 
 
Posts: 916
Joined: Fri Jan 08, 2010 6:10 pm

Re: Loading csv records into combobox

Post by WWBman »

You've probably seen this but if the csv file doesn't change very often you can use the Import option.
User avatar
alex4orly
 
 
Posts: 632
Joined: Mon Jan 20, 2014 8:17 am
Location: Australia
Contact:

Re: Loading csv records into combobox

Post 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
Post Reply