Welcome to the LimeSurvey Community Forum

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

Auto-suggest text field

More
11 years 3 months ago #89948 by frederickjayne
Replied by frederickjayne on topic Auto-suggest text field
Thanks very much for the rapid response. To answer your previous questions, I am on LS 1.92, not 2.0 (haven't had the courage yet to upgrade since I'll be likely doing it manually—but I will soon as I know it's essential). I don't see any sign of JS errors, just no functionality.

Regarding your suggested code, I am not sure what {QID} is. Is there an EM expression this corresponds to this or do you mean the literal QID value (e.g. if the qid is 925, I have noted that the selector '#question925 input.text' works perfectly)? My problem is that I need to be able to export and reimport surveys, and when I do so the IDs all change, which is why it would be sweet to just drop in Qcode.qid.

Thanks again for the help. I'll submit a bug report as suggested.

--FJ
The topic has been locked.
  • tpartner
  • tpartner's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
11 years 3 months ago #89960 by tpartner
Replied by tpartner on topic Auto-suggest text field

I am on LS 1.92, not 2.0

I tested the script above in 1.92 and 2.0.

I am not sure what {QID} is.

{QID} is an EM variable that will return the ID of the question it is found in (925 in your case?). Using this will make the survey portable. In fact, to test I exported/imported the question from 2.0 to 1.92.

Cheers,
Tony Partner

Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
The following user(s) said Thank You: frederickjayne
The topic has been locked.
More
11 years 3 months ago - 11 years 3 months ago #89989 by frederickjayne
Replied by frederickjayne on topic Auto-suggest text field
It's all good now, thanks very much. BTW, on the bug tracker, the suggestion was made to use {self.qid} or {that.Qcode.qid} as well as {QID}. All seem to do the trick and it appears that {Qcode.qid} cannot work in this context.

Many thanks again.

—FJ

Addendum: Emboldened by this success, I have tried creating a MST question (just two subquestions) with different choice sets for each subquestion. Haven't succeeded yet; the choices for the first SQ don't appear; I get the choices for the second SQ for both of them.
Last edit: 11 years 3 months ago by frederickjayne.
The topic has been locked.
More
10 years 9 months ago #97154 by arielmosto
Replied by arielmosto on topic Auto-suggest text field
Hi Tony:

It would be possible to add in this code, validation also that responses can not be repeated?

For example, Alabama can select only once. If you re-selected, get the message "You can not select the same state twice."

Thank you very much, Ariel
The topic has been locked.
More
10 years 9 months ago - 10 years 9 months ago #97158 by arielmosto
Replied by arielmosto on topic Auto-suggest text field
Hi Tony:

This is the code that I mentioned. It is possible to validate duplicate answers? (Not allowing) Thanks!

tpartner wrote: You should not need to change those. They are defined parameters of the function.

Here is a refined version that opens the selection list when the input is focused. The only thing you should need to modify is "answer68164X61X1025FAM06" for different input IDs.

Code:
<script type="text/javascript" charset="utf-8">
 
  $(document).ready(function() {
 
    var states = "Alabama,Alaska,Arizona,Arkansas,California,Colorado,Connecticut,Delaware,District of Columbia,Florida,Georgia,Hawaii,Idaho,Illinois,Indiana,Iowa,Kansas,Kentucky,Louisiana,Maine,Montana,Nebraska,Nevada,New Hampshire,New Jersey,New Mexico,New York,North Carolina,North Dakota,Ohio,Oklahoma,Oregon,Maryland,Massachusetts,Michigan,Minnesota,Mississippi,Missouri,Pennsylvania,Rhode Island,South Carolina,South Dakota,Tennessee,Texas,Utah,Vermont,Virginia,Washington,West Virginia,Wisconsin,Wyoming".split(',');
 
    var msg = 'You must select a value from the list.';
 
    mandatoryAutocomplete('#answer68164X61X1025FAM06', states);
 
    function mandatoryAutocomplete(inputID, data) {
 
      $(inputID).autocomplete({
        source: data,
        minLength: 0,
        change: function(event, ui) {
          var okay = 0;
          if($(inputID).val() != '') {
            $(data).each(function(){
              if($(inputID).val() == this) {
                okay = 1;
              }
            });
          }
          if(okay < 1) {
            alert (msg);
            $(inputID).val('');
          }
        }
 
      }).focus(function(){
        if (this.value == "") {
          $(this).autocomplete('search', '');
        }
      });
    }
  });
</script>

Last edit: 10 years 9 months ago by arielmosto.
The topic has been locked.
  • tpartner
  • tpartner's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
10 years 9 months ago #97161 by tpartner
Replied by tpartner on topic Auto-suggest text field
How many inputs and what question type(s) are you dealing with?

Can you attache a sample survey?

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
10 years 9 months ago #97163 by arielmosto
Replied by arielmosto on topic Auto-suggest text field
Thanks Tony:

10 inputs and the question type its Multiple short text.

Here its an example working with autocomplete:

tony.cio.com.ar/19/index.php?sid=21915&lang=en

Thanks !
The topic has been locked.
  • tpartner
  • tpartner's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
10 years 9 months ago #97168 by tpartner
Replied by tpartner on topic Auto-suggest text field
If you are only concerned about non-duplicate answers and do not need to force selection from the autocomplete list, you can add something like this to the question source. (there is no need to add it to every sub-question)
Code:
<script type="text/javascript" charset="utf-8">  
  $(document).ready(function() {
 
    // Identify the question
    var q1ID = '{QID}';
    var q1 = $('#question'+q1ID+'');
 
    var states = "Alabama,Alaska,Arizona,Arkansas,California,Colorado,Connecticut,Delaware,District of Columbia,Florida,Georgia,Hawaii,Idaho,Illinois,Indiana,Iowa,Kansas,Kentucky,Louisiana,Maine,Montana,Nebraska,Nevada,New Hampshire,New Jersey,New Mexico,New York,North Carolina,North Dakota,Ohio,Oklahoma,Oregon,Maryland,Massachusetts,Michigan,Minnesota,Mississippi,Missouri,Pennsylvania,Rhode Island,South Carolina,South Dakota,Tennessee,Texas,Utah,Vermont,Virginia,Washington,West Virginia,Wisconsin,Wyoming".split(',');
    var msg = 'You cannot have duplicate entries.';
 
    $('input.text', q1).autocomplete({
      source: states,
      minLength: 0,
      change: function(event, ui) {
        var thisInput = $(this);
        if($(thisInput).val() != '') {
          $('input.text', q1).not($(thisInput)).each(function(){
            if($(this).val().toLowerCase() == $(thisInput).val().toLowerCase()) {
              alert (msg);
              $(thisInput).val('').focus();
              $('input.text', q1).autocomplete('close');
              return false;
            }
          });
        }
      },
      select: function(event, ui) {
        var thisInput = $(this);
        var okay = 1;
        $('input.text', q1).not($(thisInput)).each(function(){
          if($(this).val().toLowerCase() == ui.item.value.toLowerCase()) {
            okay = 0;
          }
        });
        if(okay == 0) {
          alert (msg);
          return false;
        }
      }       
    }).focus(function(){        
      if (this.value == "") {
        $(this).autocomplete('search', '');        
      }      
    });    
  });
</script>


Here is a sample survey:

Attachment limesurvey_survey_5956741.lss not found


Cheers,
Tony Partner

Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
Attachments:
The topic has been locked.
More
10 years 9 months ago #97175 by arielmosto
Replied by arielmosto on topic Auto-suggest text field
Hi Tony, Works perfect with non-duplicate answers.

I need to use a list in csv. I tried to adapt the code, following workarounds, but I could not make it work. I generated the file .php and .csv exactly as workarounds.

Below I put the code in question I tried to adapt. thanks

Code:
<script type="text/javascript" charset="utf-8">  
  $(document).ready(function() {
 
    // Identify the question
    var q1ID = '{QID}';
    var q1 = $('#question'+q1ID+'');
 
     var url = "templates/copia_de_default/lista.php";
 
    var msg = 'You cannot have duplicate entries.';
 
var dataArr = [];
 
       $.getJSON(url,function(data){
 
           $.each(data,function(i, item){
 
               dataArr.push(item);
 
           });
 
    $('input.text', q1).autocomplete({
      source: dataArr,
      minLength: 0,
      change: function(event, ui) {
        var thisInput = $(this);
        if($(thisInput).val() != '') {
          $('input.text', q1).not($(thisInput)).each(function(){
            if($(this).val().toLowerCase() == $(thisInput).val().toLowerCase()) {
              alert (msg);
              $(thisInput).val('').focus();
              $('input.text', q1).autocomplete('close');
              return false;
            }
          });
        }
      },
      select: function(event, ui) {
        var thisInput = $(this);
        var okay = 1;
        $('input.text', q1).not($(thisInput)).each(function(){
          if($(this).val().toLowerCase() == ui.item.value.toLowerCase()) {
            okay = 0;
          }
        });
        if(okay == 0) {
          alert (msg);
          return false;
        }
      }       
    }).focus(function(){        
      if (this.value == "") {
        $(this).autocomplete('search', '');        
      }      
    });    
  });
</script>
The topic has been locked.
  • tpartner
  • tpartner's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
10 years 9 months ago - 10 years 9 months ago #97193 by tpartner
Replied by tpartner on topic Auto-suggest text field
The path to the PHP file doesn't look correct. Shouldn't it be this?
Code:
var url = "upload/templates/copia_de_default/lista.php";


Or, if your code is in the question source, you can use this:
Code:
var url = "{TEMPLATEURL}lista.php";

Cheers,
Tony Partner

Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
Last edit: 10 years 9 months ago by tpartner.
The topic has been locked.
More
10 years 9 months ago - 10 years 9 months ago #97203 by arielmosto
Replied by arielmosto on topic Auto-suggest text field
Tony: Still not working.

I'm using version 2.0

The workarounds seems to be for 1.9+

The code in the ,php file, is contained in workarounds its the following:
Code:
<?php
 
   $countriesArr = array();
 
   $file_handle = fopen("lista.csv", "r");
 
   while (!feof($file_handle) ) {
 
       $line_of_text = fgetcsv($file_handle);
 
       array_push($countriesArr, $line_of_text[0]);
 
   }
 
   fclose($file_handle);
 
   echo json_encode($countriesArr);
 
?>

Thanks !
Last edit: 10 years 9 months ago by arielmosto. Reason: forgot change something
The topic has been locked.
  • tpartner
  • tpartner's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
10 years 9 months ago - 10 years 9 months ago #97210 by tpartner
Replied by tpartner on topic Auto-suggest text field
Okay, here's another approach using the jquery-csv plugin .

1) Download jquery.csv.js and place it in your template directory.

2) Place your CSV file in your template directory

3) Add this to startpage.pstpl AFTER the {TEMPLATEJS} placeholder:
Code:
<script type="text/javascript" src="{TEMPLATEURL}jquery.csv.js"></script>

