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:
== Challenge  ==
+
<h2> Challenge  </h2>
In order to restrict the value entered in a text grid question <br/>
+
<p>In order to restrict the value entered in a text grid question <br />
As a questionnaire creator<br/>
+
As a questionnaire creator<br />
I want to show a dropdown list inside the text grid<br/>
+
I want to show a dropdown list inside the text grid<br />
<br/>
+
<br />
[[Image:QuestionnaireTips SingleQuestionAsDropdownList.png]]
+
<img src="/images/1/1c/QuestionnaireTips_SingleQuestionAsDropdownList.png" _fck_mw_filename="QuestionnaireTips SingleQuestionAsDropdownList.png" alt="" />
 
+
</p>
== Solution ==
+
<h2> Solution </h2>
*Create a text grid question (Q3)
+
<ul><li>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)
+
</li><li>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
+
</li><li>Add javascript code to the text grid question to include the single question inside
==Code==
+
</li></ul>
<source lang="javascript">
+
<h2>Code</h2>
function SingleQuestion(q, selectedValue, even)
+
<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>
{
+
</p>
  this.label = "QUESTION." + q.label;
+
<h2> Code sample </h2>
  this.text = q.text;
+
<p>Open the qnaire "Js demo - some js samples" (Resource Id: 159684). View the sub question "Q1[0]" on the text grid "Q1"
  this.aoValues = q.aoValues;
+
</p>
  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

fckLRfunction SingleQuestion(q, selectedValue, even)fckLR{fckLR this.label = "QUESTION." + 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 = $("<select>").width("400px");fckLR var selected="";fckLR for(var i=0; i<n; i++)fckLR { fckLR if (this.aoValues[i] == this.selectedValue)fckLR selected = "true";fckLR elsefckLR selected = "";fckLR list.append($("<option>")fckLR .val(this.aoValues[i])fckLR .text(this.aoTexts[i])fckLR .attr("selected", 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 $("input:hidden").each(fckLR function(i)fckLR { fckLR if ($(this).attr("name") == s_label)fckLR $(this).val(list.val());fckLR fckLR }fckLR );fckLR fckLR }fckLR );fckLR var row = $("<tr>");fckLR var s;fckLR fckLR if (this.even)fckLR s = "even";fckLR elsefckLR s = "odd";fckLR row.append($("<td>").text(this.text).addClass("grid_subquestion_text grid_subquestion_"+s));fckLR row.append(fckLR $("<td>").addClass("grid_subquestion_"+ s)fckLR .append(list)fckLR )fckLR if (this.selectedValue == "")fckLR this.selectedValue = this.aoValues[0];fckLR row.append($("<input type = \"hidden\">").attr("name", this.label).val(this.selectedValue));fckLRfckLR return row;fckLR}fckLRfckLRquest.onInit = function()fckLR{fckLR //question in JSON formatfckLR var q3x = {"label":"Q3x","text":"Titel","aoValues":[1,2,3],"aoTexts":["Ejer","Adm. direktør","Økonomichef"]};fckLRfckLR //get the previous selected valuefckLR var selectedValue = "Template:Q3x.Value"; fckLR var q = new SingleQuestion(q3x, selectedValue, true);fckLR //append the single question inside the gridfckLR $(".grid_inner").append(q.getHTML());fckLR fckLR $("input:text").width("400px");fckLR}fckLR

Code sample

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