Difference between revisions of "Show a single question as a dropdown list inside another text grid question"

From Catglobe Wiki
Jump to: navigation, search
m (Challenge)
(Code)
Line 12: Line 12:
 
==Code==
 
==Code==
 
<source lang="javascript">
 
<source lang="javascript">
var SingleQuestion = {
+
function SingleQuestion(q, selectedValue, even)
onInit: function(_label, _text, _answerOptionValues, _answerOptionTexts, _selectedValue)
+
{
{
+
  this.label = "QUESTION." + q.label;
this.label = "QUESTION." + _label;
+
  this.text = q.text;
this.text = _text;
+
  this.aoValues = q.aoValues;
this.aoValues = _answerOptionValues;
+
  this.aoTexts = q.aoTexts;
this.aoTexts = _answerOptionTexts;
+
  this.selectedValue = selectedValue;
this.selectedValue = _selectedValue;
+
  this.even = even;
},
+
}
getHTML: function()
 
{
 
var n = this.aoValues.length;
 
var list = $("<select>").width("400px");
 
var selected="";
 
for(var i=0; i<n; i++)
 
{
 
if (this.aoValues[i] == this.selectedValue)
 
selected = "true";
 
else
 
selected = "";
 
list.append($("<option>")
 
.val(this.aoValues[i])
 
.text(this.aoTexts[i])
 
.attr("selected", selected)
 
);
 
}
 
                //handle change event to change the value of the single question
 
list.change(
 
function()
 
{
 
$("input:hidden").each(
 
function(i)
 
{
 
if ($(this).attr("name") == SingleQuestion.label)
 
$(this).val(list.val());
 
 
}
 
);
 
 
}
 
);
 
var row = $("<tr>");
 
row.append($("<td>").text(this.text).addClass("grid_subquestion_text grid_subquestion_odd"));
 
row.append(
 
$("<td>").addClass("grid_subquestion_odd")
 
.append(list)
 
)
 
row.append($("<input type = \"hidden\">").attr("name", this.label).val(""));
 
  
return row;
+
SingleQuestion.prototype.getHTML =function()
}
+
{
 +
  var n = this.aoValues.length;
 +
  var list = $("<select>").width("400px");
 +
  var selected="";
 +
  for(var i=0; i<n; i++)
 +
  {
 +
      if (this.aoValues[i] == this.selectedValue)
 +
        selected = "true";
 +
      else
 +
        selected = "";
 +
      list.append($("<option>")
 +
        .val(this.aoValues[i])
 +
        .text(this.aoTexts[i])
 +
        .attr("selected", selected)
 +
      );
 +
  }
 +
  //handle change event to change the value of the single question
 +
  var s_label = this.label;
 +
  list.change(
 +
      function()
 +
      {
 +
        $("input:hidden").each(
 +
            function(i)
 +
            {                       
 +
              if ($(this).attr("name") == s_label)
 +
                  $(this).val(list.val());
 +
                 
 +
            }
 +
        );
 +
       
 +
      }
 +
  );
 +
  var row = $("<tr>");
 +
  var s;
 +
 
 +
  if (this.even)
 +
      s = "even";
 +
  else
 +
      s = "odd";
 +
  row.append($("<td>").text(this.text).addClass("grid_subquestion_text grid_subquestion_"+s));
 +
  row.append(
 +
      $("<td>").addClass("grid_subquestion_"+ s)
 +
      .append(list)
 +
  )
 +
  if (this.selectedValue == "")
 +
      this.selectedValue = this.aoValues[0];
 +
  row.append($("<input type = \"hidden\">").attr("name", this.label).val(this.selectedValue));
 +
 
 +
  return row;
 
}
 
}
  
 
quest.onInit = function()
 
quest.onInit = function()
 
