


javascript-TreeView parent-child linkage effect keeps node status consistent_javascript skills
Most of us have used the TreeView control, and the evaluation of this control is also varied, but I think it is a free and open source control anyway, so I still use it. When I first came into contact with ASP.NET, I remember that I needed to make a permission tree to allocate permissions. At that time, I only knew about this tree. After a day of research, I basically understood its server-side behavior. However, due to the limited js level at the time, I am very afraid of the client code and have basically never seen it.
There was a requirement at that time: if a node is selected, all the child nodes of the node must be selected. If all the child nodes of the node are deselected, the node must also be deselected (node consistency), and vice versa.
Another requirement is: it would be better if a three-state tree can be implemented (this is a bit difficult and will not be discussed in this article). This article will detail the previous requirement 1.
First of all, we have to be grateful that Microsoft’s TreeView control is open source. You can easily obtain both client-side code and server-side code. You can download it from Microsoft’s website. I have also seen some articles on the Internet about this problem. Most of them use a js other than TreeView to implement it. I personally think that from an object-oriented point of view, this function belongs to TreeView, so there is no reason to separate it, so today I will Modify TreeView.htc to implement the above functions. To obtain this file (TreeView.htc), you can download it from Microsoft's website.
To be honest, this function has troubled me for a long time. I have always wanted to solve this problem, but I have never had time to study the code in TreeView.htc. Today I finally made up my mind to solve it.
We know that the event processing function needs to be triggered in the oncheck of TreeView. This function is easy to find and can be found in the client script generated by TreeView. The code snippet is as follows:
oncheck="javascript: if (this. clickedNodeIndex != null) this.queueEvent('oncheck', this.clickedNodeIndex)"
From this we can go to htc (htc mentioned in the future refers to TreeView.htc) and find the doCheckboxClick method. Just look at the name to know What does it do (naming is so important, otherwise it will be really tiring to find a certain function in 3000 lines of code).
This method is a function to be used when the user clicks the CheckBox. The function is as follows:
function doCheckboxClick(el){
var checked = private_getAttribute(el,"checked")
el. checked = !checked; var evt = createEventObject(); evt.treeNodeIndex = getNodeIndex(el); g_nodeClicked = el; _tvevtCheck.fire(evt);
setNodeState(el ,el.checked);//maybe need el only,but I think it's safly
}
} The first, second and last lines were added by me. The first and second lines are for the page response. Modifications made when the checked attribute is invalid in the future are passed. According to the original method, el.checked is undefined after submission, so the nodes cannot be synchronized correctly. If the reader is interested, you can give it a try. The setNodeState function, as you can see from the name, is used to set the node state. It passes the currently selected node as a parameter to the function. As mentioned in the comments, you can actually just pass el in instead of a Boolean value, but I think it may be safer to pass it this way, but it doesn't matter, you can modify it if you feel uncomfortable:.
Let’s take a look at the function body of setNodeState. This function consists of two methods, one is to set the status of all child nodes, and the other is to set the status of the parent node of the node. The code is as follows:
function setNodeState(el,state){
_setChildNode(el,state);
_setParentNode(el,state);
}
In order to be able to set All child nodes of the current node Whether it is consistent with the status of the parent node (this node), we need the function _setChildNode. Similarly, in order to make the parent node of the node consistent with the status of the node, we need the _setParentNode function.Both functions are recursive functions and will traverse all child nodes, all parent nodes and sibling nodes (why we need to traverse sibling nodes will be discussed later). Let's first look at the code for traversing child nodes. The code is as follows:
function _setChildNode(el,state){
var childNodes = el.children;
if(childNodes.length > 0){// if has childrens
for (var i = 0 ;i _setChildNode(childNodes[i],state) ; }
}
}
This function first obtains all sub -nodes of the current node. Here you can use the function getChildren method provided by TreeView directly, and the original method I use to use HTML. If the node has child nodes, all child nodes are traversed, and the state of the child nodes is set to be consistent with the state of the current node. The role of the _saveCheckState function will be introduced later. The private_setAttribute method is an internal method for setting attributes provided by TreeView (js does not have the private keyword, this is just an explanation). This method will set the Checked attribute of each node to the current status of the node.
The following is the code for how to set the parent node state:
_setParentNode(el,state){
var parentNode = el.parentElement;
if(parentNode){
if(!_checkSiblingdNode( el)){
private_setAttribute(parentNode,"Checked",state);
_saveCheckState(parentNode);
_setParentNode(parent Node,state); > This function first obtains its parent node. If the parent node exists, it checks the sibling nodes of the current node. It is mentioned above that the sibling nodes need to be checked. The purpose of checking the sibling nodes here is: the status of the parent node is not controlled by the current node. (This situation only exists when the current node has no sibling nodes). If other brothers are selected, the parent node cannot be canceled, which will lead to inconsistency between the sibling nodes and the parent-child nodes. This function also calls the _saveCheckState function, which will be introduced later.
The following code describes how to check the status of sibling nodes. The code is as follows:
function _checkSiblingdNode(el){
var parentNode = el.parentElement;// you can use getParentTreeNode function in this htc, but I like this usage.
for(var i = 0;i tribute (parentNode.children[i],"Checked")){
🎜> You can use the getParentTreeNode method to get the parent node of a node, but I prefer to use the original method: . This will exclude itself (if(el!=parentNode.children[i])).
With the above code, we can achieve consistent selection of parent-child nodes without refreshing on the client. Now let’s introduce the _saveCheckState function. If there is no such function, after the page is submitted, except for the manually clicked node, other No nodes can save state. Therefore, the function of this function is to save the status of all nodes (mainly automatically selected nodes) to ensure that the status of the tree still exists after the return. The following code describes the _saveCheckState function. The code is as follows: }
This method first checks whether the index of the current node is valid, and if it is valid, saves the state. I need to talk about what the queueEvent method does here. I won’t post the code. The actual meaning of this function is to save the client’s operations in a hidden field called __TreeView1_State__, so that when returning, the server will This hidden field initializes the state of the tree. Among them, TreeView1 is the name of TreeView in my test system.
The final problem is deployment. Because htc has been modified, the original htc file needs to be replaced during deployment.
If you need to modify TreeView.htc (after formatting), you can change my message and leave an email on CSDN. My CSDN account is cuike519.
I hope Microsoft can add this function to the TreeView control as soon as possible, and it is best to achieve a polymorphic tree structure. Please browse the relevant comments on the blog, I will update the article in the comments!

Node.js excels at efficient I/O, largely thanks to streams. Streams process data incrementally, avoiding memory overload—ideal for large files, network tasks, and real-time applications. Combining streams with TypeScript's type safety creates a powe

The differences in performance and efficiency between Python and JavaScript are mainly reflected in: 1) As an interpreted language, Python runs slowly but has high development efficiency and is suitable for rapid prototype development; 2) JavaScript is limited to single thread in the browser, but multi-threading and asynchronous I/O can be used to improve performance in Node.js, and both have advantages in actual projects.

JavaScript originated in 1995 and was created by Brandon Ike, and realized the language into C. 1.C language provides high performance and system-level programming capabilities for JavaScript. 2. JavaScript's memory management and performance optimization rely on C language. 3. The cross-platform feature of C language helps JavaScript run efficiently on different operating systems.

JavaScript runs in browsers and Node.js environments and relies on the JavaScript engine to parse and execute code. 1) Generate abstract syntax tree (AST) in the parsing stage; 2) convert AST into bytecode or machine code in the compilation stage; 3) execute the compiled code in the execution stage.

