Welcome to the LimeSurvey Community Forum

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

Matrix - 3 columns - One column dependent - Individual drop-down choices

  • Katharina.Kuhls
  • Katharina.Kuhls's Avatar Topic Author
  • Offline
  • New Member
  • New Member
More
1 month 3 days ago #267941 by Katharina.Kuhls
Please help us help you and fill where relevant:
Your LimeSurvey version: [see right hand bottom of your LimeSurvey admin screen]
Own server or LimeSurvey hosting:
Survey theme/template:
==================
Hi, 

I desperately need your support to write a script for the following matrix: 

I need to have a matrix with 3 columns and 14 rows: 
The 14 rows contain 14 different criteria (see pictures) (Code: QY001-QY014). 
In the first column (Code: QX001) I need a drop-down where the respondents of the survey decide for each criterion, if it should be included or not. I achieved this already.
The second column (Code: QX002) should then be dependent on the first column. If the respondent chose in the first column 'yes, include', then they would need to choose a logic in the second column with a drop-down. The tricky part here is that each row would contain different drop-down choices for each criteria. 
For example, if the respondent decides to include criterion 1 (e.g. presence of NGOs), they must select the logic from a drop-down list in this second column. For this criterion, the choice would be between: Areas with a high presence of NGOs, Areas with a moderate presence of NGOs, Areas with a low or no presence of NGOs. For criterion 2, there would be other logics from which respondents would have to choose and so on. Please see pictures attached.  
For the third column (Code: QX003) I need a textfield, which would be optional and not be required to be filled out by respondents. 

Y-Skala Rows: 14 Criteria (QY001-QY014)
X-Skala Column:
QX001 Dropdown with the same drop-down choice
QX002 Dropdown with individual drop-down selection by row 
QX003 Textfield, which is not obligatory to fill out 

I hope, this was understandable. 

This is the script that I already worked on. Here the drop-down for the first column is already working. And the third column has a text field, however it's still obligatory to be filled out.
I would appreciate your help regarding the second column and making the textfield not obligatory. 

<p><script type="text/javascript" charset="utf-8">
  $(document).on('ready pjax:scriptcomplete',function(){
    var thisQuestion = $('#question{QID}');
 
    // Insert selects
    $('.answer-item.answer_cell_QX001', thisQuestion).addClass('with-select').append('<select class="inserted-select form-control list-question-select">\
  <option value="">Please select...</option>\
  <option value="1">Yes, include</option>\
  <option value="2">No, not include</option>\
</select>');  

// Listeners
$('.inserted-select', thisQuestion).on('change', function(i) {
if($(this).val() != '') {
$(this).closest('.answer-item').find('input:text').val($('option:selected',
this).val()).trigger('change');
}
else {
$(this).closest('.answer-item').find('input:text').val('').trigger('change');
}
});

// Returning to page
$('.with-select input:text', thisQuestion).each(function(i) {
var thisCell = $(this).closest('.answer-item');
var inputText = $.trim($(this).val());
$('select.inserted-select', thisCell).val(inputText);
});

  // Listeners on select elements
$('.inserted-select', thisQuestion).on('change', function(i) {
if($(this).val() != '') {
$(this).closest('.answer-item').find('input:text').val($.trim($('option:selected',
this).text())).trigger('keyup');
}
else {
$(this).closest('.answer-item').find('input:text').val('').trigger('keyup');
}
});

// Returning to page
$('.with-select input:text', thisQuestion).each(function(i) {
var thisCell = $(this).closest('.answer-item');
var inputText = $.trim($(this).val());
var selectval = $('select.inserted-select option', thisCell).filter(function () { return $(this).html()
== inputText; }).val();
$('select.inserted-select', thisCell).val(selectval);
});

// Clean-up styles
$('select.inserted-select', thisQuestion).css({
'max-width': '100%'
});
$('.with-select input:text', thisQuestion).css({
'position': 'absolute',
'left': '-9999em'
});
});
</script>
<style type="text/css">.custom-array table.subquestion-list col {
    width: auto !important;
  }
 
  .custom-array table.subquestion-list thead .column-0 {  width: 25%; }
  .custom-array table.subquestion-list thead .column-1 {  width: 20%; }
  .custom-array table.subquestion-list thead .column-2 {  width: 55%; }
