Jump to content
Toggle menu
  • 915 articles
  • 3.8K files
  • 30.2K users
  • 279.2K edits
Catglobe Wiki
Toggle preferences menu
Toggle personal menu
Not logged in
Your IP address will be publicly visible if you make any edits.

Showing Answer Option in tab: Difference between revisions

From Catglobe Wiki
Phamngocsonopenid (talk | contribs)
No edit summary
Line 1: Line 1:
== Challenge ==  
== Showing_Answer_Option_in_tab ==  
Showing answer option in Alphabetical tab   
We have a single question which has a lot of answer options. We want to show answer options in Alphabetical tab   


[[Image:Fetch)UID).INBOX.related_to_Questionnaire)2014.PNG]]
[[Image:Fetch)UID).INBOX.related_to_Questionnaire)2014.PNG]]
Line 14: Line 14:
== Code 1: No tab is selected by default ==  
== Code 1: No tab is selected by default ==  
<source lang="javascript">
<source lang="javascript">
/// Maximum number of column in tab.
/// Maximum number of column in tab.
var columnPerRow = 3;
var columnPerRow = 3;
/// Value of answer option which will not included in tab
 
/// 0 means include all answer option
 
var answerOptionValueWhichIsNotIncludeInTab = 8;
//answer option list to display outside of the tabs
var excludeAOs = [998,999];
 
Array.prototype.contains = function(item)
{
  var i;
  for(i=0; i<this.length; i++)
  {
      if (this[i] == item)
        return true;
  }
  return false;
}
 
//Overrider Array to find an item easier
Array.prototype.indexOf = function(character)
{
  for (var i = 0; i < this.length; i++)
      if (this[i] == character)
        return i;
  return -1;
}


quest.getHTML = function()
quest.getHTML = function()
{
{
    if (this.type != 1 && this.type != 2)
  if (this.type != 1 && this.type != 2)
        return;
      return;


    //This code used to set data to question.options
  //This code used to set data to question.options
    ans = this.answer.split("_|_");
  ans = this.answer.split("_|_");
    for (i = 0; i < ans.length; i++) {
  for (i = 0; i < ans.length; i++)  
        ans[i] = ans[i].split("_:_");
  {
    }
      ans[i] = ans[i].split("_:_");
  }


    for (i = 0; i < ans[0].length; i++) {
  for (i = 0; i < ans[0].length; i++)  
        if (ans[0][i].length > 0) {
  {
            for (j = 0; j < this.options.length; j++) {
      if (ans[0][i].length > 0)  
                if (this.options[j].value == ans[0][i]) {
      {
                    this.options[j].checked = true;
        for (j = 0; j < this.options.length; j++)  
                    if (ans.length > 1 && typeof this.options[j].open != "undefined" && typeof ans[1][i] != "undefined")
        {
                        this.options[j].open = ans[1][i];
            if (this.options[j].value == ans[0][i])  
                }
            {
              this.options[j].checked = true;
              if (ans.length > 1 && typeof this.options[j].open != "undefined" && typeof ans[1][i] != "undefined")
              this.options[j].open = ans[1][i];
             }
             }
        }
        }
    }
      }
 
  }
    var optionType = this.type == 1 ? 1 : 3;


    var sres = "";
  var optionType = this.type == 1 ? 1 : 3;
    sres += "<table border=" + this.bordersize + " class=\"question_outer\" cellpadding=\"0\" cellspacing=\"0\">";
    if (this.countdown > 0 && this.showcountdowndisplay) {
        sres += "<tr><td><div id=\"countdowndisplay\"></div></td></tr>";
    }
    sres += "<tr><td id=\"question_text\">" + this.text + "</td></tr>";
    sres += "<tr><td align=\"center\">";


    //Overrider Array to find an item easier
  var sres = "";
    Array.prototype.indexOf = function(character)
  sres += "<table border=" + this.bordersize + " class=\"question_outer\" cellpadding=\"0\" cellspacing=\"0\">";
    {
  if (this.countdown > 0 && this.showcountdowndisplay)  
        for (var i = 0; i < this.length; i++)
  {
            if (this[i] == character)
      sres += "<tr><td><div id=\"countdowndisplay\"></div></td></tr>";
            return i;
  }
        return -1;
  sres += "<tr><td id=\"question_text\">" + this.text + "</td></tr>";
    }
  sres += "<tr><td align=\"center\">";


    //Generate list of tabs
  //Generate list of tabs
    var tabList = new Array();
  var tabList = new Array();
    tabList.push(" #");
  tabList.push(" #");
    for (var i = 0; i < this.options.length; i++) {
  for (var i = 0; i < this.options.length; i++) {
        if (tabList.indexOf(this.options[i].text.trim().substr(0, 1).toUpperCase()) == -1)
    if (tabList.indexOf(this.options[i].text.trim().substr(0, 1).toUpperCase()) == -1)
            tabList.push(this.options[i].text.trim().substr(0, 1).toUpperCase());
        tabList.push(this.options[i].text.trim().substr(0, 1).toUpperCase());
    }
  }


    //Sort Alphabetical
  //Sort Alphabetical
    tabList.sort();
  tabList.sort();


    sres += "<div id='example_tab'>";
  sres += "<div id='example_tab' class='ui-tabs ui-widget ui-widget-content ui-corner-all'>";
    //Generate tab header UI
  //Generate tab header UI
    sres += "<ul class='ui-tabs-nav'>";
  sres += "<ul class='ui-tabs-nav ui-helper-reset ui-helper-clearfix ui-widget-header ui-corner-all'>";
    for (var i = 0; i < tabList.length; i++) {
  for (var i = 0; i < tabList.length; i++) {
        var tabHeaderContent = document.createElement("li");
    var tabHeaderContent = document.createElement("li");
        sres += "<li class='ui-tabs-selected'>";
    sres += "<li class='ui-state-default ui-corner-top'>";
        sres += "<a href='#tab_" + i + "'><span>" + tabList[i] + "</span></a></li>";
    sres += "<a href='#tab_" + i + "'><span>" + tabList[i] + "</span></a></li>";
    }
  }
    sres += "</ul>";
  sres += "</ul>";


    sres += "<div id='tab_" + 0 + "' class='ui-tabs-panel'></div>";
  sres += "<div id='tab_" + 0 + "' class='ui-tabs-panel ui-widget-content ui-corner-bottom'></div>";


    var answerOptionNotToIncluded;
  var answerOptionNotToIncluded = new Array();


    for (var i = 1; i < tabList.length; i++) {
  for (var i = 1; i < tabList.length; i++) {
        var tabContent = "";
    var tabContent = "";
        tabContent += "<div id='tab_" + i + "' class='ui-tabs-panel ui-tabs-hide'>";
    tabContent += "<div id='tab_" + i + "' class='ui-tabs-panel ui-widget-content ui-corner-bottom'>";


        //generate content for tab
    //generate content for tab
        tabContent += "<table><tbody><tr>";
    tabContent += "<table><tbody><tr>";
        var indexOfItemInTab = 1;
    var indexOfItemInTab = 1;
        var currentRow = 1;
    var currentRow = 1;
        for (var j = 0; j < this.options.length; j++) {
    for (var j = 0; j < this.options.length; j++) {
            if (this.options[j].text.trim().substr(0, 1).toUpperCase() != tabList[i])
        if (this.options[j].text.trim().substr(0, 1).toUpperCase() != tabList[i])
                continue;
            continue;


            if (answerOptionValueWhichIsNotIncludeInTab > 0 && this.options[j].value == answerOptionValueWhichIsNotIncludeInTab) {
        if (excludeAOs.contains(this.options[j].value)) {
                answerOptionNotToIncluded = this.options[j];
            answerOptionNotToIncluded.push(this.options[j]);
                continue;
            continue;
            }
        }


            tabContent += this.options[j].getHTML(optionType);
        tabContent += this.options[j].getHTML(optionType);
            indexOfItemInTab++;
        indexOfItemInTab++;
            if (indexOfItemInTab > columnPerRow * currentRow) {
        if (indexOfItemInTab > columnPerRow * currentRow) {
                tabContent += "</tr><tr>";
            tabContent += "</tr><tr>";
                currentRow++;
            currentRow++;
            }
        }
        }
    }


        if (this.options.lenght == 0)
    if (this.options.lenght == 0)
            tabContent = "<td>&nbsp;</td>";
        tabContent = "<td>&nbsp;</td>";
        tabContent += "</tr></tbody></table></div>";
    tabContent += "</tr></tbody></table></div>";


        if ($(tabContent).find("input").length <= 0)
    if ($(tabContent).find("input").length <= 0)
            __emptyTabSearchString = "a[href*='tab_" + i + "']";
        __emptyTabSearchString = "a[href*='tab_" + i + "']";
        else
    else
            sres += tabContent;
        sres += tabContent;
    }
  }
    if (answerOptionValueWhichIsNotIncludeInTab > 0)
  if (answerOptionNotToIncluded.length > 0)
        sres += "</div></td></tr><tr>" + answerOptionNotToIncluded.getHTML(optionType) + "</tr></table>";
  {
    else
      sres += "</div></td></tr><tr><td><table id='extra_options'>";
        sres += "</div></td></tr></table>";
      for(var i=0; i<answerOptionNotToIncluded.length; i++)
      {
        sres += "<tr>" + answerOptionNotToIncluded[i].getHTML(optionType) + "</tr>";
      }
      sres += "</table></td></tr></table>";
  }
  else
    sres += "</div></td></tr></table>";


    return sres;
  return sres;
};
};


Line 132: Line 158:
this.optclick = function(slbl, lidx, blnk)
this.optclick = function(slbl, lidx, blnk)
{
{
    var inputType = quest.type == 1 ? "radio" : "checkbox";
  var currentAOValue = quest.options[lidx].value;
 
  var inputType = quest.type == 1 ? "radio" : "checkbox";
    var qao, i, cb, o1, openInput, currentAOInput, val;
    cb = new Array();
    o1 = document["query"][slbl];
    cb = o1;
    qao = quest.options;
    currentAOInput = $("input[type='" + inputType + "'][value='" + quest.options[lidx].value + "']")[0];
    val = currentAOInput.value;


    //If user click on the link instead of the radiobutton/checkbox then check/uncheck the radio/checkbox
  var openInput, currentAOInput;     
    if (blnk && currentAOInput.type == "checkbox")
 
        currentAOInput.checked = !currentAOInput.checked;
  currentAOInput = $("input[type='" + inputType + "'][value='" + currentAOValue + "']")[0];
    else if (blnk)
 
        currentAOInput.checked = true;


    openInput = document["query"][slbl + ".Open." + val];
  if (excludeAOs.contains(currentAOValue))
    if ((typeof openInput) != "undefined") {
      return normaloptClick(slbl, lidx, blnk);
        if (currentAOInput.checked) {
            openInput.disabled = false;
            openInput.focus();
        } else {
            openInput.value = "";
            openInput.disabled = true;
        }
    }


    // Singleoption
  //If user click on the link instead of the radiobutton/checkbox then check/uncheck the radio/checkbox
    if (quest.type == 1 || (quest.type == 2 && quest.options[lidx].single)) {
  if (blnk && currentAOInput.type == "checkbox")
        for (var i = 0; i < cb.length; i++)
      currentAOInput.checked = !currentAOInput.checked;
            if (cb[i].value != val)
  else if (blnk)
                cb[i].checked = false;
      currentAOInput.checked = true;
    }
    else {
        for (var j = 0; j < quest.options.length; j++)
            if (quest.options[j].single)
                for (var i = 0; i < cb.length; i++)
                    if (cb[i].value == quest.options[j].value)
                        cb[i].checked = false;
    }


  openInput = document["query"][slbl + ".Open." + currentAOValue];
  if ((typeof openInput) != "undefined")
  {
      if (currentAOInput.checked)
      {
        openInput.disabled = false;
        openInput.focus();
      }
      else
      {
        openInput.value = "";
        openInput.disabled = true;
      }
  }     
  var o;
  for(var i=0; i<quest.options.length; i++)
  {
      o = quest.options[i];
      if (o.single && currentAOValue != o.value)
      {
        $("input[type='" + inputType + "'][value='" + o.value + "']").attr("checked", false);       
      }
  } 
}
}


quest.onInit = function()
quest.onInit = function()
{
{
    if (typeof(__emptyTabSearchString) != "undefined")
  if (typeof(__emptyTabSearchString) != "undefined")
         $(__emptyTabSearchString).parent().remove();
         $(__emptyTabSearchString).parent().remove();


    $("#example_tab > ul").tabs();
  $("#example_tab").tabs();
    $("li[class='ui-tabs-selected']").css("display", "none");
  $("a[href='#tab_0']").parent().css("display", "none");
 
  // set Next invisible onload
  //setVisibility(false);
  $("#extra_options").hide();
};
};
</source>
</source>
Line 187: Line 216:
== Code 2: Extra tab showing all answer options ==
== Code 2: Extra tab showing all answer options ==
<source lang="javascript">
<source lang="javascript">
/// Maximum number of column in tab.
/// Maximum number of column in tab.
var columnPerRow = 3;
var columnPerRow = 3;
/// Value of answer option which will not included in tab
/// 0 means include all answer option.
var answerOptionValueWhichIsNotIncludeInTab = 8;


quest.getHTML = function()
{
    if (this.type != 1 && this.type != 2)
        return;


    //This code used to set data to question.options
//answer option list to display outside of the tabs
    ans = this.answer.split("_|_");
var excludeAOs = [998,999];
    for (i = 0; i < ans.length; i++) {
        ans[i] = ans[i].split("_:_");
    }


    for (i = 0; i < ans[0].length; i++) {
Array.prototype.contains = function(item)
        if (ans[0][i].length > 0) {
{
            for (j = 0; j < this.options.length; j++) {
  var i;
                if (this.options[j].value == ans[0][i]) {
  for(i=0; i<this.length; i++)
                    this.options[j].checked = true;
  {
                    if (ans.length > 1 && typeof this.options[j].open != "undefined" && typeof ans[1][i] != "undefined")
      if (this[i] == item)
                        this.options[j].open = ans[1][i];
        return true;
                }
  }
            }
  return false;
        }
}
    }


    var optionType = this.type == 1 ? 1 : 3;
//Overrider Array to find an item easier
Array.prototype.indexOf = function(character)
{
  for (var i = 0; i < this.length; i++)
      if (this[i] == character)
        return i;
  return -1;
}


quest.getHTML = function()
{
  if (this.type != 1 && this.type != 2)
      return;


    var sres = "";
  //This code used to set data to question.options
    sres += "<table border=" + this.bordersize + " class=\"question_outer\" cellpadding=\"0\" cellspacing=\"0\">";
  ans = this.answer.split("_|_");
    if (this.countdown > 0 && this.showcountdowndisplay) {
  for (i = 0; i < ans.length; i++)  
        sres += "<tr><td><div id=\"countdowndisplay\"></div></td></tr>";
  {
    }
      ans[i] = ans[i].split("_:_");
    sres += "<tr><td id=\"question_text\">" + this.text + "</td></tr>";
  }
    sres += "<tr><td align=\"center\">";


    //Override Array to find an item easier
  for (i = 0; i < ans[0].length; i++)
    Array.prototype.indexOf = function(character)
  {
    {
      if (ans[0][i].length > 0)  
        for (var i = 0; i < this.length; i++)
      {
             if (this[i] == character)
        for (j = 0; j < this.options.length; j++)  
             return i;
        {
        return -1;
             if (this.options[j].value == ans[0][i])  
    }
             {
              this.options[j].checked = true;
              if (ans.length > 1 && typeof this.options[j].open != "undefined" && typeof ans[1][i] != "undefined")
              this.options[j].open = ans[1][i];
            }
        }
      }
  }


    //Generate list of tabs
  var optionType = this.type == 1 ? 1 : 3;
    var tabList = new Array();
    tabList.push(" All");
    for (var i = 0; i < this.options.length; i++) {
        if (tabList.indexOf(this.options[i].text.trim().substr(0, 1).toUpperCase()) == -1)
            tabList.push(this.options[i].text.trim().substr(0, 1).toUpperCase());
    }


    //Sort Alphabetical
  var sres = "";
    tabList.sort();
  sres += "<table border=" + this.bordersize + " class=\"question_outer\" cellpadding=\"0\" cellspacing=\"0\">";
  if (this.countdown > 0 && this.showcountdowndisplay)
  {
      sres += "<tr><td><div id=\"countdowndisplay\"></div></td></tr>";
  }
  sres += "<tr><td id=\"question_text\">" + this.text + "</td></tr>";
  sres += "<tr><td align=\"center\">";


    sres += "<div id='example_tab'>";
  //Generate list of tabs
    //Generate tab header UI
  var tabList = new Array();
    sres += "<ul class='ui-tabs-nav'>";
  tabList.push(" All");
    for (var i = 0; i < tabList.length; i++) {
  for (var i = 0; i < this.options.length; i++) {
        var tabHeaderContent = document.createElement("li");
    if (tabList.indexOf(this.options[i].text.trim().substr(0, 1).toUpperCase()) == -1)
        sres += "<li class='ui-tabs-selected'>";
        tabList.push(this.options[i].text.trim().substr(0, 1).toUpperCase());
        sres += "<a href='#tab_" + i + "'><span>" + tabList[i] + "</span></a></li>";
  }
    }
    sres += "</ul>";


    for (var i = 0; i < tabList.length; i++) {
  //Sort Alphabetical
        var tabContent = "";
  tabList.sort();
        if (i == 0)
            tabContent += "<div id='tab_" + i + "' class='ui-tabs-panel'>";
        else
            tabContent += "<div id='tab_" + i + "' class='ui-tabs-panel ui-tabs-hide'>";


        //generate content for tab
  sres += "<div id='example_tab' class='ui-tabs ui-widget ui-widget-content ui-corner-all'>";
        tabContent += "<table><tbody><tr>";
  //Generate tab header UI
        var indexOfItemInTab = 1;
  sres += "<ul class='ui-tabs-nav ui-helper-reset ui-helper-clearfix ui-widget-header ui-corner-all'>";
        var currentRow = 1;
  for (var i = 0; i < tabList.length; i++) {
    var tabHeaderContent = document.createElement("li");
    sres += "<li class='ui-state-default ui-corner-top'>";
    sres += "<a href='#tab_" + i + "'><span>" + tabList[i] + "</span></a></li>";
  }
  sres += "</ul>";


        for (var j = 0; j < this.options.length; j++) {
  var answerOptionNotToIncluded = new Array();
            if (i != 0 && this.options[j].text.trim().substr(0, 1).toUpperCase() != tabList[i])
                continue;


            var optionInput = this.options[j].getHTML(optionType);
  for (var i = 0; i < tabList.length; i++) {
            if (i == 0) {
    var tabContent = "";
    if(i==0)tabContent += "<div id='tab_" + i + "' class='ui-tabs-panel ui-widget-content ui-corner-bottom'>";
    else tabContent += "<div id='tab_" + i + "' class='ui-tabs-panel ui-tabs-hide'>";
    //generate content for tab
    tabContent += "<table><tbody><tr>";
    var indexOfItemInTab = 1;
    var currentRow = 1;
    for (var j = 0; j < this.options.length; j++) {
        if (i != 0 && this.options[j].text.trim().substr(0, 1).toUpperCase() != tabList[i])
            continue;
var optionInput = this.options[j].getHTML(optionType);
if (i == 0) {
                 while (optionInput.indexOf(this.label) != -1) {
                 while (optionInput.indexOf(this.label) != -1) {
                     optionInput = optionInput.replace(this.label, "TabAllAnswer");
                     optionInput = optionInput.replace(this.label, "TabAllAnswer");
                 }
                 }
            }
}
 
        if (i!=0 && excludeAOs.contains(this.options[j].value)) {
            if (i != 0 && answerOptionValueWhichIsNotIncludeInTab > 0 && this.options[j].value == answerOptionValueWhichIsNotIncludeInTab) {
            answerOptionNotToIncluded.push(this.options[j]);
                answerOptionNotToIncluded = this.options[j];
            continue;
                continue;
            }
        }


            tabContent += optionInput;
        tabContent += this.options[j].getHTML(optionType);
            indexOfItemInTab++;
        indexOfItemInTab++;
            if (indexOfItemInTab > columnPerRow * currentRow) {
        if (indexOfItemInTab > columnPerRow * currentRow) {
                tabContent += "</tr><tr>";
            tabContent += "</tr><tr>";
                currentRow++;
            currentRow++;
            }
        }
        }
    }


        if (this.options.lenght == 0)
    if (this.options.lenght == 0)
            tabContent = "<td>&nbsp;</td>";
        tabContent = "<td>&nbsp;</td>";
        tabContent += "</tr></tbody></table></div>";
    tabContent += "</tr></tbody></table></div>";


        if ($(tabContent).find("input").length <= 0)
    if ($(tabContent).find("input").length <= 0)
            __emptyTabSearchString = "a[href*='tab_" + i + "']";
        __emptyTabSearchString = "a[href*='tab_" + i + "']";
        else
    else
            sres += tabContent;
        sres += tabContent;
    }
  }
    if (answerOptionValueWhichIsNotIncludeInTab > 0)
  if (answerOptionNotToIncluded.length > 0)
        sres += "</div></td></tr><tr>" + answerOptionNotToIncluded.getHTML(optionType) + "</tr></table>";
  {
    else
      sres += "</div></td></tr><tr><td><table id='extra_options'>";
        sres += "</div></td></tr></table>";
      for(var i=0; i<answerOptionNotToIncluded.length; i++)
      {
        sres += "<tr>" + answerOptionNotToIncluded[i].getHTML(optionType) + "</tr>";
      }
      sres += "</table></td></tr></table>";
  }
  else
    sres += "</div></td></tr></table>";


    return sres;
  return sres;
};
};


