Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

树结构数据查找 #25

Open
conan1992 opened this issue Jun 18, 2020 · 0 comments
Open

树结构数据查找 #25

conan1992 opened this issue Jun 18, 2020 · 0 comments

Comments

@conan1992
Copy link
Owner

场景

我调取接口获取区域树数据,然后渲染成联动区域下拉框;如图:
image

问题描述

我在改变一个节点的时候,要获取到这个区域下的子节点并重新加载下拉框;
也就是获取被选择区域的区域id去树结构搜索到这个节点数据;
区域树数据:

{"code":0,"msg":"成功","data":[{"baidu":"126.372035,28.956632","children":[{"baidu":"120.76279602444632,31.931075745269744","children":[{"baidu":"120.76279602444632,31.931075745269744","children":[{"baidu":"119.990511 , 32.036116","children":[],"id":146,"name":"123","orgClass":"自然村","orgCodea":"P33Q01C08T01V33G04","pid":143,"sourceCode":"330108010100"},{"baidu":"120.76289669375707,31.931274002424868","children":[],"id":148,"name":"123","orgClass":"自然村","orgCodea":"P33Q01C08T01V33G06","pid":143,"sourceCode":"330108010100"},{"baidu":"120.76339866190557,31.93086497719618","children":[],"id":149,"name":"1235","orgClass":"自然村","orgCodea":"P33Q01C08T01V33G07","pid":143,"sourceCode":"330108010100"},{"baidu":"120.76249556495591,31.93208140315094","children":[],"id":150,"name":"1239","orgClass":"自然村","orgCodea":"P33Q01C08T01V33G08","pid":143,"sourceCode":"330108010100"},{"baidu":"120.76189093359406,31.930291824738674","children":[],"id":151,"name":"11111111112","orgClass":"自然村","orgCodea":"P33Q01C08T01V33G09","pid":143,"sourceCode":"330108010100"},{"baidu":"120.76269535372678,31.930877491159052","children":[],"id":152,"name":"22334455","orgClass":"自然村","orgCodea":"P33Q01C08T01V33G10","pid":143,"sourceCode":"330108010100"},{"baidu":"120.76189201094185,31.931392109367092","children":[],"id":153,"name":"33334444","orgClass":"自然村","orgCodea":"P33Q01C08T01V33G11","pid":143,"sourceCode":"330108010100"},{"baidu":"120.7629978448191,31.931972390307923","children":[],"id":154,"name":"3344556677","orgClass":"自然村","orgCodea":"P33Q01C08T01V33G12","pid":143,"sourceCode":"330108010100"},{"baidu":"120.76329926007091,31.931967036960298","children":[],"id":155,"name":"1515151515","orgClass":"自然村","orgCodea":"P33Q01C08T01V33G13","pid":143,"sourceCode":"330108010100"},{"baidu":"120.762293542566,31.930984738109885","children":[],"id":163,"name":"123445578","orgClass":"自然村","orgCodea":"P33Q01C08T01V33G18","pid":143,"sourceCode":"330108010100"},{"baidu":"120.76189113066809,31.93049187680314","children":[],"id":166,"name":"阿斯蒂芬","orgClass":"自然村","orgCodea":"P33Q01C08T01V33G21","pid":143,"sourceCode":"330108010200"},{"baidu":"120.76219306333581,31.930986550878725","children":[],"id":171,"name":"1234455","orgClass":"自然村","orgCodea":"P33Q01C08T01V33G26","pid":143,"sourceCode":"330108010100"},{"baidu":"120.76279523915777,31.930275537833317","children":[],"id":175,"name":"阿斯蒂芬2","orgClass":"自然村","orgCodea":"P33Q01C08T01V33G32","pid":143,"sourceCode":"330108010100"},{"baidu":"120.76209602444632,31.931275745269744","children":[],"id":192,"name":"江虹南路317","orgClass":"自然村","orgCodea":"P33Q01C08T01V33G36","pid":143,"sourceCode":"330108010100"},{"baidu":"120.76319602444632,31.930475745269746","children":[],"id":199,"name":"yy","orgClass":"自然村","orgCodea":"P33Q01C08T01V33G34","pid":143,"sourceCode":"330108010100"},{"baidu":"120.210967,30.177662","children":[],"id":200,"name":"125","orgClass":"自然村","orgCodea":"P33Q01C08T01V33G35","pid":143,"sourceCode":"330108010100"},{"baidu":"120.210959,30.177797","children":[],"id":201,"name":"1234","orgClass":"自然村","orgCodea":"P33Q01C08T01V33G37","pid":143,"sourceCode":"330108010100"},{"baidu":"120.76279602444632,31.930575745269746","children":[],"id":202,"name":"120","orgClass":"自然村","orgCodea":"P33Q01C08T01V33G38","pid":143,"sourceCode":"330108010100"},{"baidu":"120.76189602444632,31.931375745269744","children":[],"id":204,"name":"木页村","orgClass":"自然村","orgCodea":"P33Q01C08T01V33G39","pid":143,"sourceCode":"330108010100"},{"baidu":"120.76289602444632,31.930175745269743","children":[],"id":205,"name":"木页村1","orgClass":"自然村","orgCodea":"P33Q01C08T01V33G40","pid":143,"sourceCode":"330108010100"},{"baidu":"120.76239602444632,31.931575745269743","children":[],"id":206,"name":"木页村2","orgClass":"自然村","orgCodea":"P33Q01C08T01V33G41","pid":143,"sourceCode":"330108010100"},{"baidu":"120.76189602444632,31.930375745269746","children":[],"id":208,"name":"木页村3","orgClass":"自然村","orgCodea":"P33Q01C08T01V33G42","pid":143,"sourceCode":"330108010100"},{"baidu":"120.76279602444632,31.930175745269743","children":[],"id":209,"name":"木页村4","orgClass":"自然村","orgCodea":"P33Q01C08T01V33G43","pid":143,"sourceCode":"330108010100"},{"baidu":"120.76369602444632,31.931575745269743","children":[],"id":210,"name":"木页村5","orgClass":"自然村","orgCodea":"P33Q01C08T01V33G46","pid":143,"sourceCode":"330108010100"},{"baidu":"120.76269602444631,31.930275745269743","children":[],"id":211,"name":"木页6","orgClass":"自然村","orgCodea":"P33Q01C08T01V33G47","pid":143,"sourceCode":"330108010100"}],"id":143,"name":"测试村","orgClass":"村级","orgCodea":"P33Q01C08T01V33","pid":142,"sourceCode":"330108010100"},{"baidu":"120.76279602444632,31.931075745269744","children":[{"baidu":"120.76269496106697,31.930477387435054","children":[],"id":158,"name":"333444","orgClass":"自然村","orgCodea":"P33Q01C08T01V02G02","pid":144,"sourceCode":"330108020100"},{"baidu":"120.76359989209206,31.93116150985561","children":[],"id":159,"name":"xc","orgClass":"自然村","orgCodea":"P33Q01C08T01V02G03","pid":144,"sourceCode":"330108020100"},{"baidu":"120.762724,31.932516","children":[],"id":193,"name":"江虹南路316","orgClass":"自然村","orgCodea":"P33Q01C08T01V02G05","pid":144,"sourceCode":"330108020100"}],"id":144,"name":"02","orgClass":"村级","orgCodea":"P33Q01C08T01V02","pid":142,"sourceCode":"330108020100"},{"baidu":"120.76279602444632,31.931075745269744","children":[],"id":207,"name":"测试村2","orgClass":"村级","orgCodea":"P33Q01C08T01V12","pid":142,"sourceCode":"123123123123"}],"id":142,"name":"测试镇","orgClass":"镇级","orgCodea":"P33Q01C08T01","pid":141,"sourceCode":"330108010000"},{"baidu":"120.549290,31.827068","children":[{"baidu":"125.366719,28.272737","children":[{"baidu":"125.367619,28.273436999999998","children":[],"id":189,"name":"江虹南路316","orgClass":"自然村","orgCodea":"P33Q01C08T08V08G01","pid":188,"sourceCode":"330108000000"}],"id":188,"name":"456","orgClass":"村级","orgCodea":"P33Q01C08T08V08","pid":184,"sourceCode":"330108000000"}],"id":184,"name":"江虹南路","orgClass":"镇级","orgCodea":"P33Q01C08T08","pid":141,"sourceCode":"330108000000"},{"baidu":"125.366719,28.272737","children":[],"id":187,"name":"图南","orgClass":"镇级","orgCodea":"P33Q01C08T","pid":141,"sourceCode":"45523"},{"baidu":"120.857445,32.363481","children":[],"id":190,"name":"图南","orgClass":"镇级","orgCodea":"P33Q01C08T09","pid":141,"sourceCode":"152"},{"baidu":"120.733263,31.374350","children":[{"baidu":"120.75626,31.925191","children":[{"baidu":"120.75525999999999,31.924891000000002","children":[],"id":196,"name":"杭州123","orgClass":"自然村","orgCodea":"P33Q01C08T10V12G01","pid":195,"sourceCode":"45523"},{"baidu":"120.75626,31.924891000000002","children":[],"id":197,"name":"杭州","orgClass":"自然村","orgCodea":"P33Q01C08T10V12G05","pid":195,"sourceCode":"45523"},{"baidu":"120.75676,31.925991000000003","children":[],"id":198,"name":"阳光城上府","orgClass":"自然村","orgCodea":"P33Q01C08T10V12G06","pid":195,"sourceCode":"330108002001"}],"id":195,"name":"test","orgClass":"村级","orgCodea":"P33Q01C08T10V12","pid":191,"sourceCode":"45523"}],"id":191,"name":"大G","orgClass":"镇级","orgCodea":"P33Q01C08T10","pid":141,"sourceCode":"110"},{"baidu":"126.372035,28.956632","children":[],"id":213,"name":"12","orgClass":"镇级","orgCodea":"P33Q01C08T56","pid":141,"sourceCode":"1232323"}],"id":141,"name":"滨江区","orgClass":"县级","orgCodea":"P33Q01C08","pid":140,"sourceCode":"330108000000"}]}

方法1--递归

function findTreeNode(treeId){
	var node = null
	function query(data){
		var len = data.length;
		for(var i=0;i<len;i++){
			if(data[i].id == treeId){
				node =  data[i]
				break;
			}else if(data[i].children.length){
				query(data[i].children)
			}
		}
	};
	query(treeData);
	return node;
}

方法2--全遍历

利用数组,遍历所有节点,如果当前节点还有子节点,就把子节点push进数组;

function findTreeNode2(treeId){
    var itemStack = []
	var node = null
	itemStack = itemStack.concat(treeData)
    while(itemStack.length){
        var item = itemStack.shift();
        if(item.id==treeId){
            node = item
            break
        }else if(item.children.length){
            itemStack = itemStack.concat(item.children)
        }
    }
	return node;
}

参考:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant