Difference between revisions of "Add an extra row before a sub question in a grid"

From Catglobe Wiki
Jump to: navigation, search
(Code)
Line 33: Line 33:
 
//get number of columns  
 
//get number of columns  
 
var n = $(".grid_inner")[0].rows[0].cells.length;  
 
var n = $(".grid_inner")[0].rows[0].cells.length;  
$(".grid_inner").find("tr").each(
+
var tr = $(".grid_inner").find("tr").eq(subQuestionIndex);
function(i)
+
{
+
  if (!includeAnswerOptionTexts)  
if (i == subQuestionIndex + 1)  
+
  {  
{
+
      $(this).before( $("<tr>")
if (!includeAnswerOptionTexts)  
+
        .append($("<td colspan="+n+">").text(text).addClass(cssClass))
{  
+
        );  
$(this).before( $("<tr>")
+
  }  
.append($("<td colspan="+n+">").text(text).addClass(cssClass))
+
  else  
);  
+
  {  
}  
+
      var tr = $("<tr>").append($("<td>").text(text).addClass(cssClass));  
else  
+
      for (var j=0; j<quest.questions[0].options.length; j++)  
{  
+
      {  
var tr = $("<tr>").append($("<td>").text(text).addClass(cssClass));  
+
        tr.append($("<td>").text(quest.questions[0].options[j].text).addClass(cssClass));  
for (var j=0; j<quest.questions[0].options.length; j++)  
+
      }  
{  
+
      $(this).before(tr);  
tr.append($("<td>").text(quest.questions[0].options[j].text).addClass(cssClass));  
+
  }
}  
 
$(this).before(tr);  
 
}  
 
}
 
}
 
)
 
 
}
 
}
  
 
quest.onInit = function()
 
quest.onInit = function()
{  
+
{
//insert a cell before Don't know on header
+
this.addRowBefore(3, "extra_row_header", "My sub question group", true);
var dontKnow_value = 3; 
 
this.addRowBefore(1, "extra_row_header", "My sub question group", true);
 
 
}
 
}
 
</source>
 
</source>

Revision as of 06:41, 2 March 2010

Challenge

In order to categorize sub questions in a grid question

As a questionnaire creator

I want to add extra rows to the grid

Example

I have a single grid question.

QuestionTips SingleGrid 1.jpg

I want to have an extra row to group some sub questions together.

QuestionTips SingleGrid 4.jpg

Solution

Find the row in the grid, add a new row before it.

Code

 1 //add a row before a sub question in the grid
 2 //subQuestionIndex: the index of sub question which we should add a row before
 3 //cssClass: the stylesheet that would be applied to the new row
 4 //text: name of the sub question group
 5 //includeAnswerOptionTexts: if it is true, answer option texts will be included in the new row
 6 quest.addRowBefore = function(subQuestionIndex, cssClass, text, includeAnswerOptionTexts)
 7 { 
 8 	//get number of columns 
 9 	var n = $(".grid_inner")[0].rows[0].cells.length; 
10 	var tr = $(".grid_inner").find("tr").eq(subQuestionIndex);
11 		 
12    if (!includeAnswerOptionTexts) 
13    { 
14       $(this).before( $("<tr>")
15          .append($("<td colspan="+n+">").text(text).addClass(cssClass))
16          ); 
17    } 
18    else 
19    { 
20       var tr = $("<tr>").append($("<td>").text(text).addClass(cssClass)); 
21       for (var j=0; j<quest.questions[0].options.length; j++) 
22       { 
23          tr.append($("<td>").text(quest.questions[0].options[j].text).addClass(cssClass)); 
24       } 
25       $(this).before(tr); 
26    } 			
27 }
28 
29 quest.onInit = function()
30 { 	
31 	this.addRowBefore(3, "extra_row_header", "My sub question group", true);
32 }