一个表里存放了全国各地地区、省、市、县区的数据,为了提升加载速度我保存成了本地的JSON文件node
结构大体以下:ajax
[{ "text": "中华人民共和国", "spid": "2013010535", "nodes": [{ "text": "东北", "spid": "2013010535", "nodes": [{ "text": "辽宁省", "spid": "2013035210", "nodes": [{ "text": "沈阳市", "spid": "2013035211", "nodes": [{ "text": "沈阳市和平区", "spid": "2013038441" }, { "text": "沈河区", "spid": "2013038441" }, { "text": "大东区", "spid": "2013038441" }, { "text": "皇姑区", "spid": "2013038441" }, { "text": "沈阳市铁西区", "spid": "2013038441" }, { "text": "苏家屯区", "spid": "2013038441" }, { "text": "东陵区", "spid": "2013038441" }, { "text": "沈北新区", "spid": "2013038441" }, { "text": "于洪区", "spid": "2013038441" }, { "text": "辽中县", "spid": "2013038441" }, { "text": "康平县", "spid": "2013038441" }, { "text": "法库县", "spid": "2013038441" }, { "text": "新民市", "spid": "2013038441" }] }, { "text": "大连市", "spid": "2013035211", "nodes": [{ "text": "中山区", "spid": "2013038441" }, { "text": "西岗区", "spid": "2013038441" }, { "text": "沙河口区", "spid": "2013038441" }, { "text": "甘井子区", "spid": "2013038441" }, { "text": "旅顺口区", "spid": "2013038441" }, { "text": "金州区", "spid": "2013038441" }, { "text": "长海县", "spid": "2013038441" }, { "text": "瓦房店市", "spid": "2013038441" }, { "text": "普兰店市", "spid": "2013038441" }, { "text": "庄河市", "spid": "2013038441" }] }] }] }] }]
因为全国的地理信息数据太多,这里只截取了一小部分json
加载这些JSON数据展示为一棵树后,咱们看到的是全国的行政区域信息async
每一个地区对应一个地区节点网站,网站加载了该地区的特点照片,父节点的网站要包含子节点的数据网站
系统管理员能够为每一个网站能够分配管理员url
父节点的管理员同时也是子节点的管理员spa
分站管理员打开后台管理界面时,能够看到本身所管理的行政区域树code
一个管理员能够管理多个地区,一个地区能够有多个管理员blog
这就要求对JSON文件进行查询,先查出这个管理员所管理的区域节点ID,遍历生成每一个ID所在的节点树,最终将这些树合并。string
下面是代码
$(function () { //加载行政区域树数据 $.ajax({ url: './data/tree.json', dataType: 'text', async: true, type: 'GET', success: function (response) { var json = $.parseJSON(response); var result1 = up('133830', json); console.log(result1); var result2 = up('133695', json); console.log(result2); var result = merge([result1],[result2]); console.log(result); } }); });
function up(nodeid, map) { var currentMap = null, tmp = JSON.parse(JSON.stringify(map)); tmp.forEach(function (subMap) { if (subMap.nodeid === nodeid) currentMap = subMap; }); if (currentMap !== null) { return currentMap; } else { var result = null; tmp.forEach(function (subMap) { if (subMap.hasOwnProperty("nodes")) { var subRe = up(nodeid, subMap.nodes); if (subRe !== null) {subMap.nodes = subRe; result = subMap;} } }); return result; } }
function merge (map1, map2) { var result = [], tmp1 = JSON.parse(JSON.stringify(map1)), tmp2 = JSON.parse(JSON.stringify(map2)); tmp1.forEach(function (item1) { var flag = false; tmp2.forEach(function (item2) { if (item1.nodeid === item2.nodeid) { flag = true; result.push({ text: item1.text, nodeid: item1.nodeid, spid: item1.spid, level: item1.level, parentid: item1.parentid, nodes: merge([item1.nodes], [item2.nodes]) }); } }); if (!flag) result.push(item1); }); tmp2.forEach(function (item2) { var flag = false; tmp1.forEach(function (item1) { if (item1.nodeid === item2.nodeid) flag = true; }); if (!flag) result.push(item2); }); return result; }
运行结果