Welcome to the LimeSurvey Community Forum

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

Autocomplete and automatically filled questions - csv

More
13 years 9 months ago #70785 by crescentio
Hello folks

I'm trying to design an autocomplete question, which is a numeric ID and the values come from a csv file placed in the template folder. When a value is choosen two another questions, that are hidden and in the same section, must be filled automatically. Here is an example of the csv:

123456,John,Doe
234567,Mary,Smith
3456789,Carl,Simpson

The two questions are "name" and "surname".
I'm trying to make this works with the help from this thread and his wiki post's son , but no success.
In the workarounds cited before, there is a link to the plugin but it doesn't works, so I found another source , but I'm not sure if this is the appropriate one.

I'm using the last release of LimeSurvey.
I hope that you can help me with this
Thanks in advance
The topic has been locked.
More
13 years 9 months ago #70795 by tpartner

there is a link to the plugin but it doesn't works

Are you referring to the jquery.csv.js plugin? Doesn't work how?

Do you have the autocomplete portion of this workaround working?

Cheers,
Tony Partner

Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
The topic has been locked.
More
13 years 9 months ago #70798 by crescentio
Hello Tony, thanks for answer.

Yes is the jquery.csv.js plugin. The site is in development, look .

No, the autocomplete from that workaround is not working.
Although, the automatically filled answers is more important to me than the autocomplete feature.

Thanks
The topic has been locked.
More
13 years 9 months ago #70814 by tpartner
You need to get the autocompleting function to work first - the populating of following questions depends on that.

Are there any JavaScript errors? Can you activate a sample survey for us to see?

Cheers,
Tony Partner

Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
The topic has been locked.
More
13 years 9 months ago #70826 by crescentio
Hello Tony, thanks

A sample survey is here .

The csv is equal to the example in the first post. The csv and the plugin are in templates/default folder.

The following code is in the source of the first question:
Code:
<script type="text/javascript" charset="utf-8">
 
  $(document).ready(function() {
 
    var qID = 39255X1X1;             
    var qName = 39255X1X2;             
    var qSurname = 39255X1X3;
    var url = "templates/default/userdata.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'+qID+' 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) {
              // The value from column 2 of the CSV
              $('#question'+qName+' input.text').val(item[1]);
              // The value from column 3 of the CSV
              $('#question'+qSurname+' input.text').val(item[2]);
            }
          }); 
        }
 
      });
    });
  });
</script>

I've attached the plugin too
The topic has been locked.
More
13 years 9 months ago #70827 by crescentio
The topic has been locked.
More
13 years 9 months ago - 13 years 9 months ago #70832 by tpartner
The question ID vars are not correct. You don't use the full SGQA, just the question ID .

This:
Code:
var qID = 39255X1X1;             
    var qName = 39255X1X2;             
    var qSurname = 39255X1X3;

Should be this:
Code:
var qID = 1;             
    var qName = 2;             
    var qSurname = 3;

Cheers,
Tony Partner

Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
Last edit: 13 years 9 months ago by tpartner.
The topic has been locked.
More
13 years 9 months ago #70834 by crescentio
Hi Tony, thanks

I did it in that way before and doesn't work. Anyway, I made the corrections in the sample survey and still autocomplete doesn't work.
Any other suggestion?

Thanks
The topic has been locked.
More
13 years 9 months ago #70837 by tpartner
There is no link to the plugin. You need to follow all steps in the workaround:


- Download the jquery.csv.js plugin and place it in your template folder
- Add the following line to your startpage.pstpl BEFORE the tag for template.js
Code:
<script type="text/javascript" src="{TEMPLATEURL}jquery.csv.js"></script>

Cheers,
Tony Partner

Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
The topic has been locked.
More
13 years 9 months ago #70840 by crescentio
Thanks so much Tony

I feel so silly by forget that step :blush:
Although, when I was working in my localhost I did that step and the autocomplete doesn't work, maybe I forget another step.

Thanks so much, this was driving me crazy for days :laugh:
The topic has been locked.
  • Gabriela
  • Gabriela's Avatar
  • New Member
  • New Member
More
12 years 7 months ago #91684 by Gabriela
Hi Tony, sorry, Im trying to achieve the same thing but with no results,
please can you check this? (as crescentio im working on localhost too)
Thanks a lot
The topic has been locked.
More
12 years 7 months ago #91699 by tpartner
Hi Gabriela,

