Welcome to the LimeSurvey Community Forum

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

Array effecting another array

  • Ugnius
  • Ugnius's Avatar Topic Author
  • Offline
  • New Member
  • New Member
More
11 years 1 month ago #91936 by Ugnius
Array effecting another array was created by Ugnius
Hello, I am a student and I am doing a year project which requires doing an survey. I decided that I want the surwey to be online and realised that I need some more powerful features which I found in Lime Survey, but since I am a begginer I came across some problems and now I am here asking for help...

My englsih language is also not the greatest, but I hope you will be able to understand my problem.

I'm trying to create a survey which would start with array like this
yesno
thing1OO
Thing2OO



then there would be another array
highlow
thing3OO
Thing4OO

And I want to make that if in the first array you choose answer "no" next to "thing1" , then you won't need to answer "thing3" in the seccond array and it would simply disappear.

This seemed obvious to somehow make it in question conditions, but after googling for a while I red that Lime Survey just simply doesn't support subquestion conditions.

Is there a way that I could still make this ? It seems like it would be possible with array filters and I tried downloading some examples from wiki but I just can't put my head around it...

Is there anyone willing to help me ?
The topic has been locked.
  • tpartner
  • tpartner's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
11 years 1 month ago #91966 by tpartner
Replied by tpartner on topic Array effecting another array
You can use JavaScript to do this but the details would depend on the question types. Can you attach a sample survey with those two questions?

Cheers,
Tony Partner

Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
The topic has been locked.
  • Ugnius
  • Ugnius's Avatar Topic Author
  • Offline
  • New Member
  • New Member
More
11 years 1 month ago #91981 by Ugnius
Replied by Ugnius on topic Array effecting another array
I made this sample survey with actual questions and answers in it to make it clear what I am after.

In the first array person chooses the amount of days per week that he spends on the certain things and in the second array he would choose the amount of hours per day on the same certain things.

But you know, if in the first array person answers that he doesn't spend any days per week on for example watching TV, than it would be less frustrating if in the second array sub question about hours per day watching TV would disappear.

And also another questions pops up asking why the person isn't watching TV which I already made in this sample survey.

I hope you were able to understand me and since I explained exactly what kind of information I'm trying to get from people and you or anybody else have a simpler idea on how to get this information than making two arrays like I did then please show me :)
The topic has been locked.
  • tpartner
  • tpartner's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
11 years 1 month ago #91986 by tpartner
Replied by tpartner on topic Array effecting another array
I understand now but maybe a better behaviour would be to only show rows in q2 if days are selected instead of hiding them. What do you you think?

Also, if you want to make q2 mandatory, can we add an "N/A" option that we can check if the row is hidden? (we can hide the "N/A" column from respondents)

Cheers,
Tony Partner

Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
The topic has been locked.
  • Ugnius
  • Ugnius's Avatar Topic Author
  • Offline
  • New Member
  • New Member
More
11 years 1 month ago #91989 by Ugnius
Replied by Ugnius on topic Array effecting another array
I personally like to see straight away how much questions will there be in a survey and that is why I want it to look the way I explained it before and I'm not sure which behaviour would be better. probably stick to the one, that is less time consuming to make.

And yes add an "N/A" option if it is what you need.
The topic has been locked.
  • tpartner
  • tpartner's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
