- Posts: 58
- Thank you received: 9
Welcome to the LimeSurvey Community Forum
Ask the community, share ideas, and connect with other LimeSurvey users!
Filtering two ling list using autocomplete + external csv file
- eloner
- Topic Author
- Offline
- Senior Member
Less
More
8 years 2 months ago #132079
by eloner
Filtering two ling list using autocomplete + external csv file was created by eloner
Hello,
With LS 2.06+ Build 151109 (but I tried it also on the latest version) I am preparing a survey where I ask:
1) province, a short text question (using jquery + autocomplete and loading the list of the names from a csv file "provcode.csv" where I have the name of the province + the corresponding code)
2) municipality (comune), again using jquery + autocomplete and loading the list from a csv file (comcod2016.csv where I have the name of the municipality, the code of the municipality and the code of the province)
The structure of the two csv files is:
For the province:
and the data are in:
For the municipality:
and the data are in:
My question is that in the second question in the autocompletion list I want to show only the municipalities belonging to the province chosen at question 1.
I enclose the survey I prepared (incomplete, as for the municipalities I am not able to load and filter the municipalities to populate the array used for the autocompletion):
and my template:
So... I appreciate any help!
cheers,
EL
With LS 2.06+ Build 151109 (but I tried it also on the latest version) I am preparing a survey where I ask:
1) province, a short text question (using jquery + autocomplete and loading the list of the names from a csv file "provcode.csv" where I have the name of the province + the corresponding code)
2) municipality (comune), again using jquery + autocomplete and loading the list from a csv file (comcod2016.csv where I have the name of the municipality, the code of the municipality and the code of the province)
The structure of the two csv files is:
For the province:
and the data are in:
For the municipality:
and the data are in:
My question is that in the second question in the autocompletion list I want to show only the municipalities belonging to the province chosen at question 1.
I enclose the survey I prepared (incomplete, as for the municipalities I am not able to load and filter the municipalities to populate the array used for the autocompletion):
and my template:
So... I appreciate any help!
cheers,
EL
Attachments:
The topic has been locked.
- tpartner
- Offline
- LimeSurvey Community Team
Less
More
- Posts: 10108
- Thank you received: 3593
8 years 2 months ago #132102
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 Filtering two ling list using autocomplete + external csv file
I think the trick here would be to nest a new autocomplete call for "Community" in the select function of the "Province" autocomplete.
So, something like this in the source of the "Province" question:
Here's your test survey back with that modification:
So, something like this in the source of the "Province" question:
Code:
<script type="text/javascript" charset="utf-8"> $(document).ready(function() { var qIDpro = {QID}; var qProvinceCode = $('#question'+qIDpro).nextAll('.text-short:eq(0)'); var qComune = $('#question'+qIDpro).nextAll('.text-short:eq(1)'); var qComuneCode = $('#question'+qIDpro).nextAll('.text-short:eq(2)'); var url = "{TEMPLATEURL}provcode.csv"; var url2 = "{TEMPLATEURL}comcod2016.csv"; var province = []; var comune = []; // Hide the code questions qProvinceCode.hide(); qComuneCode.hide(); // Get the comune CSV fullArray2 = []; $.get(url2,function(data){ // Convert CSV contents to an array of arrays fullArray2 = $.csv.toArrays(data); }); // Get the province CSV $.get(url,function(data){ // Convert CSV contents to an array of arrays fullArray = $.csv.toArrays(data); // Load the province data array $(fullArray).each(function(i, item){ province.push(item[0]); }); // Initialise the autocomplete plugin for province $('#question'+qIDpro+' input.text').autocomplete({ source: province, select: function(event, ui) { // Load the hidden "Province Code" question var thisValue = ui.item.value; $(fullArray).each(function(i, item){ if(item[0] == thisValue) { var provinceCode = item[1]; $('input.text', qProvinceCode).val(provinceCode); // Load the comune data array comune = []; $(fullArray2).each(function(i, item){ if(item[2] == provinceCode) { comune.push(item[0]); } }); // Reset comune and code $('input.text', qComune).val('').autocomplete().autocomplete('destroy'); $('input.text', qComuneCode).val(''); // Initialise the autocomplete plugin for comune $('input.text', qComune).autocomplete({ source: comune, select: function(event, ui) { // Load the hidden "Comune Code" question var thisValue = ui.item.value; $(fullArray2).each(function(i, item){ if(item[0] == thisValue) { var comuneCode = item[1]; $('input.text', qComuneCode).val(comuneCode); } }); } }); } }); } }); }); }); </script>
Here's your test survey back with that modification:
Cheers,
Tony Partner
Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
Attachments:
The following user(s) said Thank You: eloner
The topic has been locked.
- eloner
- Topic Author
- Offline
- Senior Member
Less
More
- Posts: 58
- Thank you received: 9
8 years 1 month ago #132148
by eloner
Replied by eloner on topic Filtering two ling list using autocomplete + external csv file
Tested and perfectly working!
Thank you very much!
Thank you very much!
The topic has been locked.
- eloner
- Topic Author
- Offline
- Senior Member
Less
More
- Posts: 58
- Thank you received: 9
8 years 1 month ago #132344
by eloner
Replied by eloner on topic Filtering two ling list using autocomplete + external csv file
Hello,
I have tested the example and it works also with a short multiple text question (that's great!).
I would know if it is possible to use the autocomplete + csv external file also with an array text question.
I mean having a question like:
with the autocomplete for each cell of the array.
I enclose my sample-survey
(the template is the same of my previous post)
Where:
Q1. Is the working question where one can see how to use the autocomplete for a multiple short text question
Q2. Is the non-working question for an array (text) question.
Cheers,
EL
I have tested the example and it works also with a short multiple text question (that's great!).
I would know if it is possible to use the autocomplete + csv external file also with an array text question.
I mean having a question like:
with the autocomplete for each cell of the array.
I enclose my sample-survey
(the template is the same of my previous post)
Where:
Q1. Is the working question where one can see how to use the autocomplete for a multiple short text question
Q2. Is the non-working question for an array (text) question.
Cheers,
EL
Attachments:
The topic has been locked.
- tpartner
- Offline
- LimeSurvey Community Team
Less
More
- Posts: 10108
- Thank you received: 3593
8 years 1 month ago #132353
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 Filtering two ling list using autocomplete + external csv file
You were very close with your script. For some reason (or no reason), the inputs in the array-texts question are not given a class of "text" so you need to change the selector "input.text" to "input[type="text"]". So, something like this:
Modified survey attached:
Code:
<script type="text/javascript" charset="utf-8"> $(document).ready(function() { var q2ID = {QID}; var url = "{TEMPLATEURL}provcode.csv"; var province2 = []; // Get the province CSV $.get(url,function(data){ // Convert CSV contents to an array of arrays fullArray2 = $.csv.toArrays(data); // Load the province data array $(fullArray2).each(function(i, item){ province2.push(item[0]); }); // Initialize the autocomplete plugin $('#question'+q2ID+' input[type="text"]').autocomplete({ source: province2 }); }); }); </script>
Modified survey attached:
Cheers,
Tony Partner
Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
Attachments:
The following user(s) said Thank You: eloner
The topic has been locked.
- eloner
- Topic Author
- Offline
- Senior Member
Less
More
- Posts: 58
- Thank you received: 9
8 years 1 month ago #132356
by eloner
Replied by eloner on topic Filtering two ling list using autocomplete + external csv file
Perfect!
Thank you!
Thank you!
The topic has been locked.