Can you please explain what you are trying to do?

I see "Lingua", "provincia" and "Distrito" questions but I don't know how they are related. Do you want to auto-populate "provincia" and "Distrito" depending on "Lingua"?

Cheers,
Tony Partner

Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
The topic has been locked.
More
12 years 7 months ago - 12 years 7 months ago #91704 by tpartner
For this workaround to work, you need to:
1) Include the CSV file in your template directory
2) Place the jquery.csv.js file in your template directory
3) Add the following line to your startpage.pstpl AFTER the {TEMPLATEJS} tag
Code:
<script type="text/javascript" src="{TEMPLATEURL}jquery.csv.js"></script>

Your code looked fine to me but I modified it a bit to automatically detect the question IDs and the the path to the template directory.

Placing this code in the source of the first question will initiate the auto-complete function for that question, using the CSV column-1 values. When a value is selected, it will auto-populate the following 2 questions with the the CSV column-2 and column-3 values.

For different questions/surveys, all you should need to modify is the csvName variable.
Code:
<script type="text/javascript" charset="utf-8">
 
  $(document).ready(function() {
 
    // Define the CSV filename
    var csvName = 'linguas.csv';
 
    // Define some paths
    var surveyRoot = location.pathname.split('index.php')[0];
    var templateName = $('head link[href*="template.css"]').attr('href').replace(/\/template.css/, '').split('/templates/')[1];
    var templatePath = surveyRoot+'upload/templates/'+templateName+'/';
 
    // Define the questions
    var q1ID = '{QID}';  
    var q1 = $('#question'+q1ID);
    var q2 = $(q1).nextAll('.text-short:eq(0)');
    var q3 = $(q1).nextAll('.text-short:eq(1)');
 
    // Define the path to the CSV
    var url = templatePath+csvName;
 
    // Create an array to hold the CSV rows
    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 CSV rows array
      $(fullArray).each(function(i, item){
        namesArr.push(item[0]);
      });
 
      // Initialise the autocomplete plugin
      $('input.text', q1).autocomplete({
        source: namesArr,
        // Event fired when a selection is made (ui.item.value refers to the selected item)
        select: function(event, ui) { 
          // Find the column 2 and column 3 values associated with the selected column 1 value and load q2 and q3
          $(fullArray).each(function(i, item){
            if(item[0] == ui.item.value) {
              // The value from column 2 of the CSV
              $('input.text', q2).val(item[1]);
              // The value from column 3 of the CSV
              $('input.text', q3).val(item[2]);
            }
          }); 
        }
 
      });
    });
  });
</script>

Here is a working copy of your survey and the corresponding template with the CSV file and jquery.csv.js included. (import the template first so the survey uses the correct one)

File Attachment:

File Name: gabriela_csv_test.zip
File Size:74.46 KB


File Attachment:

File Name: autofillcvs_TP.lss
File Size:28.22 KB

Cheers,
Tony Partner

Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
Last edit: 12 years 7 months ago by tpartner.
The topic has been locked.
  • Gabriela
  • Gabriela's Avatar
  • New Member
  • New Member
More
12 years 7 months ago #91719 by Gabriela
Merci!!! I was all day in this and no moving...(mega frustrating..)
I still didt try it, but reading I can see the first difference. I was following the previuos answers where you said:

- Download the jquery.csv.js plugin and place it in your template folder
- Add the following line to your startpage.pstpl BEFORE the tag for template.js

Before. :dry:
But its After?
super.

ok, now Ill try..

It works!! but only with "gabriela csv test" template. Have to check differences with mine.

Have to go to work now, but as soon as I can ill find out what is going wrong..

Thank you so much, it made my day.

ps: In "gabriela csv test" template, startpage, appears in one corner TEMPLATEJS sorrounded by red square, type of , "variable undefined". (but still working, strange)
The topic has been locked.
More
12 years 7 months ago #91734 by tpartner

I was following the previuos answers where you said...Add the following line to your startpage.pstpl BEFORE the tag for template.js

Those instructions are for LimeSurvey 1.92. Things are slightly different in LimeSurvey 2.0.

In "gabriela csv test" template, startpage, appears in one corner TEMPLATEJS sorrounded by red square...

Can you provide a screenshot?

Cheers,
Tony Partner

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

Lime-years ahead

Online-surveys for every purse and purpose