Home Web Front-end JS Tutorial javascript-TreeView parent-child linkage effect keeps node status consistent_javascript skills

javascript-TreeView parent-child linkage effect keeps node status consistent_javascript skills

May 16, 2016 pm 07:10 PM

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!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

What should I do if I encounter garbled code printing for front-end thermal paper receipts? What should I do if I encounter garbled code printing for front-end thermal paper receipts? Apr 04, 2025 pm 02:42 PM

Frequently Asked Questions and Solutions for Front-end Thermal Paper Ticket Printing In Front-end Development, Ticket Printing is a common requirement. However, many developers are implementing...

Demystifying JavaScript: What It Does and Why It Matters Demystifying JavaScript: What It Does and Why It Matters Apr 09, 2025 am 12:07 AM

JavaScript is the cornerstone of modern web development, and its main functions include event-driven programming, dynamic content generation and asynchronous programming. 1) Event-driven programming allows web pages to change dynamically according to user operations. 2) Dynamic content generation allows page content to be adjusted according to conditions. 3) Asynchronous programming ensures that the user interface is not blocked. JavaScript is widely used in web interaction, single-page application and server-side development, greatly improving the flexibility of user experience and cross-platform development.

Who gets paid more Python or JavaScript? Who gets paid more Python or JavaScript? Apr 04, 2025 am 12:09 AM

There is no absolute salary for Python and JavaScript developers, depending on skills and industry needs. 1. Python may be paid more in data science and machine learning. 2. JavaScript has great demand in front-end and full-stack development, and its salary is also considerable. 3. Influencing factors include experience, geographical location, company size and specific skills.

How to merge array elements with the same ID into one object using JavaScript? How to merge array elements with the same ID into one object using JavaScript? Apr 04, 2025 pm 05:09 PM

How to merge array elements with the same ID into one object in JavaScript? When processing data, we often encounter the need to have the same ID...

Is JavaScript hard to learn? Is JavaScript hard to learn? Apr 03, 2025 am 12:20 AM

Learning JavaScript is not difficult, but it is challenging. 1) Understand basic concepts such as variables, data types, functions, etc. 2) Master asynchronous programming and implement it through event loops. 3) Use DOM operations and Promise to handle asynchronous requests. 4) Avoid common mistakes and use debugging techniques. 5) Optimize performance and follow best practices.

How to achieve parallax scrolling and element animation effects, like Shiseido's official website?
or:
How can we achieve the animation effect accompanied by page scrolling like Shiseido's official website? How to achieve parallax scrolling and element animation effects, like Shiseido's official website? or: How can we achieve the animation effect accompanied by page scrolling like Shiseido's official website? Apr 04, 2025 pm 05:36 PM

Discussion on the realization of parallax scrolling and element animation effects in this article will explore how to achieve similar to Shiseido official website (https://www.shiseido.co.jp/sb/wonderland/)...

The Evolution of JavaScript: Current Trends and Future Prospects The Evolution of JavaScript: Current Trends and Future Prospects Apr 10, 2025 am 09:33 AM

The latest trends in JavaScript include the rise of TypeScript, the popularity of modern frameworks and libraries, and the application of WebAssembly. Future prospects cover more powerful type systems, the development of server-side JavaScript, the expansion of artificial intelligence and machine learning, and the potential of IoT and edge computing.

The difference in console.log output result: Why are the two calls different? The difference in console.log output result: Why are the two calls different? Apr 04, 2025 pm 05:12 PM

In-depth discussion of the root causes of the difference in console.log output. This article will analyze the differences in the output results of console.log function in a piece of code and explain the reasons behind it. �...

See all articles