Reverse single grid between sub questions and answer options

From Catglobe Wiki
Revision as of 10:51, 2 February 2010 by Catglobe (talk | contribs)
Jump to: navigation, search

Challenge

Client would like to show Sub-Questions and Answer Options inversly on Single-Grid. Therefore, we have to reverse the columns and rows on Single-grid. By the way, we have to change the radio buttons accordingly.

Example

We have the original Single Grid like this:

Original SingleGrid 1.jpg

After reversing, we have a Single Grid like this:

After SingleGrid 4.jpg

Solution

1. Reverse Sub-Questions' names and Answer Options' names.
2. Rearrange the radio buttons accordingly.

Code

 1 var normaloptClick = optclick;
 2 this.optclick = function(slbl, lidx, blnk)
 3 {
 4    return normaloptClick(slbl, lidx, blnk);
 5 }
 6 
 7 quest.onInit = function()
 8 {
 9  var i,j; 
10  // declare variables
11  var mytable =  $("table[class='grid_inner']");
12  var numberOfQuestions = this.questions.length;
13  var numberOfAnswers = quest.questions[1].options.length;
14 // get all radio buttons in the Grid
15  var radioButtonsEven = $("td[class='grid_subquestion_even']");
16  var radioButtonsOdd = $("td[class='grid_subquestion_odd']");
17  
18 // swap radio buttons
19    var newTable = $("<table>");
20    var ao;
21    var sq; 
22    var aoName; 
23    var sqName;    
24    var firstRow = $("<tr>");
25    var posAo;
26    var posSq;
27    firstRow.append($(".grid_empty_cell"));
28    for(i=0;i<numberOfQuestions;i++)
29    {
30       posAo = i + 1;
31       sqName = "td[id='grid_subquestion_text_" + posAo +"']";
32       sq =$(sqName);
33       firstRow.append(sq);
34    }
35    newTable.append(firstRow);
36       
37    var newTr;
38    for(i=0;i<numberOfAnswers;i++)
39    {
40       
41       posSq = i + 1;
42       aoName = "td[id='grid_answeroption_text_" + posSq +"']";
43       ao =$(aoName);
44       newTr = $('<tr>');
45       newTr.append(ao);
46       for(j=0;j<numberOfQuestions;j++)
47       {
48          if (j%2==0)
49          {
50             newTr.append(radioButtonsEven[Math.round(j/2)*numberOfAnswers+i]);
51          }
52          else
53             newTr.append(radioButtonsOdd[(Math.round(j/2)-1)*numberOfAnswers+i]);
54       }
55       newTable.append(newTr);
56       
57    }
58    newTable.append($("input[value='']"));
59    mytable.empty();
60    mytable.append(newTable.children()[0]);      
61  
62 
63 }