Welcome to the LimeSurvey Community Forum

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

Array filter exclusion

  • id_wahyu
  • id_wahyu's Avatar Topic Author
  • Offline
  • Junior Member
  • Junior Member
More
10 years 4 months ago - 10 years 4 months ago #101142 by id_wahyu
Array filter exclusion was created by id_wahyu
Hi All.
I need some advice and how to deal with "Array filter" or "Array filter exclusion".
It said that for Multiple choice question(s) (separated by semicolons).

My case:
Q1a_FM - Brand Awa FM
Q1a_OTH - Brand Awa Others
Q1b_FM - Ad Awa FM

So at Q1b_FM, at the "Array-filter" option I defined Q1a_FM;Q1a_OTH. But it's not working properly, only show the answer of Q1a_FM.

Any advice for me please.
Last edit: 10 years 4 months ago by id_wahyu. Reason: edit wording
The topic has been locked.
  • tpartner
  • tpartner's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
10 years 4 months ago #101153 by tpartner
Replied by tpartner on topic Array filter exclusion
When filtering by multiple questions, if an answer code exists in BOTH filter questions, it must be selected in BOTH filter questions before it will appear in the filtered question.

If an answer code only exists in ONE of the filter questions, it only needs to be selected in that question before it will appear in the filtered question.

Cheers,
Tony Partner

Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
The topic has been locked.
  • id_wahyu
  • id_wahyu's Avatar Topic Author
  • Offline
  • Junior Member
  • Junior Member
More
10 years 4 months ago #101161 by id_wahyu
Replied by id_wahyu on topic Array filter exclusion
Thanks Tony, for my case Q1a_FM and Q1a_OTH are exclusive each other.

How write the "Array filter" for multiple questions?
I tried "Q1a_FM; Q1a_OTH" but only show Q1a_FM input.

Thanks.
The topic has been locked.
  • tpartner
  • tpartner's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
10 years 4 months ago #101165 by tpartner
Replied by tpartner on topic Array filter exclusion
Can you attach an exported sample survey with that group?

Cheers,
Tony Partner

Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
The topic has been locked.
  • id_wahyu
  • id_wahyu's Avatar Topic Author
  • Offline
  • Junior Member
  • Junior Member
More
10 years 4 months ago #101168 by id_wahyu
Replied by id_wahyu on topic Array filter exclusion
Please find attached.

File Attachment:

File Name: h7cc8467.lsg
File Size:39 KB
Attachments:
The topic has been locked.
  • tpartner
  • tpartner's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
10 years 4 months ago #101171 by tpartner
Replied by tpartner on topic Array filter exclusion
Ah, now I see your problem. There is no way to satisfy the "must be selected in BOTH filter questions" requirement.

I think the only solution would be to insert a hidden question and use JavaScript to toggle its check-boxes as Q1a_FM and Q1a_OTH are answered. You could then filter Q1b_FM with the hidden question.

Cheers,
Tony Partner

Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
The topic has been locked.
  • id_wahyu
  • id_wahyu's Avatar Topic Author
  • Offline
  • Junior Member
  • Junior Member
More
10 years 4 months ago #101172 by id_wahyu
Replied by id_wahyu on topic Array filter exclusion
Thanks for the prompt reply Tony.
Do you have any sample or documentation on how to create that hidden question?
The topic has been locked.
  • tpartner
  • tpartner's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
10 years 4 months ago - 9 years 11 months ago #101177 by tpartner
Replied by tpartner on topic Array filter exclusion
1) Set up your survey to use JavaScript .

2) Copy Q1a_FM and place the new question after Q1a_OTH. Lets call the new question "Q1a_Hidden".

3) Filter Q1b_FM by Q1a_Hidden.

4) Add the following script to the source of Q1a_Hidden.

The script hides Q1a_Hidden and puts a listener on the check-boxes in Q1a_FM and Q1a_OTH. If either are found to be checked, the corresponding option in Q1a_Hidden is checked and the checkconditions() function is fired.

Code:
<script type="text/javascript" charset="utf-8">  
  $(document).ready(function(){
 
    // Identify some stuff
    var qHiddenID = {QID};
    var hiddenQuestion = $('#question'+qHiddenID);
    var prevQuestions = $(hiddenQuestion).prevAll('.multiple-opt');
    var q1ID = $(hiddenQuestion).prevAll('.multiple-opt:eq(1)').attr('id').replace(/question/, '');
    var q2ID = $(hiddenQuestion).prevAll('.multiple-opt:eq(0)').attr('id').replace(/question/, '');
 
    // Hide the hidden question
    $(hiddenQuestion).hide();
 
    // Listener on the first two multiple-choice questions
    $('input.checkbox', prevQuestions).change(function(event){
 
      // Identify some more stuff
      var thisQuestionCode = $(this).closest('.multiple-opt').attr('id').replace(/question/, '');
      var thisAnswerCode = $(this).attr('id').split('X'+thisQuestionCode)[1];
      var hiddenOption = $('input.checkbox[id$="X'+qHiddenID+thisAnswerCode+'"]');
 
      // Find if either of the filter options are checked
      var filterChecked = false;
      if($('input.checkbox[id$="X'+q1ID+thisAnswerCode+'"]').is(':checked') || $('input.checkbox[id$="X'+q2ID+thisAnswerCode+'"]').is(':checked')) {
        filterChecked = true;
      }
 
      // Toggle the hidden option accordingly
      if(filterChecked != $(hiddenOption).is(':checked')) {
        $(hiddenOption).trigger('click');
        checkconditions($(hiddenOption).attr('value'), $(hiddenOption).attr('name'), $(hiddenOption).attr('type'));
      }
    });
  });
</script>


Here is a working example:

File Attachment:

File Name: h2aadb73.lss
File Size:62 KB

Cheers,
Tony Partner

Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
Attachments:
Last edit: 9 years 11 months ago by tpartner.
The topic has been locked.
  • id_wahyu
  • id_wahyu's Avatar Topic Author
  • Offline
  • Junior Member
  • Junior Member
More
10 years 4 months ago #101198 by id_wahyu
Replied by id_wahyu on topic Array filter exclusion
Thanks for the help Tony.
But, it is working perfect if I use option "Group by Group", and is not with "Question by question" - the Q1a_hidden keep showing.

Any step that I missed Tony?
The topic has been locked.
  • tpartner
  • tpartner's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
10 years 4 months ago #101210 by tpartner
Replied by tpartner on topic Array filter exclusion
This solution will only work if all questions are on the same page. JavaScript can only manipulate elements that exist in the current page.

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: id_wahyu
The topic has been locked.

Lime-years ahead

Online-surveys for every purse and purpose