Welcome to the LimeSurvey Community Forum

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

Array filter from multiple questions

  • seguis
  • seguis's Avatar Topic Author
  • Offline
  • New Member
  • New Member
More
3 years 2 months ago #211919 by seguis
Array filter from multiple questions was created by seguis
Hello,
I have a complex array filter to set up under Version 3.19.1+191009, I have spent many hours reading the manual and the forums but have not managed to implement a solution so far. I understand how to implement an existing JavaScript code in a survey but cannot write code from scratch.
An example survey with 5 questions is in attachment. B9 is the question on which I would like to set up an array filter based on B9filter, but it's currently not working. B9 filter gets its options from two different multiple choice questions and I have added JavaScript to automatically check the boxes, but I'm not sure this is the right way to do it.
Ideally, I would have liked to use this to filter the answer options in a array by column question (B9b), but I'm not sure this is feasible at all and would already be grateful if B9 could work.
The details of the questions are below.
Kind regards,
Séverine

-Question A7a (multiple-choice): people select various activities
-Question A7b (multiple-choice): if people select options 7 or 8 at A7a, this questions appears with another list of activities
-B9filter (hidden multiple-choice): a question that picks up the answers to A7a and A7b using relevance equations. I have added JavaScript to check the boxes by default, but I'm not sure this is working as intended.
-B9: a radio list question that should be filtered with an array filter on B9filter but is currently not working
-B9b: what I would ideally need, an array by column with the answer options filtered on B9filter. If that's not possible, I can split the two columns in two radio list questions like B9.
The topic has been locked.
  • tpartner
  • tpartner's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
3 years 2 months ago #211924 by tpartner
Replied by tpartner on topic Array filter from multiple questions
You have the survey displayed in all-in-one mode. Can you use group-by-group, placing A7a and A7b on a previous screen?

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: DenisChenu
The topic has been locked.
  • seguis
  • seguis's Avatar Topic Author
  • Offline
  • New Member
  • New Member
More
3 years 2 months ago #211927 by seguis
Replied by seguis on topic Array filter from multiple questions
Yes, this would be possible.
The topic has been locked.
  • tpartner
  • tpartner's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
3 years 2 months ago #211930 by tpartner
Replied by tpartner on topic Array filter from multiple questions
In that case, I would use this JavaScript in the source of B9filter - it will only check relevant items and will also fire Expression Manager so the filter on B9 is activated.

Code:
<script type="text/javascript" data-author="Tony Partner">
  $(document).ready(function() {
    var thisQuestion = $('#question{QID}');
    $('.answer-item:not(.ls-irrelevant) input[type="checkbox"]', thisQuestion).prop('checked', true).trigger('change');
  });
</script>

Sample survey attached: 

File Attachment:

File Name: limesurvey...21_2.lss
File Size:71 KB


(I may have a JavaScript solution for B9b later today)

Cheers,
Tony Partner

Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
The topic has been locked.
  • tpartner
  • tpartner's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
3 years 2 months ago #211932 by tpartner
Replied by tpartner on topic Array filter from multiple questions
...oops, here is amended code allowing you to go back and forth in the survey:

Code:
<script type="text/javascript" data-author="Tony Partner">
  $(document).ready(function() {
    var thisQuestion = $('#question{QID}');
    $('input[type="checkbox"]', thisQuestion).prop('checked', false).trigger('change');
    $('.answer-item:not(.ls-irrelevant) input[type="checkbox"]', thisQuestion).prop('checked', true).trigger('change');
  });
</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.
  • seguis
  • seguis's Avatar Topic Author
  • Offline
  • New Member
  • New Member
More
3 years 2 months ago #211934 by seguis
Replied by seguis on topic Array filter from multiple questions
Thank you, this works perfectly! I would of course be grateful if you find a solution for B9b, but this already allows us to move on with testing the questionnaire further.
The topic has been locked.
  • tpartner
  • tpartner's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
3 years 2 months ago #211951 by tpartner
Replied by tpartner on topic Array filter from multiple questions
Here is the JS solution for filtering array-by-column rows based on a hidden multiple-choice question. The workaround assumes that the multiple choice question is in the same group and precedes the array question.
 
 
Code:
<script type="text/javascript" data-author="Tony Partner">
  $(document).on('ready pjax:scriptcomplete',function(){
 
    // Identify this question
    var qID = {QID};
    var thisQuestion = $('#question'+qID);
    var filterQuestion = thisQuestion.prevAll('.multiple-opt:eq(0)');
    var filterQID = filterQuestion.attr('id').replace(/question/, '');
 
    // Loop through irrelevant items in the filter question
    $('.answer-item.ls-irrelevant', filterQuestion).each(function(i) {
      var thisCode = $(this).attr('id').split('X'+filterQID)[1];      
      var arrayRow = $('input:radio[value="'+thisCode+'"]:eq(0)', thisQuestion).closest('tr');
 
      // Hide the corresponding row in the array question
      arrayRow.addClass('ls-hidden');
 
      // Un-check the radios in the corresponding row (in case of previous answers)
      $('input:radio', arrayRow).each(function(i) {
        var thisInput = $(this);
        thisInput.prop('checked', false);
        checkconditions('', thisInput.attr('name'), 'radio')  
      });
    });
  });
</script>
 
Sample survey attached: 

File Attachment:

File Name: limesurvey...21_3.lss
File Size:72 KB

Cheers,
Tony Partner

Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
The topic has been locked.
  • seguis
  • seguis's Avatar Topic Author
  • Offline
  • New Member
  • New Member
More
3 years 2 months ago #211957 by seguis
Replied by seguis on topic Array filter from multiple questions
Thank you very much, I will implement it next week.
The topic has been locked.
  • seguis
  • seguis's Avatar Topic Author
  • Offline
  • New Member
  • New Member
More
3 years 2 months ago #212018 by seguis
Replied by seguis on topic Array filter from multiple questions
Thank you very much for your help, the filter on the array by column by question works as wished for.
The topic has been locked.

Lime-years ahead

Online-surveys for every purse and purpose