jquery find eligible parent node from child node

I wrote a tree at work. When the user clicks the query when entering the personnel information, it is necessary to find the node in the tree, and then expand the node.

 

Two ideas come to mind:

 

Idea one:

If the target node is found in the tree, determine whether the parent node of the target node is the root node. If it is not the root node, let the parent node expand, and continue to recursively determine the parent node of the current node. If it is the root node, do not recurse and expand the node. . (This method is more complicated (trouble), mainly recursive algorithm)

 

Idea two:

Use jquery to find the target node, then find all its parent nodes within the scope of the tree (so that you will not find nodes outside the tree scope), and then expand the node.

 

HTML structure of the organization tree
<ul id="rightTreePanel">
	<li>
		<div>I am the parent node</div>
		<ul>
			<li>
				<div>I am a child node</div>
			</li>
		</ul>
	</li>
</ul>

<script>
// Expand all levels of the parent node
$(" li[data_id='"+obj.id+"']", $("#rightTreePanel")).parents("ul").show();
</script>

 

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326763942&siteId=291194637