Skip to main content
Kinetic Community

Combine two menus

This is an example of combining menu options from two menus into one menu.  The combine menu is sorted and duplicates are removed from the list.

Resolution

To use this javascript place this javascript into your request and call the "combineTwoMenus" function with the appropriate parameters.

//Create Global variable to contain length of Menu
var gblLen = 0;

/**
* DESCRIPTION Combine option values from 2 menus into one menu option list.
* @method combineTwoMenus
* @param source1 {String} The first menu question label.
* @param source2 {String} The second menu question label.
* @param destination {String} The question label to be populated with the combined menu options.
*/
function combineTwoMenus(source1,source2,destination){
    var appendedValue ="";
    var menuOptions = new Object;
    var html = new Array;
    var value = new Array;
    var menu = new Array;
    var x = 0;
    gblLen = 0;


    //Source Questions
    var s1 = KD.utils.Util.getAnswerLayer(source1);
    var s2 = KD.utils.Util.getAnswerLayer(source2);
    var opts1 = s1.firstChild.options;
    var opts2 = s2.firstChild.options;
    var len1 = KD.utils.Util.getAnswerLayer(source1).firstChild.options.length;
    var len2 = KD.utils.Util.getAnswerLayer(source2).firstChild.options.length;
    if (len2>len1){var len =len2;}else{var len = len1;}


    //Destination Question
    var d1 = KD.utils.Util.getAnswerLayer(destination);

    //Clear Existing values on Destination Menu
   d1.firstChild.length = 0;

    //Concat into an Array
    for (var i = 1; i < len; i++) {
        if (opts1[i] && opts1[i].innerHTML != null && opts1[i].innerHTML != ""){
             menu[x]=new Array(2);
             menu[x][0]=opts1[i].innerHTML;
             //menu[x][1]='CHG_'+opts1[i].value;
             menu[x][1]=opts1[i].value;
             x++
        }
        if (opts2[i] && opts2[i].innerHTML != null && opts2[i].innerHTML != ""){
            menu[x]=new Array(2);
            menu[x][0]=opts2[i].innerHTML;
            //menu[x][1]='HPD_'+opts2[i].value;
            menu[x][1]=opts2[i].value;
            x++
        }
    }
    //Sort Array assending
    menu.sort(charOrdA);
    // OR Sort Array decending
    //menu.sort(charOrdD);

    if(menu.length >=1){
        var uniqueMenu = removeDuplicates(menu);
        len=uniqueMenu.length;
        gblLen = len;
    }
    else{len = 0; gblLen = 0;}

    //Populate Menu
    d1.firstChild.options[0] = new Option("","");
    
    //d1.firstChild.options[1] = new Option("-------ENTER AD-HOC REQUEST-------","AD-HOC");
    for (var i = 0; i < len; i++) {
        d1.firstChild.options[i+1] = new Option(uniqueMenu[i][0],uniqueMenu[i][1]);
    }

}


//Functions used to place all characters into lowercase inorder to correctly sort.
//Sort Accending
function charOrdA(a, b){
    a = a[0].toLowerCase(); b = b[0].toLowerCase();
    if (a>b) return 1;
    if (a <b) return -1;
    return 0;
}

//Sort Decending
function charOrdD(a, b){
    a = a[0].toLowerCase(); b = b[0].toLowerCase();
    if (a<b) return 1;
    if (a >b) return -1;
    return 0;
}


function removeDuplicates(testArray) {
    var tempArray = new Array();
    tempArray[0]=testArray[0];
    for(var i=0;i<testArray.length;i++)
    {
        var flag = true;
        for(var j=0;j<tempArray.length;j++)
        {
            if(tempArray[j][0]==testArray[i][0])
            {
                flag = false;
            }
        }//for loop
        if(flag==true)
        tempArray.push(testArray[i]);
    }//for loop    
    return tempArray;
}