- Posts: 40
- Thank you received: 0
Welcome to the LimeSurvey Community Forum
Ask the community, share ideas, and connect with other LimeSurvey users!
Array filter between Scale 1 and Scale 2 in Dual Matrix
- Ryuniri_44052
- Topic Author
- Offline
- Banned
Less
More
9 years 4 months ago #117507
by Ryuniri_44052
Array filter between Scale 1 and Scale 2 in Dual Matrix was created by Ryuniri_44052
Dear all!
I am searching for a possibility to set an array filter in a dual matrix, that deactivates answer options (or the whole second scale) in scale 2, if a participant chooses a special answer option in scale 1.
EXAMPLE: As long as the participants select answer option 1 or 2 in scale 1 they might be interessted to answer the question in scale 2 as well for that row. But if the choose answer option (column) 3 in scale 1 the answer in scale 2 is more or less self explanatory. So, I would love to deactivate scale 2 depending on the answers in scale one - row by row.
Would be very useful, but I think it's not impossible - is it?
I know that it is possible to do that with two matrices, but I need this option for so many items, that my whole survey seems much longer if I ask for each item twice in two matrices.
I am searching for a possibility to set an array filter in a dual matrix, that deactivates answer options (or the whole second scale) in scale 2, if a participant chooses a special answer option in scale 1.
EXAMPLE: As long as the participants select answer option 1 or 2 in scale 1 they might be interessted to answer the question in scale 2 as well for that row. But if the choose answer option (column) 3 in scale 1 the answer in scale 2 is more or less self explanatory. So, I would love to deactivate scale 2 depending on the answers in scale one - row by row.
Would be very useful, but I think it's not impossible - is it?
I know that it is possible to do that with two matrices, but I need this option for so many items, that my whole survey seems much longer if I ask for each item twice in two matrices.
The topic has been locked.
- tpartner
- Offline
- LimeSurvey Community Team
Less
More
- Posts: 10149
- Thank you received: 3607
9 years 4 months ago #117571
by tpartner
Cheers,
Tony Partner
Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
Replied by tpartner on topic Array filter between Scale 1 and Scale 2 in Dual Matrix
To allow for a mandatory question, you would need to add an "N/A" column to the second scale.
Then set up your survey to use JavaScript and place the following script in the source of the question. The script will:
Then set up your survey to use JavaScript and place the following script in the source of the question. The script will:
- Hide the "N/A" column
- Put a listener on the scale-1-option-3 radios, so when clicked, the hidden "N/A" radio is checked and all other scale-2 radios in that row are disabled
Code:
<script type="text/javascript" charset="utf-8"> $(document).ready(function() { // Identify this question var thisQuestion = $('#question{QID}'); // Identify the "exclusive" column(s) // Multiple columns separated by commas var exclusiveCols = [3]; // Assign classes to various elements $('.answers-list', thisQuestion).each(function(i){ var column = 1; var scale = 1; $('td', this).each(function(i){ if($(this).hasClass('radio-item')) { $(this).addClass('scale-'+scale+' column-'+column+''); column++; } else { column = 1; scale = 2; } }); $('td:last', this).addClass('na-item'); }); $(exclusiveCols).each(function(i) { $('td.scale-1.column-'+this, thisQuestion).addClass('exclusive-item'); }); // Hide the "N/A" column $('td.na-item', thisQuestion).hide(); $('.questions-list .dsheader:last', thisQuestion).attr('colspan', Number($('.questions-list thead tr.groups .dsheader:last', thisQuestion).attr('colspan'))-1); $('.questions-list thead tr:not(.groups) th:last', thisQuestion).hide(); var colsWidth = $('col.odd:eq(0)', thisQuestion).attr('width').replace(/%/, '')*$('col.odd, col.even', thisQuestion).length; var newColWidth = colsWidth/($('col.odd, col.even', thisQuestion).length-1); $('col.odd, col.even', thisQuestion).attr('width', newColWidth+'%'); // Listener on the radios $('td.scale-1 input.radio', thisQuestion).click(function(e) { var thisCell = $(this).closest('td'); var thisRow = thisCell.closest('tr'); if(thisCell.hasClass('exclusive-item')) { $('.na-item input.radio', thisRow).trigger('click'); $('td.scale-2:not(.na-item) input.radio', thisRow).prop('disabled', true); } else { $('.na-item input.radio', thisRow).prop('checked', false); $('td.scale-2:not(.na-item) input.radio', thisRow).prop('disabled', false); } }); // Initial states $('td.exclusive-item input.radio:checked', thisQuestion).each(function(i) { var thisRow = $(this).closest('tr'); $('.na-item input.radio', thisRow).trigger('click'); $('td.scale-2:not(.na-item) input.radio', thisRow).prop('disabled', true); }); }); </script>
Cheers,
Tony Partner
Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
Attachments:
The topic has been locked.
- Ryuniri_44052
- Topic Author
- Offline
- Banned
Less
More
- Posts: 40
- Thank you received: 0
9 years 4 months ago #117867
by Ryuniri_44052
Replied by Ryuniri_44052 on topic Array filter between Scale 1 and Scale 2 in Dual Matrix
Thank you very much! Works perfectly.
I deleted that part, which is hiding the N/A column, because it's even better for me that the participants can see that their answer to the question in scale 2 is "no comment".
Thank you.
I deleted that part, which is hiding the N/A column, because it's even better for me that the participants can see that their answer to the question in scale 2 is "no comment".
Thank you.
The topic has been locked.
- Ryuniri_44052
- Topic Author
- Offline
- Banned
Less
More
- Posts: 40
- Thank you received: 0
9 years 3 months ago #118867
by Ryuniri_44052
Replied by Ryuniri_44052 on topic Array filter between Scale 1 and Scale 2 in Dual Matrix
Hey,
I tried to adopt this script for a dual matrix with drop-down menu. It's not working.
Is it possible to implement the same functionality into a dual matrix with a drop-down menu. Can you please tell me what I need to change?
I tried to adopt this script for a dual matrix with drop-down menu. It's not working.
Is it possible to implement the same functionality into a dual matrix with a drop-down menu. Can you please tell me what I need to change?
The topic has been locked.
- ferzago
- Offline
- New Member
Less
More
- Posts: 4
- Thank you received: 0
7 years 11 months ago #139083
by ferzago
Replied by ferzago on topic Array filter between Scale 1 and Scale 2 in Dual Matrix
Hi Tony, the solution works perfect on 2.06. I´ve upgraded to 2.50 and now doesn't works any more. I`m so frustated.
Any suggestion?
Thanks!
Any suggestion?
Thanks!
The topic has been locked.
- tpartner
- Offline
- LimeSurvey Community Team
Less
More
- Posts: 10149
- Thank you received: 3607
7 years 11 months ago #139085
by tpartner
Cheers,
Tony Partner
Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
Replied by tpartner on topic Array filter between Scale 1 and Scale 2 in Dual Matrix
Can you attach a small sample survey export file (.lss)?
Cheers,
Tony Partner
Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
The topic has been locked.
- ferzago
- Offline
- New Member
Less
More
- Posts: 4
- Thank you received: 0
7 years 11 months ago #139086
by ferzago
Replied by ferzago on topic Array filter between Scale 1 and Scale 2 in Dual Matrix
Thanks Tony for the prompt response!
Small sample attached.
Small sample attached.
Attachments:
The topic has been locked.
- tpartner
- Offline
- LimeSurvey Community Team
Less
More
- Posts: 10149
- Thank you received: 3607
7 years 11 months ago #139091
by tpartner
Cheers,
Tony Partner
Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
Replied by tpartner on topic Array filter between Scale 1 and Scale 2 in Dual Matrix
Try this for LimeSurvey version 2.5:
Adjusted survey attached:
Code:
<script type="text/javascript" charset="utf-8"> $(document).ready(function() { // Identify this question var thisQuestion = $('#question{QID}'); // Identify the "exclusive" column(s) // Multiple columns separated by commas var exclusiveCols = [2,3]; // Assign classes to various elements $('.answers-list', thisQuestion).each(function(i){ var column = 1; var scale = 1; $('td', this).each(function(i){ if($(this).hasClass('radio-item')) { $(this).addClass('scale-'+scale+' column-'+column+''); column++; } else if($(this).hasClass('dual_scale_separator')) { column = 1; scale = 2; } }); $('td:last', this).addClass('na-item'); }); $(exclusiveCols).each(function(i) { $('td.scale-1.column-'+this, thisQuestion).addClass('exclusive-item'); }); // Hide the "N/A" column $('td.na-item', thisQuestion).hide(); $('.questions-list .dsheader:last', thisQuestion).attr('colspan', Number($('.questions-list thead tr.groups .dsheader:last', thisQuestion).attr('colspan'))-1); $('.questions-list thead tr:not(.groups) th:last', thisQuestion).hide(); var responsesWidth = $.trim($.trim($('.col-responses col:eq(0)', thisQuestion).attr('style').split('width:')[1]).replace(/%;/, '')); var colsWidth = Number(responsesWidth)*$('.col-responses col', thisQuestion).length; var newResponsesWidth = colsWidth/($('.col-responses col', thisQuestion).length-1); $('.col-responses col', thisQuestion).css('width', newResponsesWidth+'%'); // Listener on the radios $('td.scale-1 input.radio', thisQuestion).click(function(e) { var thisCell = $(this).closest('td'); var thisRow = thisCell.closest('tr'); if(thisCell.hasClass('exclusive-item')) { $('.na-item input.radio', thisRow).trigger('click'); $('td.scale-2:not(.na-item) input.radio', thisRow).prop('disabled', true); } else { $('.na-item input.radio', thisRow).prop('checked', false); $('td.scale-2:not(.na-item) input.radio', thisRow).prop('disabled', false); } }); // Initial states $('td.exclusive-item input.radio:checked', thisQuestion).each(function(i) { var thisRow = $(this).closest('tr'); $('.na-item input.radio', thisRow).trigger('click'); $('td.scale-2:not(.na-item) input.radio', thisRow).prop('disabled', true); }); }); </script>
Adjusted survey attached:
Cheers,
Tony Partner
Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
Attachments:
The topic has been locked.
- ferzago
- Offline
- New Member
Less
More
- Posts: 4
- Thank you received: 0
7 years 11 months ago #139094
by ferzago
Replied by ferzago on topic Array filter between Scale 1 and Scale 2 in Dual Matrix
It works perfect!
Thank you very much!!
Thank you very much!!
The topic has been locked.
- saifshaikh
- Offline
- Junior Member
Less
More
- Posts: 23
- Thank you received: 0
6 years 4 months ago #164794
by saifshaikh
Replied by saifshaikh on topic Array filter between Scale 1 and Scale 2 in Dual Matrix
Hi Tony,
will this work if we change the same question from radio to drop-down menu. Can you please tell me what I need to change?
Thanks & regards
Saif
will this work if we change the same question from radio to drop-down menu. Can you please tell me what I need to change?
Thanks & regards
Saif
The topic has been locked.
- tpartner
- Offline
- LimeSurvey Community Team
Less
More
- Posts: 10149
- Thank you received: 3607
6 years 4 months ago - 6 years 4 months ago #164832
by tpartner
Cheers,
Tony Partner
Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
Replied by tpartner on topic Array filter between Scale 1 and Scale 2 in Dual Matrix
No, that code only works for radio inputs.
This will do the trick for array-dual-scale questions using drop-downs in version 3.4.x.
(I have added the facility to hide or show the "N/A" option in the second scale)
Sample survey attached:
This will do the trick for array-dual-scale questions using drop-downs in version 3.4.x.
(I have added the facility to hide or show the "N/A" option in the second scale)
Code:
<script type="text/javascript" charset="utf-8"> $(document).on('ready pjax:scriptcomplete',function(){ // Identify this question var thisQuestion = $('#question{QID}'); // Identify the "exclusive" answer(s) in scale one // Multiple answers separated by commas are possible var exclusiveValues = ['A4','A5']; // Hide the "N/A" option in scale 2 // Set this to false if you want that option always visible var hideNA = true; // Identify the "N/A" value in scale 2 var naValue = $('select[id$="_1"]:eq(0) option:last', thisQuestion).attr('value'); // A function to handle the drop-down behaviour function handleSelects(thisSelect) { var selectVal = $(thisSelect).val(); var thisRow = $(thisSelect).closest('tr'); var exclusive = false; $.each(exclusiveValues, function(i, val) { if(selectVal == val) { exclusive = true; } }); if(exclusive == true) { if(hideNA == true) { $('select[id$="_1"] option[value="'+naValue+'"]', thisRow).toggleOption(true); } $('select[id$="_1"]', thisRow).val(naValue).prop('disabled', true); } else { $('select[id$="_1"]', thisRow).prop('disabled', false); if(hideNA == true) { $('select[id$="_1"] option[value="'+naValue+'"]', thisRow).toggleOption(false); } } } // Listener on the drop-downs $('select[id$="_0"]', thisQuestion).on('change', function(e) { handleSelects($(this)); }); // Initial states $('select[id$="_0"]', thisQuestion).each(function(i) { handleSelects($(this)); }); // On submit $('#ls-button-submit').on('click', function(e) { $('select[id$="_1"]', thisQuestion).prop('disabled', false); }); }); $.fn.toggleOption = function(show) { jQuery(this).toggle(show); if(show) { if( jQuery(this).parent('span.toggleOption').length) jQuery(this).unwrap(); } else { if(jQuery(this).parent('span.toggleOption' ).length == 0) jQuery(this).wrap( '<span class="toggleOption" style="display: none;" />'); } }; </script>
Sample survey attached:
Cheers,
Tony Partner
Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
Attachments:
Last edit: 6 years 4 months ago by tpartner.
The topic has been locked.
- saifshaikh
- Offline
- Junior Member
Less
More
- Posts: 23
- Thank you received: 0
6 years 4 months ago #164931
by saifshaikh
Replied by saifshaikh on topic Array filter between Scale 1 and Scale 2 in Dual Matrix
Thanks Tony for the help.
In scale 1, can we have Option 4 selected by default. i could not find any option for having default option selected for array.
In scale 1, can we have Option 4 selected by default. i could not find any option for having default option selected for array.
The topic has been locked.