4) Add a script like this to the question source:
Code:
<script type="text/javascript" charset="utf-8">  
  $(document).ready(function() {
 
    // Identify the question
    var q1ID = '{QID}';
    var q1 = $('#question'+q1ID+'');
 
    var url = '{TEMPLATEURL}countries.csv';
    var countries = new Array();
 
    // Grab the CSV contents
    $.get(url, function(data) {
 
      // Convert CSV contents to an array of arrays
      tmp = $.csv.toArrays(data);
 
      // Load the countries array
      $(tmp).each(function(i, item) {
        countries.push(item[0]);
      });
 
      var msg = 'You cannot have duplicate entries.';
 
      $('input.text', q1).autocomplete({
        source: countries,
        minLength: 0,
        change: function(event, ui) {
          var thisInput = $(this);
          if($(thisInput).val() != '') {
            $('input.text', q1).not($(thisInput)).each(function(){
              if($(this).val().toLowerCase() == $(thisInput).val().toLowerCase()) {
                alert (msg);
                $(thisInput).val('').focus();
                $('input.text', q1).autocomplete('close');
                return false;
              }
            });
          }
        },
        select: function(event, ui) {
          var thisInput = $(this);
          var okay = 1;
          $('input.text', q1).not($(thisInput)).each(function(){
            if($(this).val().toLowerCase() == ui.item.value.toLowerCase()) {
              okay = 0;
            }
          });
          if(okay == 0) {
            alert (msg);
            return false;
          }
        }       
      });  
    });  
  });
</script>



Here is a template and working demo survey (install the template first).

In this survey a list of countries is imported from a CSV file for the autocomplete function. I disabled immediate pop-up of the autocomplete as it's very long so you need to type at least one letter to fire it.

Attachment test_csv_2.zip not found


Attachment limesurvey_survey_595674.lss not found


Cheers,
Tony Partner

Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
Last edit: 10 years 9 months ago by tpartner.
The topic has been locked.

Lime-years ahead

Online-surveys for every purse and purpose