Toggle menu
876
3.8K
30.2K
279.1K
Catglobe Wiki
Toggle preferences menu
Toggle personal menu
Not logged in
Your IP address will be publicly visible if you make any edits.

Reverse single grid between sub questions and answer options: Difference between revisions

From Catglobe Wiki
Cg loc (talk | contribs)
Cg loc (talk | contribs)
Line 34: Line 34:
   var mytable =  $("table[class='grid_inner']");
   var mytable =  $("table[class='grid_inner']");
   var numberOfQuestions = this.questions.length;
   var numberOfQuestions = this.questions.length;
   var numberOfAnswers = quest.questions[1].options.length;
   var numberOfAnswers = quest.questions[0].options.length;
   // get all radio buttons in the Grid
   // get all radio buttons in the Grid
   var radioButtonsEven = $("td[class='grid_subquestion_even']");
   var radioButtonsEven = $("td[class='grid_subquestion_even']");
Line 86: Line 86:
   mytable.empty();
   mytable.empty();
   mytable.append(newTable.children()[0]);     
   mytable.append(newTable.children()[0]);     
}
  var ua = navigator.userAgent.toLowerCase();
</source>
    if ( ua.indexOf( "firefox" ) == -1 && ua.indexOf( "safari" ) == -1) {
    $("#query").append("<INPUT id='dir' type=hidden name='dir'>"); 
      }
}</source>

Revision as of 05:08, 6 February 2012

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:

After reversing, we have a Single Grid like this:

Solution

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

Code

var normaloptClick = optclick;
this.optclick = function(slbl, lidx, blnk)
{
   return normaloptClick(slbl, lidx, blnk);
}

quest.onInit = function()
{
   var i,j; 
   // declare variables
   var mytable =  $("table[class='grid_inner']");
   var numberOfQuestions = this.questions.length;
   var numberOfAnswers = quest.questions[0].options.length;
   // get all radio buttons in the Grid
   var radioButtonsEven = $("td[class='grid_subquestion_even']");
   var radioButtonsOdd = $("td[class='grid_subquestion_odd']");

// ~~~~~~`swap column to row and inversely~~~~~~


 
// swap radio buttons
   var newTable = $("<table>");
   var ao;
   var sq; 
   var aoName; 
   var sqName;    
   var firstRow = $("<tr>");
   var posAo;
   var posSq;
   firstRow.append($(".grid_empty_cell"));
   for(i=0;i<numberOfQuestions;i++)
   {
      posAo = i + 1;
      sqName = "td[id='grid_subquestion_text_" + posAo +"']";
      sq =$(sqName);
      firstRow.append(sq);
   }
   newTable.append(firstRow);
      
   var newTr;
   for(i=0;i<numberOfAnswers;i++)
   {
      
      posSq = i + 1;
      aoName = "td[id='grid_answeroption_text_" + posSq +"']";
      ao =$(aoName);
      newTr = $('<tr>');
      newTr.append(ao);
      for(j=0;j<numberOfQuestions;j++)
      {
         if (j%2==0)
         {
            newTr.append(radioButtonsEven[Math.round(j/2)*numberOfAnswers+i]);
         }
         else
            newTr.append(radioButtonsOdd[(Math.round(j/2)-1)*numberOfAnswers+i]);
      }
      newTable.append(newTr);
      
   }
   newTable.append($("input[value='']"));
   mytable.empty();
   mytable.append(newTable.children()[0]);    
   var ua = navigator.userAgent.toLowerCase(); 
    if ( ua.indexOf( "firefox" ) == -1 && ua.indexOf( "safari" ) == -1) { 
     $("#query").append("<INPUT id='dir' type=hidden name='dir'>");  
      }
}