获取地址栏参数,json遍历

1. 获取地址栏参数

 GetQueryString: function(name){     // 获取地址栏参数
            
            var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)");
            var r = window.location.search.substr(1).match(reg);
            if(r!=null)return  decodeURIComponent(r[2]); return null;
        }

2. 如果有一个结构比较深的json表,需要通过属性去遍历他的层次

function deepJson(obj, page) {
            var result = {};
            ; (function walkTree(obj, page) {
                for (key in obj) {
                    if (key == page) {
                        result = obj[key];
                        break;
                    } else if (obj[key].subMenu) {
                        walkTree(obj[key].subMenu, page);
                    }
                }
            })(obj, page);
            return result;
        }

  

 

var htmlconfig = {
  system: {
    text: '系统配置',
    subMenu: {
      account: {
        text: '账号管理',
        crumbs: 'system/account',
        url: '#system/account'
      },
      role: {
        text: '角色管理',
        crumbs: 'system/role',
        url: '#system/role'
      },
      function: {
        text: '功能管理',
        crumbs: 'system/function',
        url: '#system/function'
      },
      permission: {
        text: '权限设置',
        crumbs: 'system/permission',
        url: '#system/permission'
      },
      schedule: {
        text: '任务调度',
        crumbs: 'system/schedule',
        url: '#system/schedule/index',
        subMenu: {
          service: {
            text: '服务配置',
            crumbs: 'system/schedule/service',
            url: '#system/schedule/service',
            subMenu: {
              work: {
                text: '任务作业配置表',
                crumbs: 'system/schedule/service/work',
                url: '#system/schedule/work'
              },
            }
          }
        }
      },
      config: {
        text: '配置设置',
        crumbs: 'system/config',
        url: '#system/config'
      }
    }
  }
   }
  }
};

module.exports = htmlconfig;

deepJson(htmlconfig,'service')

 

//根据路由确定面包屑的菜单
       function  deepJsonMenu(page) {
            var result = {},
                obj = utilSys.crumbConfig;
            var menu = page.split('/'),
                testObj = {};
            var firstLevel = obj[menu[0]];
            var otherObj = firstLevel;
            testObj[0] = firstLevel;
            for (var i = 1, len = menu.length; i < len; i++) {
                testObj[i] = otherObj.subMenu[menu[i]];
                otherObj = testObj[i];
            }
            return testObj;
        }

  deepJsonMenu('#system/config')

 

posted @ 2017-12-22 10:12  lanyan  阅读(370)  评论(0编辑  收藏  举报