11 years 1 month ago #91998 by tpartner
Replied by tpartner on topic Array effecting another array
Okay, here's a script that when placed in the source of the first array:
- Identifies the two arrays (we'll call them q1 and q2)
- Hides the "N/A" column of q2
- When a radio in column 1 of q1 is clicked, the corresponding row in q2 is hidden and its "N/A" option is selected
- When any other radio in q1 is clicked, the corresponding row in q2 is shown and its "N/A" option is de-selected
Code:
<script type="text/javascript" charset="utf-8">
  $(document).ready(function(){
 
    // Identify the questions
    var q1ID = '{QID}';
    var q1 = $('#question'+q1ID+'');
    var q2 = $(q1).nextAll('.array-flexible-row:eq(0)'); 
    var q2ID = $(q2).attr('id').split('question')[1];
 
    // Add some classes
    $('input.radio', q1).each(function(i){
      if($('input.radio', $(this).closest('tr')).index($(this)) == 0) {
        $(this).addClass('exclusive');
      }
    });
 
    // Hide the "N/A" column of q2
    $('thead th:last', q2).hide();
    $('tr[id^="javatbd"]', q2).each(function(i){
      $('td:last', this).hide();
    });
 
    // Initially Hide/Show the appropriate q2 rows
    $('input.radio:checked', q1).each(function(i){
      handleRows($(this));
    });
 
    // Click events on Q1 radios
    $('input.radio', q1).click(function() {
      handleRows($(this));
    });
    $('input.radio', q1).closest('td').click(function() {
      handleRows($('input.radio', this));
    });
 
    // A function to hide/show the appropriate q2 rows
    function handleRows(el) {
      var rowIndex = $('tr', $(el).closest('tbody')).index($(el).closest('tr'));
      if($(el).hasClass('exclusive')) {
        // Check the "N/A" in the q2 row
        $('tr[id^="javatbd"]:eq('+rowIndex+') input.radio:last', q2).attr('checked', true)
        // Hide the q2 row
        $('tr[id^="javatbd"]:eq('+rowIndex+')', q2).hide();
      }
      else {
        // Un-check the "N/A" in the q2 row
        $('tr[id^="javatbd"]:eq('+rowIndex+') input.radio:last', q2).attr('checked', false)
        // Show the q2 row
        $('tr[id^="javatbd"]:eq('+rowIndex+')', q2).show();
      }
      // Now, fix up the q2 row background colours
      var q2RowIndex = 0;
      $('tr[id^="javatbd"]:visible', q2).each(function(i, el){
        q2RowIndex ++;
        $(el).removeClass('array1, array2');
        if(q2RowIndex % 2 == 0) {
          $(el).addClass('array1');
        }
        else {
          $(el).addClass('array2');
        }
      });
      // Hide/show q2
      if($('.radio.exclusive:checked', q1).length == $('.radio.exclusive', q1).length) {
        $(q2).hide();
      }
      else {
        $(q2).show();
      }
    }
 
  });
</script>

Here's the survey back with the script in the first question of the first group.

File Attachment:

File Name: limesurvey...6_TP.lss
File Size:27 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.
  • Ugnius
  • Ugnius's Avatar Topic Author
  • Offline
  • New Member
  • New Member
More
11 years 1 month ago #92071 by Ugnius
Replied by Ugnius on topic Array effecting another array
It looks great and I'm really thankful, but of course there is a problem and I hope it is the last one.

I need this survey to be strictly to be in Lithuanian language without English language option.

As far as I can see there is no way to change the base language and I am not able to remake this survey myself with new base language.

Could you help me out on this and make another file that I could download with Lithuanian base language ?
The topic has been locked.
  • tpartner
  • tpartner's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
11 years 1 month ago #92082 by tpartner
Replied by tpartner on topic Array effecting another array
The code is fully portable and not affected by language.

You can copy and paste the script from the source of question code "F" to an array in your Lithuanian 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 2 months ago #103604 by LloydW
Replied by LloydW on topic Array effecting another array
I've successfully implemented this and adapted for the question styles I have. But I also have q3, q4 & q5 which like q2 depended on the check box of q1. How would I make this solution apply to the extra arrays - is there a simple way using a for() function?
The topic has been locked.
  • tpartner
  • tpartner's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
10 years 2 months ago #103606 by tpartner
Replied by tpartner on topic Array effecting another array
Not tested, but try changing this:
Code:
var q2 = $(q1).nextAll('.array-flexible-row:eq(0)');

To this:
Code:
var q2 = $(q1).nextAll('.array-flexible-row:eq(0), .array-flexible-row:eq(1), .array-flexible-row:eq(2), .array-flexible-row:eq(3)');


That will include the next 4 array questions in the "$(q2)" object.

Oh, and you can remove this - it's not required.:
Code:
var q2ID = $(q2).attr('id').split('question')[1];

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: LloydW
The topic has been locked.
More
10 years 2 months ago #103614 by LloydW
Replied by LloydW on topic Array effecting another array
Yes Tony that was what was required. Glad I asked as a neat and simple solution. :cheer:

MANY thanks, Lloyd.
The topic has been locked.
More
10 years 1 month ago #105005 by firstservey
Replied by firstservey on topic Array effecting another array
If I have dropdown menues instead of radibuttons?
The topic has been locked.

Lime-years ahead

Online-surveys for every purse and purpose