Welcome to the LimeSurvey Community Forum

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

Hide column in array question

  • giovbod
  • giovbod's Avatar Topic Author
  • Offline
  • New Member
  • New Member
More
3 years 1 month ago #212506 by giovbod
Hide column in array question was created by giovbod
Hi Everyone,
I have an array question (let's say 5 rows and 4 columns) and I want to hide one specific column according to the answer to the previous question.
Let's say:
Q1: (multiple coiches) Which column do you want to hide?
Possible answers: A1 = column A, A2 = column B, A3 = column C, A4=column D

I planned to put something like {if(Q1_A1=="Y"} as relevance equation, but I noticed there is no relevance equation for columns in arrays.
What should I do? I saw there is a plugin to hide a column if the label is empty, but it seems in my installation (limesurvey in docker) I cannot access via ssh and therefore I cannot install plugin (unless there is a way from the web interface ...).

Anyone can help me?

Than k you in advance
 

Please Log in to join the conversation.

  • tpartner
  • tpartner's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
3 years 1 month ago #212537 by tpartner
Replied by tpartner on topic Hide column in array question
Assuming that the filter question is on a previous page...

1) In the array answers, insert hidden <span> elements to hold the values of the filter checkboxes:



2) Place this code in the source of the array:

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 thisTable = $('table.subquestion-list:eq(0)', thisQuestion)
 
    $($('.answer-text .filter', thisTable).get().reverse()).each(function(i) {
      var thisIndex = $(this).closest('th').index();
      var filter = $.trim($(this).text());
      if($('span[id^="LEMtailor"]', this).length > 0) {
        filter = $.trim($('span[id^="LEMtailor"]', this).text())
      }
      if(filter == 'Y') {
        hideColumn(thisIndex)
      }
    });
 
    function hideColumn(index) {
      $('col:nth-child('+(index+1)+'), thead .answer-text:nth-child('+(index+1)+'), tr.answers-list .answer-item:nth-child('+(index+1)+')', thisTable).hide();
      $('tr.answers-list .answer-item:nth-child('+(index+1)+') :radio:checked', thisTable).each(function(i) {
        $(this).prop('checked', false);
        checkconditions('', $(this).attr('name'), $(this).attr('type'));
      });
    }
  });
</script>

Sample survey attached: 

File Attachment:

File Name: limesurvey...9381.lss
File Size:29 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: giovbod, tenroc

Please Log in to join the conversation.

More
2 years 6 months ago #219573 by tenroc
Replied by tenroc on topic Hide column in array question
Thanks a lot for the code!
It works great, but only for normal arrays. Could the same be achieved for Numbers arrays?

Please Log in to join the conversation.

More
2 years 6 months ago - 2 years 6 months ago #219885 by rolasj
Replied by rolasj on topic Hide column in array question
But this work only in array questions (with radio buttons) (example  limesurvey...9381.lss )
if i use array(numbers) it doesn't work for me

can anyone help?
Last edit: 2 years 6 months ago by rolasj.

Please Log in to join the conversation.

  • Joffm
  • Joffm's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
2 years 6 months ago #219886 by Joffm
Replied by Joffm on topic Hide column in array question
Please have a look at the plugin "hideEmptyColumn".
[url] gitlab.com/SondagesPro/QuestionSettingsType/hideEmptyColumn [/url]
Demo survey is included.

Joffm

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

Please Log in to join the conversation.

More
2 years 6 months ago #219988 by rolasj
Replied by rolasj on topic Hide column in array question
Thanks,
works great
i use limesurvey 3.17.1

... is it possible and how this can be done with just one survey or one question?

Please Log in to join the conversation.