Line 313: Line 365:
this.optclick = function(slbl, lidx, blnk)
this.optclick = function(slbl, lidx, blnk)
{
{
    var inputType = quest.type == 1 ? "radio" : "checkbox";
  var currentAOValue = quest.options[lidx].value;
    other_slbl = slbl == "TabAllAnswer" ? quest.label : "TabAllAnswer";
  var inputType = quest.type == 1 ? "radio" : "checkbox";
    var val = quest.options[lidx].value;
 
    var currentAOInput = $("input[type='" + inputType + "'][name='" + slbl + "'][value='" + val + "']")[0];
  var openInput, currentAOInput;    
    var other_currentAOInput = $("input[type='" + inputType + "'][name='" + other_slbl + "'][value='" + val + "']")[0];
 
  currentAOInput = $("input[type='" + inputType + "'][value='" + currentAOValue + "']")[0];
 


      // Singleoption
  if (excludeAOs.contains(currentAOValue))
    if (quest.type == 1 || (quest.type == 2 && quest.options[lidx].single)) {
      return normaloptClick(slbl, lidx, blnk);
        var InputNeedToBeUnCheck = $("input:" + inputType + "[value!='" + val + "']:checked");
        for (var i = 0; i < InputNeedToBeUnCheck.length; i++) {
            InputNeedToBeUnCheck[i].checked = false;
        }
    }
    else {
        for (var j = 0; j < quest.options.length; j++) {
            if (quest.options[j].single) {
                var InputNeedToBeUnCheck = $("input:" + inputType + "[value='" + quest.options[j].value + "']:checked");
                for (var i = 0; i < InputNeedToBeUnCheck.length; i++)
                    InputNeedToBeUnCheck[i].checked = false;
            }
        }
    }


    //If user click on the link instead of the radiobutton/checkbox then check/uncheck the radio/checkbox
  //If user click on the link instead of the radiobutton/checkbox then check/uncheck the radio/checkbox
    if (blnk && currentAOInput.type == "checkbox")
  if (blnk && currentAOInput.type == "checkbox")
        currentAOInput.checked = !currentAOInput.checked;
      currentAOInput.checked = !currentAOInput.checked;
    else if (blnk)
  else if (blnk)
        currentAOInput.checked = true;
      currentAOInput.checked = true;


    other_currentAOInput.checked = currentAOInput.checked;
  openInput = document["query"][slbl + ".Open." + currentAOValue];
  if ((typeof openInput) != "undefined")
  {
      if (currentAOInput.checked)
      {
        openInput.disabled = false;
        openInput.focus();
      }
      else
      {
        openInput.value = "";
        openInput.disabled = true;
      }
  }     
  var o;
  for(var i=0; i<quest.options.length; i++)
  {
      o = quest.options[i];
      if (o.single && currentAOValue != o.value)
      {
        $("input[type='" + inputType + "'][value='" + o.value + "']").attr("checked", false);        
      }
  } 
}
}