</style>
</p>

Thank you in advance for your help!

Please Log in to join the conversation.

  • Joffm
  • Joffm's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
1 month 3 days ago #267943 by Joffm
Hi, Katharina,
yes this is possible, but you have to insert different "selects" into each cell, like
Code:
        // Insert selects into row 1 / column 2
        if($('tr.subquestion-list:eq(0) .answer-item.column-2 .inserted-select', thisQuestion).length == 0) {
            $('tr.subquestion-list:eq(0) .answer-item.column-2', thisQuestion).addClass('with-select').append('<select class="inserted-select form-control list-question-select">\
                <option value="">Please choose...</option>\
                <option value="1">VW</option>\
                <option value="2">FORD</option>\
                <option value="3">FIAT</option>\
            </select>');
        }

 

however it's still obligatory to be filled out.

It's obvious that this question can't be set to "mandatory"
1. If the second column is set to "readonly" because of "No" in the first column, the field is empty
2. If the third column is optional, the field may be empty.
So you have to use "question validation".

For further advise, please send the lss export of this part of your survey.
And please answer the questions at the top about your version of LimeSurvey.
Then I can show more.

Joffm

P.S.
If you are able to read Germen you may have a look at my "Tutorial 1: Matrizen".
In the German section, about page 15 +-2.



 

Volunteers are not paid.
Not because they are worthless, but because they are priceless

Please Log in to join the conversation.

  • Katharina.Kuhls
  • Katharina.Kuhls's Avatar Topic Author
  • Offline
  • New Member
  • New Member
More
1 month 2 days ago #267944 by Katharina.Kuhls
Hi Joffm,

vielen Dank für deine Hilfe!
Ich benutze die folgende Version: Version 3.28.76

Ich habe eine lsq Datei von der Frage hinzugefügt. Reicht dir das? 

Leider klappt es mit folgendem versuchten Code immer noch nicht:

<p><script type="text/javascript" charset="utf-8">
  $(document).on('ready pjax:scriptcomplete',function(){
    var thisQuestion = $('#question{QID}');
 
    // Insert selects
    $('.answer-item.answer_cell_QX001', thisQuestion).addClass('with-select').append('<select class="inserted-select form-control list-question-select">\
  <option value="">Please select...</option>\
  <option value="1">Yes, include</option>\
  <option value="2">No, not include</option>\
</select>');  

    // Listeners
$('.inserted-select', thisQuestion).on('change', function(i) {
if($(this).val() != '') {
$(this).closest('.answer-item').find('input:text').val($('option:selected',
this).val()).trigger('change');
}
else {
$(this).closest('.answer-item').find('input:text').val('').trigger('change');
}
});

    // Returning to page
$('.with-select input:text', thisQuestion).each(function(i) {
var thisCell = $(this).closest('.answer-item');
var inputText = $.trim($(this).val());
$('select.inserted-select', thisCell).val(inputText);
});
    
    // Insert selects into row 1 / column 2
        if($('tr.subquestion-list:eq(0). answer-item.column-2 .inserted-select', thisQuestion).length == 0) {
            $('tr.subquestion-list:eq(0). answer-item.column-2', thisQuestion).addClass('with-select').append('<select class="inserted-select form-control list-question-select">\
                <option value="">Select logic...</option>\
                <option value="1">Native or Primary Forests</option>\
                <option value="2">Secondary or Regenerating Forests</option>\
                <option value="3">Endangered or Rare Forest Types</option>\
                <option value="4">Mix of different forest types</option>\
            </select>');
        }

// Listeners
$('.inserted-select', thisQuestion).on('change', function(i) {
if($(this).val() != '') {
$(this).closest('.answer-item').find('input:text').val($('option:selected',
this).val()).trigger('change');
}
else {
$(this).closest('.answer-item').find('input:text').val('').trigger('change');
}
});

// Returning to page
$('.with-select input:text', thisQuestion).each(function(i) {
var thisCell = $(this).closest('.answer-item');
var inputText = $.trim($(this).val());
$('select.inserted-select', thisCell).val(inputText);
});

  // Listeners on select elements
$('.inserted-select', thisQuestion).on('change', function(i) {
if($(this).val() != '') {
$(this).closest('.answer-item').find('input:text').val($.trim($('option:selected',
this).text())).trigger('keyup');
}
else {
$(this).closest('.answer-item').find('input:text').val('').trigger('keyup');
}
});

// Returning to page
$('.with-select input:text', thisQuestion).each(function(i) {
var thisCell = $(this).closest('.answer-item');
var inputText = $.trim($(this).val());
var selectval = $('select.inserted-select option', thisCell).filter(function () { return $(this).html()
== inputText; }).val();
$('select.inserted-select', thisCell).val(selectval);
});

// Clean-up styles
$('select.inserted-select', thisQuestion).css({
'max-width': '100%'
});
$('.with-select input:text', thisQuestion).css({
'position': 'absolute',
'left': '-9999em'
});
});
</script>
<style type="text/css">.custom-array table.subquestion-list col {
    width: auto !important;
  }
 
  .custom-array table.subquestion-list thead .column-0 {  width: 25%; }
  .custom-array table.subquestion-list thead .column-1 {  width: 20%; }
  .custom-array table.subquestion-list thead .column-2 {  width: 55%; }
