Javascript递归循环自动生成菜单结构不工作
很久以来,我一直在挠头,但无法解决这个问题。在twitter引导主题中,我编写了这个函数来递归地构建导航菜单。目前,我将菜单结构放在一个包含JSON对象的列表中,但最终它将被动态获取。以下是执行此操作的代码:Javascript递归循环自动生成菜单结构不工作,javascript,json,twitter-bootstrap,Javascript,Json,Twitter Bootstrap,很久以来,我一直在挠头,但无法解决这个问题。在twitter引导主题中,我编写了这个函数来递归地构建导航菜单。目前,我将菜单结构放在一个包含JSON对象的列表中,但最终它将被动态获取。以下是执行此操作的代码: function createMenus() { //fill the menus in navbar dynamically. vars={}; vars.title = "RAFinder"; vars.menus = ['Home', {'S
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);
}
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
}
}
}
函数createMenus(){
//动态填充导航栏中的菜单。
vars={};
vars.title=“RAFinder”;
vars.menus=['Home',
{'Student':['Search Jobs','send Applications','Find Professors','My Profile','Test Scores']},
{‘教授’:[
{‘研究工作’:[‘研究工作’、‘搜索RA’、‘发送申请’、‘查找学生’、‘学生档案’、‘学生过去的工作和表现’、‘我的档案’]},
{'Posted Jobs':['Contracts Fixed Hourly'、'Work Daily'、'Review Valuation of Student performance'],
{'Messages':['Inbox','Sent','Archive']},
]},
“关于”,“联系”
];
//$('#divmain').html('');
对于(var i=0;i最后,我用以下方式解决了这个问题:
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);
});
}
});
}
函数createMenus(){
//动态填充导航栏中的菜单。
vars.menus=['Home',
{'Student':['Search Jobs','send Applications','My Profile']},
{‘教授’:[
{‘研究工作’:[‘搜索RA’、‘发送申请’、‘查找学生’、‘学生档案’、‘学生过去的工作和表现’]},
{‘张贴的工作’:[‘每小时固定的合同’、‘工作日记’、‘学生表现审查/评估’]},
{'Messages':['Inbox','Sent','Archive']},
]},
“关于”,“联系”
];
buildMenu(vars.menus,$('.navbar-nav'));
}
功能构建菜单(arr、parentEl){
arr.forEach(函数(根){
李华;
变量id;
//这是一个普通的字符串条目
如果(根的类型=='string'){
id=根;
li=$('').html('');
parentEl.append(li);
}
//或者这是一个物体
否则{
//把所有的钥匙圈起来
Object.keys(root.forEach)函数(子函数){
id=移除特殊字符(子项);
istopmenu=parentEl.hasClass('navbar-nav');
li=$('li class=“dropdown'+(istopmenu?'':'dropdown submenu')+“>”).html(''');
var ul=$('
);
//递归地循环数组元素
构建菜单(根目录[子目录],ul);
附加(ul);
parentEl.append(li);
});
}
});
}