The future trends of Python and JavaScript include: 1. Python will consolidate its position in the fields of scientific computing and AI, 2. JavaScript will promote the development of web technology, 3. Cross-platform development will become a hot topic, and 4. Performance optimization will be the focus. Both will continue to expand application scenarios in their respective fields and make more breakthroughs in performance.

Both Python and JavaScript's choices in development environments are important. 1) Python's development environment includes PyCharm, JupyterNotebook and Anaconda, which are suitable for data science and rapid prototyping. 2) The development environment of JavaScript includes Node.js, VSCode and Webpack, which are suitable for front-end and back-end development. Choosing the right tools according to project needs can improve development efficiency and project success rate.

Yes, the engine core of JavaScript is written in C. 1) The C language provides efficient performance and underlying control, which is suitable for the development of JavaScript engine. 2) Taking the V8 engine as an example, its core is written in C, combining the efficiency and object-oriented characteristics of C. 3) The working principle of the JavaScript engine includes parsing, compiling and execution, and the C language plays a key role in these processes.

JavaScript is at the heart of modern websites because it enhances the interactivity and dynamicity of web pages. 1) It allows to change content without refreshing the page, 2) manipulate web pages through DOMAPI, 3) support complex interactive effects such as animation and drag-and-drop, 4) optimize performance and best practices to improve user experience.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

Notepad++7.3.1
Easy-to-use and free code editor

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software