</style>
</p>


Könntest du mir weiterhelfen? Und mehr zeigen? U.a. wie das z.B. mit dem Validieren der Frage läuft.

Please Log in to join the conversation.

  • Katharina.Kuhls
  • Katharina.Kuhls's Avatar Topic Author
  • Offline
  • New Member
  • New Member
More
1 month 2 days ago #267945 by Katharina.Kuhls
lsq Datei  

File Attachment:

File Name: limesurvey...42-3.lsq
File Size:15 KB
 

Please Log in to join the conversation.

  • Joffm
  • Joffm's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
1 month 2 days ago #267946 by Joffm
Please, write English in this part of the forum!

And we always ask for lss exports, neither lsq nor lsg.
Reason:
lsq and lsg exports are language sensitive. You only can import them into a survey with the same base language.
So we have to create a survey - hopefully guessing the base language - create a group to be able to import a lsq export.
Furthermore the survey wide settings are missing, which may also be important.
Please, ease the life os us volunteers.

Joffm

Volunteers are not paid.
Not because they are worthless, but because they are priceless

Please Log in to join the conversation.

  • holch
  • holch's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
1 month 2 days ago #267947 by holch
To be honest, the first column is not necessary, at least not if you have everything in one table anyway.

If you would separate the first question, then you could filter the second one based on that and only show those that should be included. But if you have it all on one page anyway, get rid of the first column and just include "Do not include" as an answer option in the second question.

I answer at the LimeSurvey forum in my spare time, I'm not a LimeSurvey GmbH employee.
No support via private message.

Please Log in to join the conversation.

  • Katharina.Kuhls
  • Katharina.Kuhls's Avatar Topic Author
  • Offline
  • New Member
  • New Member
More
1 month 2 days ago #267948 by Katharina.Kuhls
Okay I am sorry, got it. 
Now I included the lss export. 

File Attachment:

File Name: limesurvey...1736.lss
File Size:89 KB

Please Log in to join the conversation.

  • Joffm
  • Joffm's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
1 month 2 days ago - 1 month 2 days ago #267951 by Joffm
Hi, here a first idea.
 
 


and instead of this long, long explanations in the question text, here a solution with "popovers"
 
These things you find in my "Tutorial 3: Gimmicks"

 

File Attachment:

File Name: limesurvey...36_J.lss
File Size:92 KB


You see the validation is really simple.
1. condition: The count of answers orf the first column has to be equal to the subquestion count
2. condition: The count of "Yes"-answers in the first column has to be equal to the count of answers in the second column.

Joffm

If you have further questions I'd prefer to communicate in the German part - without any language gap.
 

Volunteers are not paid.
Not because they are worthless, but because they are priceless
Last edit: 1 month 2 days ago by Joffm.

Please Log in to join the conversation.

  • Katharina.Kuhls
  • Katharina.Kuhls's Avatar Topic Author
  • Offline
  • New Member
  • New Member
More
1 month 2 days ago #267953 by Katharina.Kuhls
It works, and it's exactly how I imagined.
Thank you a lot for your time and effort Joffm. I highly appreciate it!

Please Log in to join the conversation.

Moderators: tpartnerholch

Lime-years ahead

Online-surveys for every purse and purpose