quest.onInit = function()
quest.onInit = function()
{
{
    if (typeof(__emptyTabSearchString) != "undefined")
  if (typeof(__emptyTabSearchString) != "undefined")
         $(__emptyTabSearchString).parent().remove();
         $(__emptyTabSearchString).parent().remove();
    $("#example_tab > ul").tabs();
 
  $("#example_tab").tabs();
  //$("a[href='#tab_0']").parent().css("display", "none");
 
  // set Next invisible onload
  //setVisibility(false);
  $("#extra_options").hide();
};
};
</source>
</source>
== Source ==
Questionnaire Resource Id on cg.catglobe.com site: 164079

Revision as of 08:02, 16 January 2012

Showing_Answer_Option_in_tab

We have a single question which has a lot of answer options. We want to show answer options in Alphabetical tab

Solution

Using Java Script property of question.

There are 2 type of alphabetical tabs.

  1. No tab is selected as default
  2. An extra tab showing all answer options which is selected as default

Code 1: No tab is selected by default

/// Maximum number of column in tab.
var columnPerRow = 3;


//answer option list to display outside of the tabs
var excludeAOs = [998,999];

Array.prototype.contains = function(item)
{
   var i;
   for(i=0; i<this.length; i++)
   {
      if (this[i] == item)
         return true;
   }
   return false;
}

 //Overrider Array to find an item easier
Array.prototype.indexOf = function(character)
{
   for (var i = 0; i < this.length; i++)
      if (this[i] == character)
         return i;
   return -1;
}

quest.getHTML = function()
{
   if (this.type != 1 && this.type != 2)
      return;

   //This code used to set data to question.options
   ans = this.answer.split("_|_");
   for (i = 0; i < ans.length; i++) 
   {
      ans[i] = ans[i].split("_:_");
   }

   for (i = 0; i < ans[0].length; i++) 
   {
      if (ans[0][i].length > 0) 
      {
         for (j = 0; j < this.options.length; j++) 
         {
            if (this.options[j].value == ans[0][i]) 
            {
               this.options[j].checked = true;
               if (ans.length > 1 && typeof this.options[j].open != "undefined" && typeof ans[1][i] != "undefined")
               this.options[j].open = ans[1][i];
            }
         }
      }
   }

   var optionType = this.type == 1 ? 1 : 3;

   var sres = "";
   sres += "<table border=" + this.bordersize + " class=\"question_outer\" cellpadding=\"0\" cellspacing=\"0\">";
   if (this.countdown > 0 && this.showcountdowndisplay) 
   {
      sres += "<tr><td><div id=\"countdowndisplay\"></div></td></tr>";
   }
   sres += "<tr><td id=\"question_text\">" + this.text + "</td></tr>";
   sres += "<tr><td align=\"center\">";  

   //Generate list of tabs
   var tabList = new Array();
   tabList.push(" #");
   for (var i = 0; i < this.options.length; i++) {
     if (tabList.indexOf(this.options[i].text.trim().substr(0, 1).toUpperCase()) == -1)
         tabList.push(this.options[i].text.trim().substr(0, 1).toUpperCase());
   }

   //Sort Alphabetical
   tabList.sort();

   sres += "<div id='example_tab' class='ui-tabs ui-widget ui-widget-content ui-corner-all'>";
   //Generate tab header UI
   sres += "<ul class='ui-tabs-nav ui-helper-reset ui-helper-clearfix ui-widget-header ui-corner-all'>";
   for (var i = 0; i < tabList.length; i++) {
     var tabHeaderContent = document.createElement("li");
     sres += "<li class='ui-state-default ui-corner-top'>";
     sres += "<a href='#tab_" + i + "'><span>" + tabList[i] + "</span></a></li>";
   }
   sres += "</ul>";

   sres += "<div id='tab_" + 0 + "' class='ui-tabs-panel ui-widget-content ui-corner-bottom'></div>";

   var answerOptionNotToIncluded = new Array();

   for (var i = 1; i < tabList.length; i++) {
     var tabContent = "";
     tabContent += "<div id='tab_" + i + "' class='ui-tabs-panel ui-widget-content ui-corner-bottom'>";

     //generate content for tab
     tabContent += "<table><tbody><tr>";
     var indexOfItemInTab = 1;
     var currentRow = 1;
     for (var j = 0; j < this.options.length; j++) {
         if (this.options[j].text.trim().substr(0, 1).toUpperCase() != tabList[i])
             continue;

         if (excludeAOs.contains(this.options[j].value)) {
             answerOptionNotToIncluded.push(this.options[j]);
             continue;
         }

         tabContent += this.options[j].getHTML(optionType);
         indexOfItemInTab++;
         if (indexOfItemInTab > columnPerRow * currentRow) {
             tabContent += "</tr><tr>";
             currentRow++;
         }
     }

     if (this.options.lenght == 0)
         tabContent = "<td>&nbsp;</td>";
     tabContent += "</tr></tbody></table></div>";

     if ($(tabContent).find("input").length <= 0)
         __emptyTabSearchString = "a[href*='tab_" + i + "']";
     else
         sres += tabContent;
   }
   if (answerOptionNotToIncluded.length > 0)
   {
      sres += "</div></td></tr><tr><td><table id='extra_options'>";
      for(var i=0; i<answerOptionNotToIncluded.length; i++)
      {
        sres += "<tr>" + answerOptionNotToIncluded[i].getHTML(optionType) + "</tr>";
      }
      sres += "</table></td></tr></table>";
   }
   else
     sres += "</div></td></tr></table>";

   return sres;
};

