Welcome to the LimeSurvey Community Forum

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

Array - Text (Col1), Text (Col2), Slider(Col3)

  • 2022needhelp
  • 2022needhelp's Avatar Topic Author
  • Offline
  • New Member
  • New Member
More
2 years 9 months ago #226603 by 2022needhelp
Please help us help you and fill where relevant:
Your LimeSurvey version: 3.22.28
Own server or LimeSurvey hosting:
Survey theme/template:
==================
Hi all,
I am currently trying to modify a question format for a survey so that the matrix includes a slider from 1 to 100 in addition to the text fields. I want to enter a short text in column 1 and 2 and then rate the entry in column 3 with a slider. I have found a forum entry in which exactly this is achieved with the help of a javaskript. Unfortunately I can't get it to work for me: ([url] forums.limesurvey.org/forum/can-i-do-thi...n-col2-,-slider-col3 [/url]).
Can someone help me in this regard?

Thanks a lot!

 

Please Log in to join the conversation.

  • holch
  • holch's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
2 years 9 months ago #226615 by holch
The post you are referring to is 5 years old and the solution is based on LS 2.5x. However, the survey themes/templates have changed significantly in LS 3.x so I am not surprised that it won't work. You will have to adapt the script to the new themes.

By the way, you did not mention which survey theme you are using and if your survey is hosted by Limesurvey or on another hosting platform.

Help us to help you!
  • Provide your LS version and where it is installed (own server, uni/employer, SaaS hosting, etc.).
  • Always provide a LSS file (not LSQ or LSG).
Note: I answer at this forum in my spare time, I'm not a LimeSurvey GmbH employee.

Please Log in to join the conversation.

  • 2022needhelp
  • 2022needhelp's Avatar Topic Author
  • Offline
  • New Member
  • New Member
More
2 years 9 months ago #226617 by 2022needhelp
Replied by 2022needhelp on topic Array - Text (Col1), Text (Col2), Slider(Col3)
Thank you for pointing this out. I use Limesurvey hosting and a custom theme that inherits from Bootswatch.
Yes I know that the post is already relatively old. The dropdown and the removal of the text field in column three still worked, but the interesting part with the slider did not. 

Please Log in to join the conversation.

  • tpartner
  • tpartner's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
2 years 9 months ago #226627 by tpartner
Replied by tpartner on topic Array - Text (Col1), Text (Col2), Slider(Col3)
One of the problems is that the Bootstrap slider JavaScript and CSS are not loaded unless a core multi-numeric-slider question exists on the page.

The easiest solution is to insert a multi-numeric-slider question directly after the array and then use JavaScript to:
- Remove the core array text inputs
- Hide the multi-numeric question
- Move the sliders from the multi-numeric question into the appropriate column of the array

This solution is far easier than inerting your own sliders or extending the array. The only downside is that you will have unused columns in the data (for the removed text inputs).

After inserting the multi-numeric-slider question, add this script to the question source of the array question:
 
 
Code:
<script type="text/javascript" data-author="Tony Partner">  
  $(document).on('ready pjax:scriptcomplete',function(){
 
    // The column to receive the sliders
    var sliderColumn = 3;
 
    var thisQuestion = $('#question{QID}');
    var nextQuestion = thisQuestion.nextAll('.numeric-multi:eq(0)');
 
        // Hide the next question
    nextQuestion.hide();
 
    //Remove the core array text inputs
    $('tr[id^="javatbd"] .answer-item:nth-child('+(sliderColumn+1)+') input', thisQuestion).remove();
 
 
    // Wait for the sliders to be inititialized  
    $('input:text', nextQuestion).on('slideEnabled',function(){
 
      var thisItem = $(this).closest('li');
      var thisItemIndex = thisItem.index();
 
      // Move the slider and add some styling
      $('tr[id^="javatbd"]:eq('+thisItemIndex+') .answer-item:nth-child('+(sliderColumn+1)+')', thisQuestion).append($('.slider-container', thisItem));
      $('tr[id^="javatbd"]:eq('+thisItemIndex+') .slider-container', thisQuestion)
        .removeClass('col-xs-12 col-sm-12')
        .css({
          'display': 'block',
          'padding': '25px 10px 0 10px'
        });
    });
  });
</script>

 

Sample 5.x survey attached: 

File Attachment:

File Name: limesurvey...8(1).lss
File Size:53 KB

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: Joffm, ngolub

Please Log in to join the conversation.

  • 2022needhelp
  • 2022needhelp's Avatar Topic Author
  • Offline
  • New Member
  • New Member
More
2 years 9 months ago #226644 by 2022needhelp
Replied by 2022needhelp on topic Array - Text (Col1), Text (Col2), Slider(Col3)
Thanks already for the approach. I just tried it, but it does not work properly yet. It shows column 3 without text field and hides the question with the sliders. But unfortunately the sliders are not displayed in the matrix. 

Please Log in to join the conversation.

  • Joffm
  • Joffm's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
2 years 9 months ago #226645 by Joffm
In this case it is always the best to send your approach as lss export.
Tony's example works without any issue.

So there might be a small thing you missed.

Joffm

Volunteers are not paid.
Not because they are worthless, but because they are priceless

Please Log in to join the conversation.

  • 2022needhelp
  • 2022needhelp's Avatar Topic Author
  • Offline
  • New Member
  • New Member
More
2 years 9 months ago #226647 by 2022needhelp
Replied by 2022needhelp on topic Array - Text (Col1), Text (Col2), Slider(Col3)
When I prepared the lss export it suddenly worked. I have looked where the difference between the survey and the test export file is. We have an autocomplete function for the text field in column 1. This is the problem. When I added the function back in, the slider stopped working. Is there any way to make both the autocomplete and the slider work together? Here is the script to our autocomplete:

<link href="//code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css" rel="stylesheet" /><script src="//code.jquery.com/jquery-1.10.2.js"></script><script src="//code.jquery.com/ui/1.11.4/jquery-ui.js"></script>
<link href="/resources/demos/style.css" rel="stylesheet" /><script>
$(document).ready(function() {
  var insertans = "{q1.NAOK}";
  var url="/upload/surveys/XXX/files/";
  var end=".js";
  $.when(
    //then load the JavaScript file
    $.getScript(url+insertans+end),
    $.Deferred(function( deferred ){
            $( deferred.resolve );
        })
  ).done(function(){
                $('td.answer_cell_1 > input[type="text"]').autocomplete({
                    source: function(request, response) {
        var term = request.term;  // request.term = user query
        var data = handleAutocomplete(term); //your custom handling
                      data.sort(function(a,b) {
    a = a.toLowerCase();
    b = b.toLowerCase();
    if( a == b) return 0;
    return a < b ? -1 : 1;
});
        response(data.slice(0,10));
    }
});

function recursiveListUpdate(autofill, keywords) {
    newList = [];
    autofill.forEach(function(element) {
        if (element.toLowerCase().indexOf(keywords[counter].toLowerCase())>= 0) {
            newList.push(decodeURI(element)); //if any %20, etc symbols
        }
    });
    counter++;
    if (counter == keywords.length) {
        return newList;
    }
    return recursiveListUpdate(newList, keywords);
}

function handleAutocomplete(term) {
    var str = term; // get the "keywords"
    var keywordsFromNameInput = str.split(" "); // split them into a list
    counter =0 ; // set counter for keywords.length
    // recursively get new list that contains the given keywords no matter the order
    return recursiveListUpdate(autofill, keywordsFromNameInput);
}

  $.ui.autocomplete.prototype._renderItem = function (ul, item) {
        var t = String(item.value).replace(
                new RegExp(this.term, "gi"),
                "<strong>$&</strong>");
        return $("<li></li>")
            .data("item.autocomplete", item)
            .append("<a>" + t + "</a>")
            .appendTo(ul);
    };
});
});
</script>

Please Log in to join the conversation.

  • tpartner
  • tpartner's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
2 years 9 months ago #226652 by tpartner
Replied by tpartner on topic Array - Text (Col1), Text (Col2), Slider(Col3)
Why are you loading jQuery.js again? It is already loaded by LimeSurvey. This may cause conflicts.

Check the console for errors caused by your autocomplete script. It seems quite cryptic.

Cheers,
Tony Partner

Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.

Please Log in to join the conversation.

  • Joffm
  • Joffm's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
2 years 9 months ago - 2 years 9 months ago #226660 by Joffm
Hm,
if I use the autocomplete solutions here (with the use of {QID} instead of hardcoding , etc. )
[url] www.limesurvey.org/manual/Workarounds:_M...r_LimeSurvey_2.05.2B [/url]

it works fine.
 

Joffm
 

Volunteers are not paid.
Not because they are worthless, but because they are priceless
Last edit: 2 years 9 months ago by Joffm.
The following user(s) said Thank You: tpartner

Please Log in to join the conversation.

  • Joffm
  • Joffm's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
2 years 9 months ago #226662 by Joffm
To be more precise:
I used this script:
Code:
<link href="/upload/surveys/{SID}/files/jquery-ui.css" rel="stylesheet" />
<script src="/upload/surveys/{SID}/files/jquery-ui.js"></script>
<script src="/upload/surveys/{SID}/files/jquery.csv.min.js"></script>
 
<script type="text/javascript" charset="utf-8">
  $(document).on('ready pjax:complete',function() {
    // Hier wird der Speicherort der Datei angegeben
    var url = "/upload/surveys/{SID}/files/names.csv";
 
    var Names = new Array();
 
    $.get(url,function(data){
      fullArray = $.csv.toArrays(data);
      $(fullArray).each(function(i, item){
        Names.push(item[0]);
      });
 
        $('#question{QID} .answer_cell_X001 input[type="text"]').autocomplete({
        minLength: 2,
        source: Names
      });
    });
  });
</script>

Joffm

Volunteers are not paid.
Not because they are worthless, but because they are priceless

Please Log in to join the conversation.

  • 2022needhelp
  • 2022needhelp's Avatar Topic Author
  • Offline
  • New Member
  • New Member
More
2 years 9 months ago #226664 by 2022needhelp
Replied by 2022needhelp on topic Array - Text (Col1), Text (Col2), Slider(Col3)
Thank you very much for your help! I will test the script for the autocomplete.

Please Log in to join the conversation.

  • 2022needhelp
  • 2022needhelp's Avatar Topic Author
  • Offline
  • New Member
  • New Member
More
2 years 9 months ago #226680 by 2022needhelp
Replied by 2022needhelp on topic Array - Text (Col1), Text (Col2), Slider(Col3)
I wanted to get started with the autocomplete using your posted script. In the process, I noticed one more thing. We had used the script I posted because it allows only certain answers to be suggested to the participant in the autocomplete depending on the selection in the previous question. So for example if the participant selects California in a previous question, only facilities from CA will be shown to him in the autocomplete. Is there a way to modify this script or do you know another way to use the autocomplete and make it work with the sliders?

Please Log in to join the conversation.

Moderators: tpartnerholch

Lime-years ahead

Online-surveys for every purse and purpose