Welcome to the LimeSurvey Community Forum

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

Hide column in array question

More
4 years 6 months ago #212506 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.

More
4 years 6 months ago #212537 by tpartner
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:28.64 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
3 years 11 months ago #219573 by tenroc
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
3 years 11 months ago - 3 years 11 months ago #219885 by rolasj
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: 3 years 11 months ago by rolasj.

Please Log in to join the conversation.

More
3 years 11 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
3 years 11 months ago #219988 by rolasj
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
3 years 11 months ago - 3 years 11 months ago #219990 by tenroc
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: 3 years 11 months ago by tpartner. Reason: Remove author name from script

Please Log in to join the conversation.

More
3 years 1 month ago - 3 years 1 month ago #230612 by ecoplan
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: 3 years 1 month ago by ecoplan.

Please Log in to join the conversation.

More
3 years 1 month ago #230621 by tpartner
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.

More
3 years 1 month ago #230625 by tpartner
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.46 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
3 years 1 month ago #230626 by ecoplan
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.

More
3 years 1 month ago #230637 by tpartner
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.

More
3 years 1 month ago #230663 by tpartner
The HTML changes have been reverted - bugs.limesurvey.org/view.php?id=18276#c71177

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

Please Log in to join the conversation.

More
3 years 1 month ago #230715 by ecoplan
that was fast! thanks for pushing this. we have 60+ surveys running atm with lots scripts from the forum. in the end I'm even more confident with LS. thanks!

Please Log in to join the conversation.

More
3 years 1 week ago - 3 years 1 week ago #231524 by IDEA_Rozwoju
What if I wanted to hide only the last column, without filtering on the previous questions (the last column contains the answers automatically filled in, for the answer "other").

In the past (version 2.06+) the following code, developed by tpartner, worked like a dream:
Code:
<script type="text/javascript" charset="utf-8">  
 
  $(document).ready(function() { 
 
    // Identify this question
    var thisQuestion = $('#question'+{QID}+'');
 
    // Add some column-specific classes
    $('table.subquestions-list tr', thisQuestion).each(function(i){
      $('> *', this).each(function(i){
        $(this).addClass('column-'+(i+1));
      });
    });
 
    // Hide all column-1 elements
    $('.column-1', thisQuestion).hide();   
    $('table.subquestions-list col', thisQuestion).css('width', 'auto');
 
  });
</script>

Now (5.3.31 version) this code is unfortunately not working.
Last edit: 3 years 1 week ago by IDEA_Rozwoju.

Please Log in to join the conversation.

Moderators: tpartnerholch

Lime-years ahead

Online-surveys for every purse and purpose