var normaloptClick = optclick;
this.optclick = function(slbl, lidx, blnk)
{
   var currentAOValue = quest.options[lidx].value;
   var inputType = quest.type == 1 ? "radio" : "checkbox";

   var openInput, currentAOInput;      
   
   currentAOInput = $("input[type='" + inputType + "'][value='" + currentAOValue + "']")[0];
   

   if (excludeAOs.contains(currentAOValue))
      return normaloptClick(slbl, lidx, blnk);

   //If user click on the link instead of the radiobutton/checkbox then check/uncheck the radio/checkbox
   if (blnk && currentAOInput.type == "checkbox")
      currentAOInput.checked = !currentAOInput.checked;
   else if (blnk)
      currentAOInput.checked = true;

   openInput = document["query"][slbl + ".Open." + currentAOValue];
   if ((typeof openInput) != "undefined") 
   {
      if (currentAOInput.checked) 
      {
         openInput.disabled = false;
         openInput.focus();
      } 
      else 
      {
         openInput.value = "";
         openInput.disabled = true;
      }
   }      
   var o;
   for(var i=0; i<quest.options.length; i++)
   {
      o = quest.options[i];
      if (o.single && currentAOValue != o.value)
      {
         $("input[type='" + inputType + "'][value='" + o.value + "']").attr("checked", false);         
      }
   }   
}

quest.onInit = function()
{
  if (typeof(__emptyTabSearchString) != "undefined")
        $(__emptyTabSearchString).parent().remove();

   $("#example_tab").tabs();
   $("a[href='#tab_0']").parent().css("display", "none");
   
   // set Next invisible onload
   //setVisibility(false);
   $("#extra_options").hide();
};

Code 2: Extra tab showing all answer options

/// Maximum number of column in tab.
var columnPerRow = 3;


//answer option list to display outside of the tabs
var excludeAOs = [998,999];

Array.prototype.contains = function(item)
{
   var i;
   for(i=0; i<this.length; i++)
   {
      if (this[i] == item)
         return true;
   }
   return false;
}

 //Overrider Array to find an item easier
Array.prototype.indexOf = function(character)
{
   for (var i = 0; i < this.length; i++)
      if (this[i] == character)
         return i;
   return -1;
}

