Filter a question with options

More
3 years 10 months ago #102575 by Cartha
Cartha created the topic: Filter a question with options
Hello All :)

I have Q2 filtred from Q1.

Q1 type = Array (Radio)
Q2 type = Array (checkbox)

Here is what I want to do exactly:


As you can see, I want to show in Q2 only what was selected in Q1 with the options "satisfied" or "not satisfied".
If an other option is selected, the subquestion should not appear on Q2.

How can I do that please ?
please note that in Q2, I put Q1 in the filed "array_filter", but this will show all selected subquestion (even those having option different than "satisfied" or "not satisfied).

Thank you for your help.

Please Log in to join the conversation.

More
3 years 10 months ago #102599 by tpartner
tpartner replied the topic: Filter a question with options
Here is a post that may give some insights into using JavaScript for column-specific array filtering - www.limesurvey.org/en/forum/can-i-do-thi...-another-array#91998

Cheers,
Tony Partner
Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.

Please Log in to join the conversation.

More
3 years 10 months ago #102609 by Cartha
Cartha replied the topic: Filter a question with options

tpartner wrote: Here is a post that may give some insights into using JavaScript for column-specific array filtering - www.limesurvey.org/en/forum/can-i-do-thi...-another-array#91998


Thank you for your answer :)

I tried it, even with some modification but could'nt get result.
Can you please help me with that and tell me what have I to change to adapt it to my form ?

Thanks again :)

Please Log in to join the conversation.

More
3 years 10 months ago #102616 by tpartner
tpartner replied the topic: Filter a question with options
Can you attach a sample survey?

Cheers,
Tony Partner
Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.

Please Log in to join the conversation.

More
3 years 10 months ago - 3 years 10 months ago #102619 by Cartha
Cartha replied the topic: Filter a question with options

tpartner wrote: Can you attach a sample survey?


Please find attached the exemple.

Note please that I included in that exemple your code (with some modifications). But it works in reverse.
I explain: I want to show subquestions in Q2 when option 3 and 4 are selected in Q1 and not to hidden them.
Subquestions in Q2 should not be visible when form is loaded.

Thank you :)
Attachments:
Last Edit: 3 years 10 months ago by Cartha. Reason: More explaination

Please Log in to join the conversation.

More
3 years 10 months ago #102622 by Cartha
Cartha replied the topic: Filter a question with options
Done after some modification :)

This is the code if it can help someone.
(Thanks a lot TPARTNER, you were great :) )
<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-multi-flexi: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)) == 2) {
				$(this).addClass('exclusive');
			}
			if($('input.radio', $(this).closest('tr')).index($(this)) == 3) {
				$(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();
		});
 
		$('tr[id^="javatbd"]', q2).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.checkbox:last', q2).attr('checked', true)
				// Hide the q2 row
				$('tr[id^="javatbd"]:eq('+rowIndex+')', q2).show();
			}
			else {
				// Un-check the "N/A" in the q2 row
				$('tr[id^="javatbd"]:eq('+rowIndex+') input.checkbox:last', q2).attr('checked', false)
				// Show the q2 row
				$('tr[id^="javatbd"]:eq('+rowIndex+')', q2).hide();
			}
			// 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>

Please Log in to join the conversation.

Start now!

Just create your account and start using Limesurvey today.

Register now