- Posts: 59
- Thank you received: 9
Welcome to the LimeSurvey Community Forum
Ask the community, share ideas, and connect with other LimeSurvey users!
LONG LIST AUTOCOMPLETION + CHARSET
- eloner
- Topic Author
- Offline
- Senior Member
Less
More
8 years 1 month ago #130176
by eloner
LONG LIST AUTOCOMPLETION + CHARSET was created by eloner
Hello,
I have a short text question where I ask for the address of the respondent (name of the municipality).
As I have a long list of municipalities (about 8000) , I have created a CSV file (according to: stackoverflow.com/questions/27803669/lim...uto-complete-feature and manual.limesurvey.org/Workarounds:_Manip...ime_using_Javascript ).
I have two problems about the question:
1. The names of the municipalities are not shown correctly in the case they contain special codes for Italy (è, ì, ù, à, ò), i.e. "Agliè" becomes "Agli" + a strange character and this happens also after adding charset=iso-8859-1 in the script.
The script attached to the question is:
<script type="text/javascript"; charset=iso-8859-1"; src="{TEMPLATEURL}jquery.csv-0.71.js"></script><script>
var url = "{TEMPLATEURL}comun.csv";
$(function() {
var qID = {QID};
var comuni = new Array();
$.get(url,function(data){
// Convert CSV contents to an array of arrays
fullArray = $.csv.toArrays(data);
// Load the data array
$(fullArray).each(function(i, item){
comuni.push(item[0]);
});
// Initialise the autocomplete plugin
$('#question'+qID+' input.text').autocomplete({
source: comuni
});
// console.log(data);
});
});</script>Address:
I enclose here the survey code:
and the csv file:
2. Is there a way to save in a new variable (possibly not visible to the respondent) the code of the municipality, instead of the name? In this case the csv file (I enclose here a sample ) should be something like:
municipality1 code1
municipality2 code2
municipality3 code3
and so on
Kind regards,
Elo
P.S. I use LS 2.06+ Build 151215
I have a short text question where I ask for the address of the respondent (name of the municipality).
As I have a long list of municipalities (about 8000) , I have created a CSV file (according to: stackoverflow.com/questions/27803669/lim...uto-complete-feature and manual.limesurvey.org/Workarounds:_Manip...ime_using_Javascript ).
I have two problems about the question:
1. The names of the municipalities are not shown correctly in the case they contain special codes for Italy (è, ì, ù, à, ò), i.e. "Agliè" becomes "Agli" + a strange character and this happens also after adding charset=iso-8859-1 in the script.
The script attached to the question is:
<script type="text/javascript"; charset=iso-8859-1"; src="{TEMPLATEURL}jquery.csv-0.71.js"></script><script>
var url = "{TEMPLATEURL}comun.csv";
$(function() {
var qID = {QID};
var comuni = new Array();
$.get(url,function(data){
// Convert CSV contents to an array of arrays
fullArray = $.csv.toArrays(data);
// Load the data array
$(fullArray).each(function(i, item){
comuni.push(item[0]);
});
// Initialise the autocomplete plugin
$('#question'+qID+' input.text').autocomplete({
source: comuni
});
// console.log(data);
});
});</script>Address:
I enclose here the survey code:
and the csv file:
2. Is there a way to save in a new variable (possibly not visible to the respondent) the code of the municipality, instead of the name? In this case the csv file (I enclose here a sample ) should be something like:
municipality1 code1
municipality2 code2
municipality3 code3
and so on
Kind regards,
Elo
P.S. I use LS 2.06+ Build 151215
Attachments:
The topic has been locked.
- Ben_V
- Offline
- Platinum Member
Less
More
- Posts: 1223
- Thank you received: 351
8 years 1 month ago #130177
by Ben_V
Benoît
EM Variables => bit.ly/1TKQyNu | EM Roadmap => bit.ly/1UTrOB4
Last Releases => 2.6x.x goo.gl/ztWfIV | 2.06/2.6.x => bit.ly/1Qv44A1
Demo Surveys => goo.gl/HuR6Xe (already included in /docs/demosurveys)
Replied by Ben_V on topic LONG LIST AUTOCOMPLETION + CHARSET
Hi Eleonor,
Have you already seen this post ?
Have you already seen this post ?
Benoît
EM Variables => bit.ly/1TKQyNu | EM Roadmap => bit.ly/1UTrOB4
Last Releases => 2.6x.x goo.gl/ztWfIV | 2.06/2.6.x => bit.ly/1Qv44A1
Demo Surveys => goo.gl/HuR6Xe (already included in /docs/demosurveys)
The following user(s) said Thank You: eloner
The topic has been locked.
- tpartner
- Offline
- LimeSurvey Community Team
Less
More
- Posts: 10611
- Thank you received: 3691
8 years 1 month ago - 8 years 1 month ago #130278
by tpartner
Cheers,
Tony Partner
Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
Replied by tpartner on topic LONG LIST AUTOCOMPLETION + CHARSET
Hi Eloner,
1) The CSV files need to be encoded UTF8 to display those characters correctly. You can set the encoding in a good plain-text editor like Notepad++ (or perhaps in LibreOffice Calc). After setting the encoding, avoid editing the files in MS Office Excel - it is horrible at handling CSVs - use Calc instead. I have included your two CSVs encoded to UTF8 in the template attached below.
2) You can use the autocomplete "select" event to populate a text question with the code (column 2 in comecode.csv). In Q1 of the attached survey, you will find the script below that:
- Identifies the next short-text question
- Hides that question (commented out for testing)
- Uses the autocomplete plugin for the "Address" question
- Uses the autocomplete "select" event to populate the next short-text question
Attached are a template (install this first) and a sample survey.
1) The CSV files need to be encoded UTF8 to display those characters correctly. You can set the encoding in a good plain-text editor like Notepad++ (or perhaps in LibreOffice Calc). After setting the encoding, avoid editing the files in MS Office Excel - it is horrible at handling CSVs - use Calc instead. I have included your two CSVs encoded to UTF8 in the template attached below.
2) You can use the autocomplete "select" event to populate a text question with the code (column 2 in comecode.csv). In Q1 of the attached survey, you will find the script below that:
- Identifies the next short-text question
- Hides that question (commented out for testing)
- Uses the autocomplete plugin for the "Address" question
- Uses the autocomplete "select" event to populate the next short-text question
Code:
<script type="text/javascript" charset="utf-8"> $(document).ready(function() { var qID = {QID}; var nextQuestion = $('#question'+qID).nextAll('.text-short'); var url = "{TEMPLATEURL}comcode.csv"; var comuni = []; // Hide the next question //nextQuestion.hide(); $.get(url,function(data){ // Convert CSV contents to an array of arrays fullArray = $.csv.toArrays(data); // Load the data array $(fullArray).each(function(i, item){ comuni.push(item[0]); }); // Initialise the autocomplete plugin $('#question'+qID+' input.text').autocomplete({ source: comuni, select: function(event, ui) { // Load the hidden "Code" question var thisValue = ui.item.value; $(fullArray).each(function(i, item){ if(item[0] == thisValue) { $('input.text', nextQuestion).val(item[1]); } }); } }); }); }); </script>
Attached are a template (install this first) and a sample survey.
Cheers,
Tony Partner
Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
Attachments:
Last edit: 8 years 1 month ago by tpartner.
The following user(s) said Thank You: eloner
The topic has been locked.
- eloner
- Topic Author
- Offline
- Senior Member
Less
More
- Posts: 59
- Thank you received: 9
8 years 1 month ago #130313
by eloner
Replied by eloner on topic LONG LIST AUTOCOMPLETION + CHARSET
I have tested tpartner's example.
It works very well (so, thank you again!).
I suggest to change this code in the script:
var nextQuestion = $('#question'+qID).nextAll('.text-short');
with this:
var nextQuestion = $('#question'+qID).next('.text-short');
(just to avoid to display the code in each of the following short-text questions).
If someone wants to use the script, remember to copy the file jquery.csv-0.71.js in the template's folder.
Cheers,
Enzo
It works very well (so, thank you again!).
I suggest to change this code in the script:
var nextQuestion = $('#question'+qID).nextAll('.text-short');
with this:
var nextQuestion = $('#question'+qID).next('.text-short');
(just to avoid to display the code in each of the following short-text questions).
If someone wants to use the script, remember to copy the file jquery.csv-0.71.js in the template's folder.
Cheers,
Enzo
The topic has been locked.
- tpartner
- Offline
- LimeSurvey Community Team
Less
More
- Posts: 10611
- Thank you received: 3691
8 years 1 month ago #130315
by tpartner
Cheers,
Tony Partner
Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
Replied by tpartner on topic LONG LIST AUTOCOMPLETION + CHARSET
Oops, sorry Eloner, that was a typo and should have been :
So it would have only selected the first instance of ".text-short".
Code:
$('#question'+qID).nextAll('.text-short:eq(0)');
Cheers,
Tony Partner
Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
The topic has been locked.