Autocomplete a question from csv and fill two more question from same CSV

More
6 years 4 weeks ago #67518 by Kent
Kent created the topic: Autocomplete a question from csv and fill two more question from same CSV
Hi,

Has anyone done this or knows how it could be done with Limesurvey.

In my survey I want the user to choose to enter a name that is autocompleted for accuracy.

My CSV is like this

Name,ID#,email
Bob, 123, This email address is being protected from spambots. You need JavaScript enabled to view it.

1st question would be to enter name - so you start typing and it autocompletes and you choose Bob. I then would like it to automatically fill the next two question which are ID # and email - (from that CSV).

Is there some sort of hack that someone has tried to do this - I'm still trying to get the autocomplete workaround to work ;) Thanks in advance for any advice.

Please Log in to join the conversation.

More
6 years 4 weeks ago #67520 by tpartner
tpartner replied the topic: Autocomplete a question from csv and fill two more question from same CSV
I think you may need to do it in two steps:

1) Use the autocomplete workaround to fill the name field.

2) Use a plugin like js-tables to convert the CSV to an array of arrays. When a change to the name field is detected you could loop through this array to get the ID and email values.

Cheers,
Tony Partner
Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.

Please Log in to join the conversation.

More
6 years 4 weeks ago - 6 years 4 weeks ago #67522 by tpartner
tpartner replied the topic: Autocomplete a question from csv and fill two more question from same CSV
Hmm, after a little exploring, it turns out that there is a "select" event that gets fired when an autocomplete selection is made. This means that you don't need the listener. You can use that event to populate your ID and Email questions.

So, assuming all questions are on the same page, the script would look something like The following.

Replace:
- "NN" with the "Name" question ID
- "II" with the "ID" question ID
- "EE" with the "Email" question ID

Modify the path to your CSV file.
<script type="text/javascript" charset="utf-8">
 
	$(document).ready(function() {
 
		var qNameID = NN;					   
		var qIDID = II;					   
		var qEmailID = EE;
		var url = "upload/templates/yourTemplate/yourCsvFile.csv";
 
		// Create an array to hold the names
		var namesArr = new Array();
 
		// Grab the CSV contents
		$.get(url,function(data){
 
			// Convert CSV contents to an array of arrays
			fullArray = jQuery.csv()(data);
 
			// Load the names array
			$(fullArray).each(function(i, item){
				namesArr.push(item[0]);
			});
 
			// Initialise the autocomplete plugin
			$('#question'+qNameID+' input.text').autocomplete({
				source: namesArr,
				// Event fired when a selection is made (ui.item.value refers to the selected item)
				select: function(event, ui) { 
					// Find the "ID" and "Email" values associated with the selected name value and load those questions
					$(fullArray).each(function(i, item){
						if(item[0] == ui.item.value) {
							$('#question'+qIDID+' input.text').val(item[1]);
							$('#question'+qEmailID+' input.text').val(item[2]);
						}
					}); 
				}
 
			});
		});
	});
</script>

Cheers,
Tony Partner
Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
Last Edit: 6 years 4 weeks ago by tpartner.

Please Log in to join the conversation.

More
6 years 4 weeks ago #67523 by Mazi
Mazi replied the topic: Autocomplete a question from csv and fill two more question from same CSV
Great solution, I think it's worth extending the "load data from CSV file" workaround!?


Best regards/Beste Grüße,
Dr. Marcel Minke
(Limesurvey Head of Support)
Need Help? We offer professional Limesurvey support
Contact: marcel.minke(at)survey-consulting.com'"

Please Log in to join the conversation.

More
6 years 4 weeks ago #67532 by tpartner
tpartner replied the topic: Autocomplete a question from csv and fill two more question from same CSV
Sure Mazi, where is that?

Cheers,
Tony Partner
Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.

Please Log in to join the conversation.

More
6 years 4 weeks ago #67541 by Mazi
Mazi replied the topic: Autocomplete a question from csv and fill two more question from same CSV
I think there are two autocomplete workarounds, one deals with loading data from CSV files.
Didn't you add those?


Best regards/Beste Grüße,
Dr. Marcel Minke
(Limesurvey Head of Support)
Need Help? We offer professional Limesurvey support
Contact: marcel.minke(at)survey-consulting.com'"

Please Log in to join the conversation.

