search
HomeWeb Front-endJS TutorialHow to solve the BUG that cannot enter the visible area when ztree obtains the selected node_jquery

zTree is a multi-functional "tree plug-in" implemented with jQuery. Excellent performance, flexible configuration, and combination of multiple functions are the biggest advantages of zTree.

zTree Features Editing

● zTree v3.0 divides the core code according to functions, and unnecessary code does not need to be loaded
● Adopting lazy loading technology, tens of thousands of nodes can be easily loaded, and it can basically achieve instant killing even under IE6
● Compatible with IE, FireFox, Chrome, Opera, Safari and other browsers
● Support JSON data
● Supports static and Ajax asynchronous loading of node data
● Supports any skin change/custom icon (relying on css)
● Supports extremely flexible checkbox or radio selection function
● Provides multiple event response callbacks
● Flexible editing (add/delete/modify/check) functions, you can drag and drop nodes at will, and you can drag and drop multiple nodes
● Multiple Tree instances can be generated simultaneously in one page
● Simple parameter configuration to achieve flexible functions

Advantages of zTree Editing

The author of zTree uses his spare time to continuously improve zTree functions, and can also communicate with users in a timely manner and respond to various questions in a timely manner, so that new users can quickly master it. Currently, more and more users are using zTree to replace the original tree plug-in in the system, including the recently released latest version of the QUI framework.

Related plug-in version:

jquery.ztree.exedit-3.4.js

jquery.ztree.all-3.4.js

jquery-1.8.0.js

 function onAsyncSuccess(event, treeId, treeNode, msg) {
     curAsyncCount--;
     if (curStatus == "expand") {
       expandNodes(treeNode.children);
     } else if (curStatus == "async") {
       asyncNodes(treeNode.children);
     } 
     if (curAsyncCount <= ) {
       curStatus = "";
       // 节点定位
       if(devicesSelect.selectNodeId){
         // 节点变成被选中状态
         var zTree = $.fn.zTree.getZTreeObj(zTreeId);
         zTree.cancelSelectedNode();
         $("#" + devicesSelect.selectNodeId + "_a").addClass("curSelectedNode");
         $("#treeDiv").animate({scrollTop:$("#"+devicesSelect.selectNodeId).offset().top-},);//是ms,也可以用slow代替
         devicesSelect.selectNodeId = "";
       }
     }
   } 
   function expandNodes(nodes) {
     if (!nodes) return;
     curStatus = "expand";
     var zTree = $.fn.zTree.getZTreeObj(zTreeId);
     for (var i=, l=nodes.length; i<l; i++) {
       if(ids.indexOf(nodes[i].id) != -){
         if (nodes[i].isParent&&(ids.substring(,ids.indexOf(","))!=nodes[i].id)) {
           if(nodes[i].typeName.indexOf("虚拟")=="-"){
             zTree.expandNode(nodes[i], true, false, false);
           } else if(nodes[i].type.indexOf(type)>"-"){
             zTree.expandNode(nodes[i], true, false, false);
           }
         } else {
           goAsync = true;
         }
       }
     }
     if(goAsync==true){
       var id_ = ids.substring(,ids.indexOf(","));
       var node = zTree.getNodeByParam("id",id_);
       goAsync = false;
       me.curStatus = "";
       me.type = "";
       me.selectNodeId = node.tId;
     }
   } 

Positioning ideas:

1. Suppose you want to locate node A. The unique identifier of node A is objid

2. Get all superior objids from db based on objid, splice them together and save them in the ids variable.

3. Call expandNodes in the onAsyncSuccess method. In this method, pass [if (nodes[i].isParent&&(ids.substring(0,ids.indexOf(","))!=nodes[i].id) )] filter and expand the nodes with id in ids.

4. Under normal circumstances, at the end of expansion, obtain the node A object through [var node = zTree.getNodeByParam("id",id_);] based on objid, and then select it through [zTree.selectNode(node);] node.

In this case, the target node can be located and selected. However, when there is a lot of data with the same parent node as the node, node A may not appear in the visible area. Searching the source code, I found that Ztree uses the [$("#" node.tId).focus().blur();] method to achieve positioning. But unfortunately, there is a BUG.

So, use the scroll bar to control the positioning yourself. The implementation is as follows:

1. Delete [zTree.selectNode(node);] to prevent positioning conflicts