{
 
{
var n = 8;
+
  //question in JSON format
var aoValues = new Array(n);
+
var q3x = {"label":"Q3x","text":"Titel","aoValues":[1,2,3],"aoTexts":["Ejer","Adm. direktør","Økonomichef"]};
var aoTexts = new Array(n);
 
for (var i=0; i<n; i++)
 
{
 
aoValues[i] = i+1;
 
}
 
        //answer option texts
 
aoTexts[0] = "Ejer";
 
aoTexts[1] = "Adm. direktør";
 
aoTexts[2] = "Økonomichef";
 
aoTexts[3] = "Personalechef";
 
aoTexts[4] = "Bogholder / økonomimedarbejder";
 
aoTexts[5] = "Medhjælpende ægtefælle";
 
aoTexts[6] = "Andet, notér på næste side";
 
aoTexts[7] = "Ved ikke";
 
 
 
        //get the previous selected value
 
var single_selectedValue = "{{Q3x.Value}}";
 
SingleQuestion.onInit("Q3x","Titel", aoValues, aoTexts, single_selectedValue);
 
  
        //append the single question inside the grid
+
  //get the previous selected value
$(".grid_inner").append(SingleQuestion.getHTML());
+
var selectedValue = "{{Q3x.Value}}";
 +
  var q = new SingleQuestion(q3x, selectedValue, true);
 +
//append the single question inside the grid
 +
$(".grid_inner").append(q.getHTML());
 
 
 
$("input:text").width("400px");
 
$("input:text").width("400px");
 
}
 
}
 
</source>
 
</source>

Revision as of 04:47, 22 June 2009

Challenge

In order to restrict the value entered in a text grid question
As a questionnaire creator
I want to show a dropdown list inside the text grid

QuestionnaireTips SingleQuestionAsDropdownList.png

Solution

  • Create a text grid question (Q3)
  • Create a dummy single question with the same answer options as we want to show inside the text grid question (Q3x)
  • Add javascript code to the text grid question to include the single question inside

Code

function SingleQuestion(q, selectedValue, even)
{
   this.label = "QUESTION." + q.label;
   this.text = q.text;
   this.aoValues = q.aoValues;
   this.aoTexts = q.aoTexts;		
   this.selectedValue = selectedValue;	
   this.even = even;
}

SingleQuestion.prototype.getHTML =function()
{
   var n = this.aoValues.length;
   var list = $("<select>").width("400px");
   var selected="";
   for(var i=0; i<n; i++)
   {		
      if (this.aoValues[i] == this.selectedValue)
         selected = "true";
      else
         selected = "";
      list.append($("<option>")
         .val(this.aoValues[i])
         .text(this.aoTexts[i])
         .attr("selected", selected)
      );
   }
   //handle change event to change the value of the single question
   var s_label = this.label;
   list.change(
      function()
      {
         $("input:hidden").each(
            function(i)
            {                        
               if ($(this).attr("name") == s_label)
                  $(this).val(list.val());
                  
            }
         );
         
      }
   );
   var row = $("<tr>");
   var s;
   
   if (this.even)
      s = "even";
   else
      s = "odd";
   row.append($("<td>").text(this.text).addClass("grid_subquestion_text grid_subquestion_"+s));
   row.append(
      $("<td>").addClass("grid_subquestion_"+ s)
      .append(list)
   )
   if (this.selectedValue == "")
      this.selectedValue = this.aoValues[0];
   row.append($("<input type = \"hidden\">").attr("name", this.label).val(this.selectedValue));

   return row;
}

quest.onInit = function()
{
   //question in JSON format
	var q3x = {"label":"Q3x","text":"Titel","aoValues":[1,2,3],"aoTexts":["Ejer","Adm. direktør","Økonomichef"]};

   //get the previous selected value
	var selectedValue = "{{Q3x.Value}}";	
   var q = new SingleQuestion(q3x, selectedValue, true);
	//append the single question inside the grid
	$(".grid_inner").append(q.getHTML());
	
	$("input:text").width("400px");
}