Javascript递归for循环到自动构建菜单结构不起作用

时间:2014-11-15 11:44:11

标签: javascript json twitter-bootstrap

我很长时间以来一直在挠头,但却无法得到任何解决方案。在twitter bootstrap主题中,我已经编写了这个函数来递归地构建导航菜单。目前,我已将菜单结构放在包含JSON对象的列表中,但最终会动态获取。以下是执行此操作的代码:

function createMenus() {
    //fill the menus in navbar dynamically.
    vars={};
    vars.title = "RAFinder";
    vars.menus = ['Home', 
    {'Student':['Search Jobs','Sent Applications', 'Find Professors', 'My Profile', 'Test Scores']},
    {'Professor':[
        {'Research Jobs':['Research Jobs', 'Search RA', 'Sent Applications', 'Find Students', 'Student Profiles', 'Students Past Jobs and Performance', 'My Profile']},
        {'Posted Jobs':['Contracts FixedHourly', 'Work Diary', 'Reviewevaluation of students performance']},
        {'Messages':['Inbox', 'Sent', 'Archive']},
        ]},
    'About','Contact'
    ];

    //$('#divmain').html('');
    for (var i=0;i<vars.menus.length;i++)
        populateMenu(vars.menus[i]);

    $('title').text(vars.title);
    $('.navbar-brand').text(vars.title);
}

正如您所看到的,该结构由可以是String(用于菜单)或JSON对象(用于进一步递归的子菜单)的项列表组成。这是populateMenu以递归方式执行此操作:

function populateMenu(menu, parent='') {
    console.log(menu, parent);
    tp=type(menu);
    if (tp=="string")
    {
        //Just fill menu.
        $('#divmain').append(parent + '::' + menu + '<br>');
        addMenuItem(menu, parent, false); //actually add it on bootstrap navbar
    }
    else if (tp=="object")
    {
        $('#divmain').append('Length of dict is: ' + Object.keys(menu).length.toString() + '<br>');
        k='';
        for (var key in menu) //this should ideally be one loop
        {
            //if (menu.hasOwnProperty(k)) {
            //  }
            console.log('ITERATING::',key);
            k = key;
        }
            //else {$('#divmain').append('notOwnProperty: ' + k);}

            //just fill k
            $('#divmain').append(parent + '::' + k.toString() + '-><br>');
            addMenuItem(k.toString(), parent, true);   //actually add it on bootstrap navbar
            l = menu[k];
            //for(item in l)
            for(var i=0;i<l.length;i++)
            {
                //$('#divmain').append(k + '::' + l[item] + '-><br>');
                $('#divmain').append('Now calling populateMenu for dict ' + l[i].toString()  + ' and parentIs: ' + k.toString() + '<br>');
                populateMenu(l[i], k.toString()); //these are dropdowns
            }
    }
}

根级菜单可以正常填写(即主页,学生,教授,关于,联系)。即使学生子菜单也可以填写。问题出在教授子菜单上。只有列表中的第一个JSON对象是&#34;研究工作&#34;充满。其余的都被for循环忽略了,我不明白为什么。

1 个答案:

答案 0 :(得分:0)

最后,我通过以下方式解决了这个问题:

function createMenus() {
    //fill the menus in navbar dynamically.
    vars.menus = ['Home', 
    {'Student':['Search Jobs','Sent Applications', 'My Profile']},
    {'Professor':[
        {'Research Jobs':['Search RA', 'Sent Applications', 'Find Students', 'Student Profiles', 'Students Past Jobs and Performance']},
        {'Posted Jobs':['Contracts FixedHourly', 'Work Diary', 'Review/Evaluation of students performance']},
        {'Messages':['Inbox', 'Sent', 'Archive']},
        ]},
    'About','Contact'
    ];

    buildMenu(vars.menus, $('.navbar-nav'));
}

 function buildMenu(arr, parentEl) {
    arr.forEach(function(root) {
        var li;
        var id;

        // This is a normal string entry
        if ( typeof root === 'string' ) {
            id = root;
            li = $('<li></li>').html('<a href="' + id  + '">' + root +  '</a>');
            parentEl.append(li);
        }

        // Or this is an object 
        else {
            // Loop over all the keys
            Object.keys(root).forEach(function(child) {
                id = removeSpecialChars(child);
                istopmenu = parentEl.hasClass('navbar-nav');
                li = $('<li class="dropdown' + (istopmenu ? '' : ' dropdown-submenu') + '"></li>').html('<a class="dropdown-toggle" data-toggle="dropdown"  href="#" onclick="javascript:createView(\'' + id +  '\');" aria-expanded="false">' + child + (istopmenu ? '<span class="caret"></span>' : '') + '</a>');
                var ul = $('<ul class="dropdown-menu" role="menu"></ul>');


                // Recursively loop over the array elements 
                buildMenu(root[child], ul);
                li.append(ul);
                parentEl.append(li);
             });
        }
    });
  }