Welcome to the LimeSurvey Community Forum

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

Tree question type for selecting multiple hierarchical topics

More
3 weeks 3 days ago #272903 by kdz
I did not find a question type that would allow me for a tree-style answering options. I want users of a survey to select topics from a hierarchical list. They should be able to select several topics or sub-topics by clicking on a hierarchical structure.
Is this possible at all?

+ O Plants
- O Animals
   L O Mammals
       L O Primates
       ....
+ L O Birds
 

Please Log in to join the conversation.

More
3 weeks 3 days ago #272905 by holch
This type of question does not exist out of the box. You would have to do some work around.

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.

More
3 weeks 3 days ago - 3 weeks 3 days ago #272908 by Joffm
Yes, it is possible.
 
But you should have answered the questions at the beginning about your version and the hosting.
And please provide a lss export of this question.

Joffm

 

Volunteers are not paid.
Not because they are worthless, but because they are priceless
Last edit: 3 weeks 3 days ago by Joffm.

Please Log in to join the conversation.

More
3 weeks 2 days ago #272920 by Joffm
Hi, the script relies on a certain structure of the subquestion codes
like this
 

Now insert this script into your question text (source code mode)
Code:
<script type="text/javascript" charset="utf-8">
 
 
  // A function to handle "child" checkboxes
  function dependantCheckboxes(qID, primaryCodes, secondaryCodes, tertiaryCodes) {
    // Identify the elements annd assign classes/attributes
    var thisQuestion = $('#question'+qID);
    thisQuestion.addClass('with-dependants');
    $.each(primaryCodes, function(i, val) {
      var thisItem = $('li[id$="X'+qID+val+'"]');
      $(thisItem).addClass('level-1 parent-item').attr('data-code', val).attr('data-level', '1');
    });
    $.each(secondaryCodes, function(i, val) {
      var thisItem = $('li[id$="X'+qID+val+'"]');
      var thisParent1 = $(thisItem).prevAll('li[data-level="1"]:eq(0)');
      $(thisItem).addClass('level-2 parent-item child-item').attr('data-code', val).attr('data-level', '2').attr('data-parent-1', $(thisParent1).attr('data-code'));
    });
    $.each(tertiaryCodes, function(i, val) {
      var thisItem = $('li[id$="X'+qID+val+'"]');
      var thisParent1 = $(thisItem).prevAll('li[data-level="1"]:eq(0)');
      var thisParent2 = $(thisItem).prevAll('li[data-level="2"]:eq(0)');
      $(thisItem).addClass('level-3 child-item').attr('data-code', val).attr('data-level', '3').attr('data-parent-1', $(thisParent1).attr('data-code')).attr('data-parent-2', $(thisParent2).attr('data-code'));
    });
 
    // A function to handle the states of child items
    function handleChildren(el) {
      var thisitem = $(el).closest('li');
      var thisCode = $(thisitem).attr('data-code');
      var thisLevel = $(thisitem).attr('data-level');
      var thisChildren = $('li[data-level="'+(Number(thisLevel)+1)+'"][data-parent-'+thisLevel+'="'+thisCode+'"]', thisQuestion);
 
      // Hide/show the secondary answers accordingly
      if (!$(el).is(':checked')) {
        $(thisChildren).fadeOut(300, function(e) {
          $('input:checkbox', thisChildren).prop('checked', false).trigger('change');
        });
      }
      else {
        $(thisChildren).fadeIn(300);
      }
    }
 
    // Initial states of the secondary answers
    $('.parent-item input:checkbox', thisQuestion).each(function(i) {
      handleChildren($(this));
    });
 
    // A listener on the primary answer to show or hide secondary answers
    $('.parent-item input:checkbox', thisQuestion).on('change', function(e) {
      handleChildren($(this));
    });
  }
 
 
  $(document).on('ready pjax:scriptcomplete',function(){
    dependantCheckboxes(
      {QID},
      ['SQ1', 'SQ2', 'SQ3'], // First-level sub-question codes
      ['SQ11', 'SQ12', 'SQ21', 'SQ22'], // Second-level sub-question codes
      ['SQ111', 'SQ112', 'SQ113', 'SQ121', 'SQ211', 'SQ212', 'SQ221', 'SQ222', 'SQ223'] // Third-level sub-question codes
    );
    });
</script>

For a two level design just leave empty the third-level codes.


And some css
Code:
<style type="text/css">.with-dependants .child-item {
            display: none;
        }
 
        .with-dependants li.level-2 {
            margin-left: 2.5em;
        }
 
        .with-dependants li.level-3 {
            margin-left: 5em;
        }
</style>

 
 

File Attachment:

File Name: limesurvey...2968.lss
File Size:34.84 KB

Joffm

Volunteers are not paid.
Not because they are worthless, but because they are priceless
The following user(s) said Thank You: Jmantysalo

Please Log in to join the conversation.

Moderators: tpartnerholch

Lime-years ahead

Online-surveys for every purse and purpose