Welcome, Guest
Username: Password: Remember me

TOPIC: Filter a question with options

Filter a question with options 7 months 5 days ago #102575

  • Cartha
  • Cartha's Avatar
  • OFFLINE
  • Fresh Lemon
  • Posts: 7
  • Karma: 0
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.
The administrator has disabled public write access.

Filter a question with options 7 months 5 days ago #102599

  • tpartner
  • tpartner's Avatar
  • OFFLINE
  • LimeSurvey Team
  • Posts: 4049
  • Thank you received: 739
  • Karma: 341
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.

LimeSurvey is open-source and run entirely by volunteers so please consider donating to support the project.
The administrator has disabled public write access.

Filter a question with options 7 months 5 days ago #102609

  • Cartha
  • Cartha's Avatar
  • OFFLINE
  • Fresh Lemon
  • Posts: 7
  • Karma: 0
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 :)
The administrator has disabled public write access.

Filter a question with options 7 months 5 days ago #102616

  • tpartner
  • tpartner's Avatar
  • OFFLINE
  • LimeSurvey Team
  • Posts: 4049
  • Thank you received: 739
  • Karma: 341
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.

LimeSurvey is open-source and run entirely by volunteers so please consider donating to support the project.
The administrator has disabled public write access.

Filter a question with options 7 months 5 days ago #102619

  • Cartha
  • Cartha's Avatar
  • OFFLINE
  • Fresh Lemon
  • Posts: 7
  • Karma: 0
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: 7 months 5 days ago by Cartha. Reason: More explaination
The administrator has disabled public write access.

Filter a question with options 7 months 5 days ago #102622

  • Cartha
  • Cartha's Avatar
  • OFFLINE
  • Fresh Lemon
  • Posts: 7
  • Karma: 0
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>
The administrator has disabled public write access.
Moderators: ITEd
Time to create page: 0.180 seconds
Donation Image