Treeview parent-child linkage effect to keep the node status consistent

Source: Internet
Author: User
Most of us have used the Treeview control, and the evaluation of this control is also diverse, but I think it is a free open-source control, so I still use it. ASP. net, remember to create a permission tree for assigning permissions. At that time, I only knew this tree. After a day of research, I found out the basic behavior of the server, however, due to the limited JS level at that time, I was very afraid of the client code and had never seen it.
At that time, there was a requirement that if a node is selected, all the child nodes of the node should be selected, if all the sub-nodes of the node are deselected, the node also needs to be deselected (node consistency). The opposite is true.
Another requirement is that it is better to implement a three-state tree (this is a bit difficult and will not be discussed in this article ). This article describes in detail requirements 1.
First of all, we should be glad that Microsoft's Treeview control is open-source. You can easily obtain it from both client code and server code and download it from Microsoft's website. I have also seen some articles on the Internet about this issue. Most of them use JS implementations other than Treeview. I personally think that from the object-oriented perspective, this function belongs to Treeview, so there is no reason to separate it, so today I will modify the Treeview. HTC to implement the above functions. To get this file (Treeview. HTC), you can download it on Microsoft's website.
To be honest, this feature has plagued me for a long time. I have been trying 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 handler function needs to be stimulated in 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 )"
Therefore, we can go to HTC (HTC in the future refers to Treeview. HTC), find the docheckboxclick method, and you only need to look at the name to know what it is (the name is too important, otherwise it is really tiring to find a function in line 3000 code ).
This method is the function to be processed when you click 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 rows are added by me. The first and second rows are modifications made to the failed checked attribute after the page is returned, and El is submitted according to the original method. checked is undefined, so the node cannot be correctly synchronized. If you are interested, try it. The setnodestate function is used to set the node status from the name. It passes the node you selected as a parameter to the function. As mentioned in the comment, you can only pass El in, instead of passing a Boolean value. However, I think the transfer may be safer, but it doesn't matter. If you feel uncomfortable, you can modify it :.
Let's take a look at the setnodestate function body. 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 );
}
To set whether all the child nodes of the current node are in the same state as the parent node (the current node), we need the function _ setchildnode, similarly, we need the _ setparentnode function to enable the node's parent node and its State to reach the _ setparentnode function. Both functions are recursive functions that traverse all child nodes, all parent nodes, and sibling nodes (why do we need to traverse sibling nodes later ), the code for Traversing child nodes is as follows:
Function _ setchildnode (El, state ){
VaR childnodes = El. Children;
If (childnodes. length> 0) {// If has Childs
For (VAR I = 0; I <= childnodes. Length-1; I ++ ){
Private_setattribute (childnodes [I], "checked", State );
_ Savecheckstate (childnodes [I]);
_ Setchildnode (childnodes [I], State );
}
}
}
This function first obtains all the subnodes of the current node. Here, you can directly use the getchildren method provided by Treeview, while I use the original HTML method. If the node has a subnode, it traverses all the subnodes and sets the subnode status to the same as that of the current node. The function of _ savecheckstate will be described later. The private_setattribute method is an internal attribute setting method provided by Treeview (JS does not have a private keyword, but this is just an illustration). This method sets the checked attribute of each node to the status of the current node.
The following code sets the status of the parent node:
Function _ setparentnode (El, state ){
VaR parentnode = El. parentelement;
If (parentnode ){
If (! _ Checksiblingdnode (EL )){
Private_setattribute (parentnode, "checked", State );
_ Savecheckstate (parentnode );
_ Setparentnode (parentnode, State );
}
}
}
This function first obtains the parent node. If the parent node exists, it checks the sibling node of the current node. The purpose of checking the sibling node is as follows: the status of the parent node is not controlled by one node of the current node (this situation only exists when the current node does not have a sibling node). If other siblings are selected, the parent node cannot be canceled, which will lead to inconsistency between the sibling node and the parent and child node. This function also calls the _ savecheckstate function, which will be described later.
The following code describes how to check the status of a sibling node:
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 <= parentnode. Children. Length-1; I ++ ){
If (El! = Parentnode. Children [I]) {
If (private_getattribute (parentnode. Children [I], "checked ")){
Return true;
}
}
}
Return false;
}
As described in the comment, you can use the getparenttreenode method to obtain the node's parent node, but I prefer the original method :. Exclude yourself (if (El! = Parentnode. Children [I]).
With the above code, we can achieve consistent selection of the client without refreshing the New Parent and Child Nodes. Now let's introduce the _ savecheckstate function. If the function page is not submitted, nodes other than manually clicked nodes cannot be saved. Therefore, this function is used to save the status of all nodes (mainly automatically selected nodes) and ensure that the status of the tree still exists after the return. The following code describes the _ savecheckstate function. The Code is as follows:
Function _ savecheckstate (EL ){
If (getnodeindex (EL ))
Queueevent ('oncheck', getnodeindex (EL ));
}
This method first checks whether the index of the current node is valid. If it is valid, it is saved. Here we need to talk about what the queueevent method has done, and I will not post the code. The actual meaning of this function is to save the client operation in a hidden domain named _ treeview1_state, in this way, the server will initialize the tree status based on the hidden domain during the return. Here, treeview1 is the name of my Treeview in the test system.
The final problem is the deployment problem. Because HTC is modified, the original HTC file needs to be replaced during deployment.
If you need to modify Treeview. HTC (formatted), you can 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, so it is best to implement a multi-state tree structure. Please visit the related comments of the blog. I will update the article in the comments!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.