quest.getHTML = function()
{
   if (this.type != 1 && this.type != 2)
      return;

   //This code used to set data to question.options
   ans = this.answer.split("_|_");
   for (i = 0; i < ans.length; i++) 
   {
      ans[i] = ans[i].split("_:_");
   }

   for (i = 0; i < ans[0].length; i++) 
   {
      if (ans[0][i].length > 0) 
      {
         for (j = 0; j < this.options.length; j++) 
         {
            if (this.options[j].value == ans[0][i]) 
            {
               this.options[j].checked = true;
               if (ans.length > 1 && typeof this.options[j].open != "undefined" && typeof ans[1][i] != "undefined")
               this.options[j].open = ans[1][i];
            }
         }
      }
   }

   var optionType = this.type == 1 ? 1 : 3;

   var sres = "";
   sres += "<table border=" + this.bordersize + " class=\"question_outer\" cellpadding=\"0\" cellspacing=\"0\">";
   if (this.countdown > 0 && this.showcountdowndisplay) 
   {
      sres += "<tr><td><div id=\"countdowndisplay\"></div></td></tr>";
   }
   sres += "<tr><td id=\"question_text\">" + this.text + "</td></tr>";
   sres += "<tr><td align=\"center\">";  

   //Generate list of tabs
   var tabList = new Array();
  tabList.push(" All");
   for (var i = 0; i < this.options.length; i++) {
     if (tabList.indexOf(this.options[i].text.trim().substr(0, 1).toUpperCase()) == -1)
         tabList.push(this.options[i].text.trim().substr(0, 1).toUpperCase());
   }

   //Sort Alphabetical
   tabList.sort();

   sres += "<div id='example_tab' class='ui-tabs ui-widget ui-widget-content ui-corner-all'>";
   //Generate tab header UI
   sres += "<ul class='ui-tabs-nav ui-helper-reset ui-helper-clearfix ui-widget-header ui-corner-all'>";
   for (var i = 0; i < tabList.length; i++) {
     var tabHeaderContent = document.createElement("li");
     sres += "<li class='ui-state-default ui-corner-top'>";
     sres += "<a href='#tab_" + i + "'><span>" + tabList[i] + "</span></a></li>";
   }
   sres += "</ul>";

   var answerOptionNotToIncluded = new Array();

   for (var i = 0; i < tabList.length; i++) {
     var tabContent = "";
     if(i==0)tabContent += "<div id='tab_" + i + "' class='ui-tabs-panel ui-widget-content ui-corner-bottom'>";
     else tabContent += "<div id='tab_" + i + "' class='ui-tabs-panel ui-tabs-hide'>";
	 
     //generate content for tab
     tabContent += "<table><tbody><tr>";
     var indexOfItemInTab = 1;
     var currentRow = 1;
     for (var j = 0; j < this.options.length; j++) {
         if (i != 0 && this.options[j].text.trim().substr(0, 1).toUpperCase() != tabList[i])
             continue;
		var optionInput = this.options[j].getHTML(optionType);
		if (i == 0) {
                while (optionInput.indexOf(this.label) != -1) {
                    optionInput = optionInput.replace(this.label, "TabAllAnswer");
                }
				}
         if (i!=0 && excludeAOs.contains(this.options[j].value)) {
             answerOptionNotToIncluded.push(this.options[j]);
             continue;
			 
         }

         tabContent += this.options[j].getHTML(optionType);
         indexOfItemInTab++;
         if (indexOfItemInTab > columnPerRow * currentRow) {
             tabContent += "</tr><tr>";
             currentRow++;
         }
     }

     if (this.options.lenght == 0)
         tabContent = "<td>&nbsp;</td>";
     tabContent += "</tr></tbody></table></div>";

     if ($(tabContent).find("input").length <= 0)
         __emptyTabSearchString = "a[href*='tab_" + i + "']";
     else
         sres += tabContent;
   }
   if (answerOptionNotToIncluded.length > 0)
   {
      sres += "</div></td></tr><tr><td><table id='extra_options'>";
      for(var i=0; i<answerOptionNotToIncluded.length; i++)
      {
        sres += "<tr>" + answerOptionNotToIncluded[i].getHTML(optionType) + "</tr>";
      }
      sres += "</table></td></tr></table>";
   }
   else
     sres += "</div></td></tr></table>";

   return sres;
};

var normaloptClick = optclick;
this.optclick = function(slbl, lidx, blnk)
{
   var currentAOValue = quest.options[lidx].value;
   var inputType = quest.type == 1 ? "radio" : "checkbox";

   var openInput, currentAOInput;      
   
   currentAOInput = $("input[type='" + inputType + "'][value='" + currentAOValue + "']")[0];
   

   if (excludeAOs.contains(currentAOValue))
      return normaloptClick(slbl, lidx, blnk);

   //If user click on the link instead of the radiobutton/checkbox then check/uncheck the radio/checkbox
   if (blnk && currentAOInput.type == "checkbox")
      currentAOInput.checked = !currentAOInput.checked;
   else if (blnk)
      currentAOInput.checked = true;

   openInput = document["query"][slbl + ".Open." + currentAOValue];
   if ((typeof openInput) != "undefined") 
   {
      if (currentAOInput.checked) 
      {
         openInput.disabled = false;
         openInput.focus();
      } 
      else 
      {
         openInput.value = "";
         openInput.disabled = true;
      }
   }      
   var o;
   for(var i=0; i<quest.options.length; i++)
   {
      o = quest.options[i];
      if (o.single && currentAOValue != o.value)
      {
         $("input[type='" + inputType + "'][value='" + o.value + "']").attr("checked", false);         
      }
   }   
}

quest.onInit = function()
{
  if (typeof(__emptyTabSearchString) != "undefined")
        $(__emptyTabSearchString).parent().remove();

   $("#example_tab").tabs();
   //$("a[href='#tab_0']").parent().css("display", "none");
   
   // set Next invisible onload
   //setVisibility(false);
   $("#extra_options").hide();
};

Source

Questionnaire Resource Id on cg.catglobe.com site: 164079