Welcome to the LimeSurvey Community Forum

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

Mixed array with text values and numbers

  • florian_filat
  • florian_filat's Avatar Topic Author
  • Offline
  • New Member
  • New Member
More
2 years 10 months ago #217603 by florian_filat
Mixed array with text values and numbers was created by florian_filat
Hi guys,
I am new with limesurvey and do not know/understand all the functionalities. I have studied some of the questions on the forum but did not manage how to set up the survey they way I need. Basically, I would like to have an array type question having the subquestions of different types: number and text values. I noticed that the basic settings allow either number or text values but not both.
The survey aims to evaluate some teachers on several dimensions/questions.
Attached you can find the type of array that I would like to generate:
- first column/question 1 would be number type with values between 1 to 5
- second column / question 2 would be text
- third column / question 3 would be text
- fourth column / question 4 would be a list box with 4 values: a, b, c, d
I attach also the survey sample.
Your help is really appreciated - and I say this after more than 6 hours spent on this. I realized that there is a need to write some javascript code for that which I did (managed to have the question array looked the way I wanted but the columns modified were not saved when the survey was executed - I guess I do not understand how it really functions).
Thanks a lot for your time and help.
Florian
 

File Attachment:

File Name: limesurvey...1976.lss
File Size:17 KB
The topic has been locked.
  • tpartner
  • tpartner's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
2 years 10 months ago #217605 by tpartner
Replied by tpartner on topic Mixed array with text values and numbers
LimeSurvey version?

Can you post the code that you tried?

Cheers,
Tony Partner

Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
The topic has been locked.
  • florian_filat
  • florian_filat's Avatar Topic Author
  • Offline
  • New Member
  • New Member
More
2 years 10 months ago #217627 by florian_filat
Replied by florian_filat on topic Mixed array with text values and numbers
Version 2.72.3+171020

Code that I tried:
code
<script type="text/javascript" charset="utf-8">
$(document).ready(function() {
// Identify the questions
var thisQuestion = $('#question{QID}');
var qHidden = thisQuestion.nextAll('.text-short:eq(0)');
var hiddenInput = $('input.text', qHidden);



var table = $( "tbody" );

$(table).children('tr').each(function () {
var td = $(this).children("td").eq(3);
var class_name = td.prop('class');
var text_input =`<td class="answer-cell-4 answer_cell_x4 answer-item text-item">
<label class="visible-xs-block read" for="answer931976X1064X21119Y4_x4">Question 4</label>
<input type="hidden" name="java931976X1064X21119Y4_x4" id="java931976X1064X21119Y4_x4"/>
<input type="text" name="931976X1064X21119Y4_x4" id="answer931976X1064X21119Y4_x4" class="form-control " size="12" value=""/>
</td>`
td.removeClass(class_name);
var new_class_name = "answer-cell-5 answer_cell_x4 question-item answer-item text-item";
td.addClass(new_class_name);
td.html(text_input);
});

$(table).children('tr').each(function () {
var td = $(this).children("td").eq(2);
var class_name = td.prop('class');
var text_input =`<td class="answer-cell-5 answer_cell_x3 question-item answer-item dropdown-item ">
<input type="hidden" name="java931976X1064X21119Y1_x2" id="java931976X1064X21119Y1_x3" value=""/>
<label for="answer931976X1064X21119Y1_x3" class='col-xs-12 col-sm-12'></label>
<div class="text-right visible-xs" id="label-answer931976X1064X21119Y1_x3">Question 3</div>
<select class="multiflexiselect form-control text-right"
name="931976X1064X21119Y1_x3"
id="answer931976X1064X21119Y1_x3"
onchange="fixnum_checkconditions(this.value, this.name, this.type)"
aria-labelledby="label-answer931976X1064X21119Y1_x3">
<option value="">...</option>
<option value="a" >1</option>
<option value="b" >2</option>
<option value="c" >3</option>
<option value="d" >4</option>
</select>
</td>`
td.removeClass(class_name);
var new_class_name = "answer-cell-5 answer_cell_x3 question-item answer-item text-item";
td.addClass(new_class_name);
td.html(text_input);
});

$(table).children('tr').each(function () {
var td = $(this).children("td").eq(1);
var class_name = td.prop('class');
var text_input =`<td class="answer-cell-4 answer_cell_x2 answer-item text-item">
<label class="visible-xs-block read" for="answer931976X1064X21119Y4_x4">Question 2</label>
<input type="hidden" name="java931976X1064X21119Y4_x2" id="java931976X1064X21119Y4_x2"/>
<input type="text" name="931976X1064X21119Y4_x2" id="answer931976X1064X21119Y4_x2" class="form-control " size="12" value=""/>
</td>`
td.removeClass(class_name);
var new_class_name = "answer-cell-5 answer_cell_x2 question-item answer-item text-item";
td.addClass(new_class_name);
td.html(text_input);
});

});
</script>
end code

For questions 2 and 4, the field displayed is text, as I need. For question 3, the options 1,2,3,4,5 are still the ones for generals settings and not a,b,c,d as I want. Also, none of the responses for questions 2 and 4 are saved - which means maybe the code modifies only the display of the questions in the front-end with no effect in back-end.

Thanks for any idea on that.
The topic has been locked.
  • tpartner
  • tpartner's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
2 years 10 months ago #217631 by tpartner
Replied by tpartner on topic Mixed array with text values and numbers
Your LimeSurvey version is very old but that code is for a version far older.

It's easier to insert drop-downs into an array-texts type question.

Add this script to the questions source:
 
 
Code:
<script type="text/javascript" data-author="Tony Partner">
 
  $(document).ready(function() {
    var thisQuestion = $('#question{QID}');
 
    // Column specific classes
    $('tr.subquestion-list', thisQuestion).each(function(i) {
      $('th, td', this).each(function(i) {
        $(this).addClass('column-'+i);
      });
    });
 
    // Insert selects into column 1
    if($('.answer-item.column-1 .inserted-select', thisQuestion).length == 0) {
      $('.answer-item.column-1', thisQuestion).addClass('with-select').append('<select class="inserted-select form-control list-question-select">\
        <option value="">Please choose...</option>\
        <option value="1">1</option>\
        <option value="2">2</option>\
        <option value="3">3</option>\
        <option value="4">4</option>\
        <option value="5">5</option>\
      </select>');
    } 
 
    // Insert selects into column 4
    if($('.answer-item.column-4 .inserted-select', thisQuestion).length == 0) {
      $('.answer-item.column-4', thisQuestion).addClass('with-select').append('<select class="inserted-select form-control list-question-select">\
        <option value="">Please choose...</option>\
        <option value="a">a</option>\
        <option value="b">b</option>\
        <option value="c">c</option>\
        <option value="d">d</option>\
      </select>');  
    } 
 
    // 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>



Sample survey attached: 

File Attachment:

File Name: limesurvey...6(2).lss
File Size:20 KB

Cheers,
Tony Partner

Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
The topic has been locked.
  • florian_filat
  • florian_filat's Avatar Topic Author
  • Offline
  • New Member
  • New Member
More
2 years 10 months ago #217633 by florian_filat
Replied by florian_filat on topic Mixed array with text values and numbers
This worked very well and was exactly what I was looking for. I am really impressed and I thank you very much!
Hats off, Tony!
The topic has been locked.

Lime-years ahead

Online-surveys for every purse and purpose