Welcome to the LimeSurvey Community Forum

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

In customized array quesiton, how to avoid disabled cell invalid when pagereload

  • LISHANGQI
  • LISHANGQI's Avatar Topic Author
  • Offline
  • Senior Member
  • Senior Member
More
1 year 2 months ago - 1 year 2 months ago #240566 by LISHANGQI
Please help us help you and fill where relevant:
Your LimeSurvey version: [3.28]
Own server or LimeSurvey hosting: Own
Survey theme/template: fruity
==================

Dear experts,

I'm using the array (text) question with dropdown lists designed by tpartner and letting the cells of one column be disabled according to the selection from the other column. This method is also well explained in Joffm's tutorial. Thanks again for your great help. 

But there is a problem, once the page is reloaded due to some unfilled mandatory questions or validation checks, the disabled cells will be activated again which might be very confusing to respondents:
 

I was wondering if there are any solutions to this problem?  I strived to write a JS script to check the selected options and trigger the event, but couldn't make it. 

Thank you in advance for any ideas or help you can provide!

Best regards,
Shann
Last edit: 1 year 2 months ago by LISHANGQI.

Please Log in to join the conversation.

  • tpartner
  • tpartner's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
1 year 2 months ago #240613 by tpartner
On page load, you will need to loop through all of the column-2 inserted drop-downs and handle the corresponding column-3 drop-down.

Code:
<script type="text/javascript" data-author="Tony Partner">
  $(document).on('ready pjax:scriptcomplete',function(){
    var thisQuestion = $('#question{QID}');
 
    // Insert selects
    $('.answer-item.answer_cell_X001', thisQuestion).addClass('with-select').append('<select class="inserted-select form-control list-question-select">\
        <option value="">...</option>\
        <optgroup label="Altona">\
        <option value="1">Altona-Altstadt</option>\
        <option value="2">Altona-Nord</option>\
        <option value="3">Bahrenfeld</option>\
        <option value="4">Blankenese</option>\
        </optgroup>\
        <optgroup label="Bergedorf">\
        <option value="5">Allermöhe</option>\
        <option value="6">Altengamme</option>\
        <option value="7">Billwerder</option>\
        </optgroup>\
    </select>');
    $('.answer-item.answer_cell_X002', thisQuestion).addClass('with-select').append('<select name="select1" class="inserted-select form-control list-question-select">\
        <option value="">...</option>\
        <option value="1">Fußball</option>\
        <option value="2">Handball</option>\
        <option value="3">Karate</option>\
        <option value="4">Leichtathletik</option>\
        <option value="5">Schwimmen</option>\
        <option value="6">Volleyball</option>\
    </select>');  
    $('select[name="select1"] option[value="2"]').attr('selected','selected');
    $('.answer-item.answer_cell_X003', thisQuestion).addClass('with-select').append('<select class="inserted-select form-control list-question-select">\
        <option value="">...</option>\
        <option value="1">U8 (2014 und jünger)</option>\
        <option value="2">U10 (2012-2013)</option>\
        <option value="3">U12 (2010-2011)</option>\
        <option value="4">U14 (2008-2009)</option>\
        <option value="5">U16 (2006-2007)</option>\
        <option value="6" selected>U18 (2004-2005)</option>\
    </select>');
 
    // Listeners
    $('.inserted-select', thisQuestion).on('change', function(i) {
      if($(this).val() != '') {
        $(this).closest('.answer-item').find('input:text').val($('option:selected', this).val()).trigger('change');
      }
      else {
        $(this).closest('.answer-item').find('input:text').val('').trigger('change');
      }
 
 
      // 3rd column conditional on 2nd column
      if($(this).closest('.answer-item').hasClass('answer_cell_X002')) {
        handleColumnX003($(this));
      }
    });
 
    function handleColumnX003(thisSelect) {
      var thisRow = $(thisSelect).closest('tr.subquestion-list');
      var item3 = $('.answer_cell_X003', thisRow);
      if($(thisSelect).val() < '3') {
        $('.inserted-select', item3).prop('disabled', false);
        $('input:text', item3).val('4').trigger('change');    
      }
      else {
        $('.inserted-select', item3).val('').prop('disabled', true);
        $('input:text', item3).val('').trigger('change');
      }
    }
 
    // Returning to page
    $('.with-select input:text', thisQuestion).each(function(i) {
      var thisCell = $(this).closest('.answer-item');
      var inputText = $.trim($(this).val());
      $('select.inserted-select', thisCell).val(inputText);
    });
    $('.answer_cell_X002 .inserted-select').each(function(i) {
      handleColumnX003($(this));
    });
 
    // Clean-up styles
    $('select.inserted-select', thisQuestion).css({
      'max-width': '100%'
    });
    $('.with-select input:text', thisQuestion).css({
      'position': 'absolute',
      'left': '-9999em'
    });
  });
</script>

Sample survey attached: 

File Attachment:

File Name: limesurvey...9941.lss
File Size:24 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, LISHANGQI

Please Log in to join the conversation.

  • LISHANGQI
  • LISHANGQI's Avatar Topic Author
  • Offline
  • Senior Member
  • Senior Member
More
1 year 2 months ago #240632 by LISHANGQI
Thanks a lot Mr.Partner! I've applied it to my survey and it works!

Please Log in to join the conversation.

Lime-years ahead

Online-surveys for every purse and purpose