Welcome to the LimeSurvey Community Forum

Ask the community, share ideas, and connect with other LimeSurvey users!

LONG LIST AUTOCOMPLETION + CHARSET

  • eloner
  • eloner's Avatar Topic Author
  • Offline
  • Senior Member
  • Senior Member
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:

File Attachment:

File Name: LongList.lss
File Size:13 KB

and the csv file:

File Attachment:

File Name: comun.csv
File Size:73 KB


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

File Attachment:

File Name: comcode.csv
File Size:147 KB
) 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
The topic has been locked.
More
8 years 1 month ago #130177 by Ben_V
Replied by Ben_V on topic LONG LIST AUTOCOMPLETION + CHARSET
Hi Eleonor,
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
  • tpartner's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
8 years 1 month ago - 8 years 1 month ago #130278 by tpartner
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

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.

File Attachment:

File Name: autocomple...test.zip
File Size:177 KB

File Attachment:

File Name: limesurvey...9856.lss
File Size:13 KB

Cheers,
Tony Partner

Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
Last edit: 8 years 1 month ago by tpartner.
The following user(s) said Thank You: eloner
The topic has been locked.
  • eloner
  • eloner's Avatar Topic Author
  • Offline
  • Senior Member
  • Senior Member
More
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
The topic has been locked.
  • tpartner
  • tpartner's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
8 years 1 month ago #130315 by tpartner
Replied by tpartner on topic LONG LIST AUTOCOMPLETION + CHARSET
Oops, sorry Eloner, that was a typo and should have been :
Code:
$('#question'+qID).nextAll('.text-short:eq(0)');
So it would have only selected the first instance of ".text-short".

Cheers,
Tony Partner

Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
The topic has been locked.

Lime-years ahead

Online-surveys for every purse and purpose