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

From Catglobe Wiki
Jump to: navigation, search
Line 1: Line 1:
<h2> Challenge </h2>
+
== Challenge ==
<p>In order to restrict the value entered in a text grid question <br />
+
 
As a questionnaire creator<br />
+
In order to restrict the value entered in a text grid question<br/>As a questionnaire creator<br/>I want to show a dropdown list inside the text grid<br/><br/>&lt;img src="/images/1/1c/QuestionnaireTips_SingleQuestionAsDropdownList.png" _fck_mw_filename="QuestionnaireTips SingleQuestionAsDropdownList.png" alt="" /&gt;
I want to show a dropdown list inside the text grid<br />
+
 
<br />
+
== Solution ==
<img src="/images/1/1c/QuestionnaireTips_SingleQuestionAsDropdownList.png" _fck_mw_filename="QuestionnaireTips SingleQuestionAsDropdownList.png" alt="" />
+
 
</p>
+
*Create a text grid question (Q3)
<h2> Solution </h2>
+
*Create a dummy single question with the same answer options as we want to show inside the text grid question (Q3x)
<ul><li>Create a text grid question (Q3)
+
*Add javascript code to the text grid question to include the single question inside
</li><li>Create a dummy single question with the same answer options as we want to show inside the text grid question (Q3x)
+
 
</li><li>Add javascript code to the text grid question to include the single question inside
+
== Code ==
</li></ul>
+
 
<h2>Code</h2>
+
<source lang="javascript">
<p><span class="fck_mw_source" _fck_mw_customtag="true" _fck_mw_tagname="source" lang="javascript">fckLRfunction SingleQuestion(q, selectedValue, even)fckLR{fckLR  this.label = &quot;QUESTION.&quot; + q.label;fckLR  this.text = q.text;fckLR  this.aoValues = q.aoValues;fckLR  this.aoTexts = q.aoTexts; fckLR  this.selectedValue = selectedValue; fckLR  this.even = even;fckLR}fckLRfckLRSingleQuestion.prototype.getHTML =function()fckLR{fckLR  var n = this.aoValues.length;fckLR  var list = $(&quot;&lt;select&gt;&quot;).width(&quot;400px&quot;);fckLR  var selected=&quot;&quot;;fckLR  for(var i=0; i&lt;n; i++)fckLR  { fckLR      if (this.aoValues[i] == this.selectedValue)fckLR        selected = &quot;true&quot;;fckLR      elsefckLR        selected = &quot;&quot;;fckLR      list.append($(&quot;&lt;option&gt;&quot;)fckLR        .val(this.aoValues[i])fckLR        .text(this.aoTexts[i])fckLR        .attr(&quot;selected&quot;, selected)fckLR      );fckLR  }fckLR  //handle change event to change the value of the single questionfckLR  var s_label = this.label;fckLR  list.change(fckLR      function()fckLR      {fckLR        $(&quot;input:hidden&quot;).each(fckLR            function(i)fckLR            {                       fckLR              if ($(this).attr(&quot;name&quot;) == s_label)fckLR                  $(this).val(list.val());fckLR                  fckLR            }fckLR        );fckLR        fckLR      }fckLR  );fckLR  var row = $(&quot;&lt;tr&gt;&quot;);fckLR  var s;fckLR  fckLR  if (this.even)fckLR      s = &quot;even&quot;;fckLR  elsefckLR      s = &quot;odd&quot;;fckLR  row.append($(&quot;&lt;td&gt;&quot;).text(this.text).addClass(&quot;grid_subquestion_text grid_subquestion_&quot;+s));fckLR  row.append(fckLR      $(&quot;&lt;td&gt;&quot;).addClass(&quot;grid_subquestion_&quot;+ s)fckLR      .append(list)fckLR  )fckLR  if (this.selectedValue == &quot;&quot;)fckLR      this.selectedValue = this.aoValues[0];fckLR  row.append($(&quot;&lt;input type = \&quot;hidden\&quot;&gt;&quot;).attr(&quot;name&quot;, this.label).val(this.selectedValue));fckLRfckLR  return row;fckLR}fckLRfckLRquest.onInit = function()fckLR{fckLR  //question in JSON formatfckLR var q3x = {&quot;label&quot;:&quot;Q3x&quot;,&quot;text&quot;:&quot;Titel&quot;,&quot;aoValues&quot;:[1,2,3],&quot;aoTexts&quot;:[&quot;Ejer&quot;,&quot;Adm. direktør&quot;,&quot;Økonomichef&quot;]};fckLRfckLR  //get the previous selected valuefckLR var selectedValue = &quot;{{Q3x.Value}}&quot;; fckLR  var q = new SingleQuestion(q3x, selectedValue, true);fckLR //append the single question inside the gridfckLR $(&quot;.grid_inner&quot;).append(q.getHTML());fckLR fckLR $(&quot;input:text&quot;).width(&quot;400px&quot;);fckLR}fckLR</span>
+
function SingleQuestion(q, selectedValue, even)
</p>
+
{
<h2> Code sample </h2>
+
this.label = "QUESTION." + q.label;
<p>Open the qnaire "Js demo - some js samples" (Resource Id: 159684). View the sub question "Q1[0]" on the text grid "Q1"
+
this.text = q.text;
</p>
+
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");
 +
}
 +
</source>
 +
 
 +
== Code sample ==
 +
 
 +
Open the qnaire "Js demo - some js samples" (Resource Id: 159684). View the sub question "Q1[0]" on the text grid "Q1"

Revision as of 05:16, 17 October 2013

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

<img src="/images/1/1c/QuestionnaireTips_SingleQuestionAsDropdownList.png" _fck_mw_filename="QuestionnaireTips SingleQuestionAsDropdownList.png" alt="" />

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");
}

Code sample

Open the qnaire "Js demo - some js samples" (Resource Id: 159684). View the sub question "Q1[0]" on the text grid "Q1"