More
2 years 6 months ago - 2 years 6 months ago #219990 by tenroc
Replied by tenroc on topic Hide column in array question
For anyone that isn't allowed to install plugins on their limesurvey instance, this works for me on numbered array:
Code:
<script type="text/javascript">
    $(document).on('ready pjax:scriptcomplete',function(){
 
        // Identify this question
        var qID = {QID};
        var thisQuestion = $('#question'+qID);
        var thisTable = $('table.subquestion-list:eq(0)', thisQuestion)
 
        $($('.ls-heading .filter', thisTable).get().reverse()).each(function(i) {
            var lineRead = $($('.ls-heading .filter', thisTable).get().reverse());
            var thisIndex = $(this).closest('th').index();
            var filter = $.trim($(this).text());
            if($('span[id^="LEMtailor"]', this).length > 0) {
                filter = $.trim($('span[id^="LEMtailor"]', this).text())
            }
            if(filter == 'Y') {
                hideColumn(thisIndex)
            }
        });
 
        function hideColumn(index) {
            $('td.answer-item:nth-child('+(index+1)+')', thisTable).hide();
            $('th.answertext:nth-child('+(index+1)+')', thisTable).hide();
            $('tr.ls-heading th:nth-child('+(index+1)+')', thisTable).hide();
            $('col:nth-child('+(index+1)+')', thisTable).hide();
        }
    });
</script>

This is just a slight modification of the code previously posted here for normal arrays. So the procedure is the same.
My javascript skills are pretty awful, be sure to test thoroughly before deploying this.
Last edit: 2 years 6 months ago by tpartner. Reason: Remove author name from script

Please Log in to join the conversation.

More
1 year 8 months ago - 1 year 8 months ago #230612 by ecoplan
Replied by ecoplan on topic Hide column in array question
The scripts for array questions from TPartner as well as the adapted one for array (numbers) from tenroc worked fine for version 5.3.26. As of yesterday with the update to 5.3.27 they are not working anymore. (With 3.25.9 all good too)

The same goes for Joffm's method in his array workaround tutorial (it's super helpful btw:https://forums.limesurvey.org/forum/german-forum/125239-joffms-tutorial-1-matrizen-stand-27-10-2021)
His method (chapter 14) was working fine, aso of yesterday with V 5.3.27 it's not.

Any ideas?
Last edit: 1 year 8 months ago by ecoplan.

Please Log in to join the conversation.

  • tpartner
  • tpartner's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
1 year 8 months ago #230621 by tpartner
Replied by tpartner on topic Hide column in array question
The problem is that for some reason the HTML structure of the array table was changed.

- bugs.limesurvey.org/view.php?id=18276

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: ecoplan

Please Log in to join the conversation.

  • tpartner
  • tpartner's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
1 year 8 months ago #230625 by tpartner
Replied by tpartner on topic Hide column in array question
In the meantime while I lobby for reverting the "fix", this will work for arrays.

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 thisTable = $('table.subquestion-list:eq(0)', thisQuestion)
 
    $($('.answer-text .filter', thisTable).get().reverse()).each(function(i) {
      var thisIndex = $(this).closest('.answer-text').index();
      var filter = $.trim($(this).text());
      if($('span[id^="LEMtailor"]', this).length > 0) {
        filter = $.trim($('span[id^="LEMtailor"]', this).text())
      }
      if(filter == 'Y') {
        hideColumn(thisIndex)
      }
    });
 
    function hideColumn(index) {
      $('col:nth-child('+(index+1)+'), .ls-heading .answer-text:nth-child('+(index+1)+'), tr.answers-list .answer-item:nth-child('+(index+1)+')', thisTable).hide();
      $('tr.answers-list .answer-item:nth-child('+(index+1)+') :radio:checked', thisTable).each(function(i) {
        $(this).prop('checked', false);
        checkconditions('', $(this).attr('name'), $(this).attr('type'));
      });
    }
  });
 
</script>

Sample survey attached: 

File Attachment:

File Name: limesurvey...1(1).lss
File Size:39 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: ecoplan

Please Log in to join the conversation.

More
1 year 8 months ago #230626 by ecoplan
Replied by ecoplan on topic Hide column in array question
Again big thank you tpartner for your efforts.
I should have noted the change in the table header elements (as of update to 5.3.27).

The adapted script works fine!
Let's see if I have to change back soon if the "fix" is reverted -> This is a reminder for everyone that the script in #230625 might be irrelevant again soon.

Please Log in to join the conversation.

  • tpartner
  • tpartner's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
1 year 8 months ago #230637 by tpartner
Replied by tpartner on topic Hide column in array question
This will work if the "fix" is reverted.

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: ecoplan

Please Log in to join the conversation.

Lime-years ahead

Online-surveys for every purse and purpose