2. In the onAsyncSuccess method, determine when the node is expanded, obtain the offset of node A, and assign the value to the scrollTop attribute of the DIV where the tree is located.

$("#treeDiv1").animate({scrollTop:$("#" devicesSelect.selectNodeId).offset().top-300},1000);//1000 is ms, you can also use slow instead

Note: devicesSelect.selectNodeId is the node tid of node A, obtained through [me.selectNodeId = node.tId]; the id attribute of the div where the treeDiv1 tree is located

3. Cancel the previously selected node: zTree.cancelSelectedNode();

4. Add the selected status class to node A: $("#" devicesSelect.selectNodeId "_a").addClass("curSelectedNode");

The author said lightly: For the second point, I originally wanted to use [$("#treeDiv1").scrollTop($("#" devicesSelect.selectNodeId).offset().top-200);] , although it is effective, when there are too many nodes on the same layer, it still cannot enter the visible area. I deeply suspect that it is because the tree has not been expanded at this time, so I used animation, which is a rogue move.

The author’s last words: If there are any errors in this article, please point it out. I will be very grateful. . . .

The above content is the entire description of how to solve the bug that occurs when ztree cannot enter the visible area when acquiring selected nodes. I hope you like it.

Statement
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
Replace String Characters in JavaScriptReplace String Characters in JavaScriptMar 11, 2025 am 12:07 AM

Detailed explanation of JavaScript string replacement method and FAQ This article will explore two ways to replace string characters in JavaScript: internal JavaScript code and internal HTML for web pages. Replace string inside JavaScript code The most direct way is to use the replace() method: str = str.replace("find","replace"); This method replaces only the first match. To replace all matches, use a regular expression and add the global flag g: str = str.replace(/fi

Custom Google Search API Setup TutorialCustom Google Search API Setup TutorialMar 04, 2025 am 01:06 AM

This tutorial shows you how to integrate a custom Google Search API into your blog or website, offering a more refined search experience than standard WordPress theme search functions. It's surprisingly easy! You'll be able to restrict searches to y

Build Your Own AJAX Web ApplicationsBuild Your Own AJAX Web ApplicationsMar 09, 2025 am 12:11 AM

So here you are, ready to learn all about this thing called AJAX. But, what exactly is it? The term AJAX refers to a loose grouping of technologies that are used to create dynamic, interactive web content. The term AJAX, originally coined by Jesse J

Example Colors JSON FileExample Colors JSON FileMar 03, 2025 am 12:35 AM

This article series was rewritten in mid 2017 with up-to-date information and fresh examples. In this JSON example, we will look at how we can store simple values in a file using JSON format. Using the key-value pair notation, we can store any kind

10 jQuery Syntax Highlighters10 jQuery Syntax HighlightersMar 02, 2025 am 12:32 AM

Enhance Your Code Presentation: 10 Syntax Highlighters for Developers Sharing code snippets on your website or blog is a common practice for developers. Choosing the right syntax highlighter can significantly improve readability and visual appeal. T

8 Stunning jQuery Page Layout Plugins8 Stunning jQuery Page Layout PluginsMar 06, 2025 am 12:48 AM

Leverage jQuery for Effortless Web Page Layouts: 8 Essential Plugins jQuery simplifies web page layout significantly. This article highlights eight powerful jQuery plugins that streamline the process, particularly useful for manual website creation

10  JavaScript & jQuery MVC Tutorials10 JavaScript & jQuery MVC TutorialsMar 02, 2025 am 01:16 AM

This article presents a curated selection of over 10 tutorials on JavaScript and jQuery Model-View-Controller (MVC) frameworks, perfect for boosting your web development skills in the new year. These tutorials cover a range of topics, from foundatio

What is 'this' in JavaScript?What is 'this' in JavaScript?Mar 04, 2025 am 01:15 AM

Core points This in JavaScript usually refers to an object that "owns" the method, but it depends on how the function is called. When there is no current object, this refers to the global object. In a web browser, it is represented by window. When calling a function, this maintains the global object; but when calling an object constructor or any of its methods, this refers to an instance of the object. You can change the context of this using methods such as call(), apply(), and bind(). These methods call the function using the given this value and parameters. JavaScript is an excellent programming language. A few years ago, this sentence was

See all articles

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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!