More
6 years 4 weeks ago #67543 by tpartner
tpartner replied the topic: Autocomplete a question from csv and fill two more question from same CSV
Yeah, I did but didn't realize that those were what you were reffering to. I'll do it.

Cheers,
Tony Partner
Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.

Please Log in to join the conversation.

More
6 years 4 weeks ago #67545 by Mazi
Mazi replied the topic: Autocomplete a question from csv and fill two more question from same CSV
Thanks ;-)


Best regards/Beste Grüße,
Dr. Marcel Minke
(Limesurvey Head of Support)
Need Help? We offer professional Limesurvey support
Contact: marcel.minke(at)survey-consulting.com'"

Please Log in to join the conversation.

More
6 years 4 weeks ago #67546 by tpartner
tpartner replied the topic: Autocomplete a question from csv and fill two more question from same CSV

Cheers,
Tony Partner
Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.

Please Log in to join the conversation.

More
4 years 4 months ago - 4 years 4 months ago #97054 by xsiyez
xsiyez replied the topic: Autocomplete a question from csv and fill two more question from same CSV
Hi Tony

I do not seem to get this work on my side. I have followed all the steps from this post and the workaround here ,downloaded the jquery.csv.js plugin added it to my template created 2 questions in the same group and modified the javascript provided on the workaround to suite my environment.

When I launch the question group I do not get any autocompeltion and firebug gives the following errors in the console:

1. ReferenceError: jQuery is not defined
[Break On This Error]
})(jQuery);
jquery.csv.js (line 191)

2. TypeError: jQuery.csv is not a function
fullArray = jQuery.csv()(data);

I can confirm that jquery.csv.js gets loaded as I can see it in the html source code and in firebug. Please find attached the javascript file I modified from the workaround mentioned above and after following this post togehter with my sample CSV file. Any assistance will be greatly appreciated.

Alex
Attachments:
Last Edit: 4 years 4 months ago by xsiyez.

Please Log in to join the conversation.

More
4 years 4 months ago #97056 by xsiyez
xsiyez replied the topic: Autocomplete a question from csv and fill two more question from same CSV
Hi

I managed to resolve the first one because I loaded jquery.csv.js before jquery. However, am still not able to this work.
TypeError: jQuery.csv is not a function
fullArray = jQuery.csv()(data);

Still persists in Firebug. Please help.

Please Log in to join the conversation.

More
4 years 4 months ago #97101 by xsiyez
xsiyez replied the topic: Autocomplete a question from csv and fill two more question from same CSV
Anyone please?

Please Log in to join the conversation.

More
4 years 4 months ago - 4 years 4 months ago #97120 by tpartner
tpartner replied the topic: Autocomplete a question from csv and fill two more question from same CSV
Well, I see a few possible problems.

1) This:
var qCompanyName = answer86545X17X536;
Should be
var qCompanyName = $('#answer86545X17X536');


2) This:
var qCompanyId = answer86545X17X538
Should be
var qCompanyId = $('#answer86545X17X538');


3) Does http://bozpcf/templates/default/php/companies.csv actually exist?


4) It looks like the attached CSV file has the company IDs in the first column and the names in the second but your script indicates that you want them the other way around. The autocomplete function will look at the first column and qCompanyId will be populated from the second column.

Cheers,
Tony Partner
Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
Last Edit: 4 years 4 months ago by tpartner.

Please Log in to join the conversation.

More
4 years 4 months ago #97139 by xsiyez
xsiyez replied the topic: Autocomplete a question from csv and fill two more question from same CSV
Hi Tony

Thanks for the response. I have modified the variables as suggested and swapped the column names in the csv.... :). The csv actually exists as I can browse to in the browser and can see its output in firebug console when I load the csv url.

However, the error:
TypeError: jQuery.csv is not a function
fullArray = jQuery.csv()(data);
Still appears in firebug. I see the complaint in firebug is on
jQuery.csv()
.I only included
jquery.csv.js
from here in startpage.pstpl. Do I need to include any other javascript plugin apart from the
jquery.csv.js
plugin and the javascript that I included in the question group?

Please Log in to join the conversation.

More
4 years 4 months ago #97140 by tpartner
tpartner replied the topic: Autocomplete a question from csv and fill two more question from same CSV
Can you activate a test survey?

Cheers,
Tony Partner
Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.

Please Log in to join the conversation.

Start now!

Just create your account and start using Limesurvey today.

Register now
Join our Newsletter!