递归生成菜单

仅仅是一个demo,用来给团队示例讲解

/**
 *  2019-09-08.
 */


/**
 * 创建一级菜单项,没有url,easyUI要求放在panel内部
 * @param title
 */
function getPanelHeader(title){
    var topMenu = [];
    topMenu.push('  
'); topMenu.push('
' + title + '
'); topMenu.push('
'); topMenu.push('
'); return topMenu.join("").toString(); } /** * 创建子菜单项的容器 */ function getPanelBody(menuItems){ var topMenu = []; topMenu.push('
'); topMenu.push(' '); topMenu.push('
'); return topMenu.join("").toString(); } /** * 创建菜单子项 * @param title * @param url * @returns {string} */ function getPanelBoyInsideMenuItem(title,url){ var topMenu = []; topMenu.push('
  • '); topMenu.push('
    '); topMenu.push('  ' + title + ''); topMenu.push('
    '); topMenu.push('
  • '); return topMenu.join("").toString(); } /** * 获取本系统菜单数据 * @param iscMenuJson 树的json对象 * @returns {*} */ function getMenuData(iscMenuJson){ var funcNode = iscMenuJson.funcNode; if(funcNode != null && funcNode.length >0){ var menus = funcNode[0]; if(menus != null && menus.length != 0){ return menus.nextNode; }else{ return null; } } } function create(node){ var menus = []; var name = node.currentNode.name; var url = node.currentNode.url; var panelHead = getPanelHeader(name); menus.push(panelHead); if(hasChildNode(node)){ var childNodes = getChildNode(node); var items = []; for(var i = 0; i < childNodes.length;i++){ var childName = childNodes[i].currentNode.name; var childUrl = childNodes[i].currentNode.url; if(hasChildNode(childNodes[i])){ items.push(create(childNodes[i]));//递归 }else{ var childItem = getPanelBoyInsideMenuItem(childName,childUrl); //子节点缓存 items.push(childItem); } } var panelBody = getPanelBody(items.join("").toString()); menus.push(panelBody); } return menus.join("").toString(); } function getChildNode(currentNode){ if(hasChildNode(currentNode)){ return currentNode.nextNode; } return null; } function hasChildNode(currentNode){ if(currentNode && currentNode.nextNode){ var nextNode = currentNode.nextNode; return nextNode && nextNode.length != 0; } }

    你可能感兴趣的:(递归生成菜单)