Home Web Front-end JS Tutorial How to solve the problem that adding child nodes is always repeated when zTree is loaded asynchronously

How to solve the problem that adding child nodes is always repeated when zTree is loaded asynchronously

Mar 15, 2018 pm 04:20 PM
Add to node

This time I will show you how to solve the problem of adding sub-nodes that are always repeated when zTree is loaded asynchronously. How to solve the problem of adding sub-nodes that are always repeated when zTree is loaded asynchronously. What are the precautions? What are the practical cases? Let’s take a look.

zTree is a multi-functional "tree plug-in" implemented by jQuery. Through this article, I will share with you the duplication problem of jQuery zTree asynchronous loading to add child nodes. Friends who need it can refer to it

ZTree Introduction

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

zTree is open source and free software (MIT license). If you are interested in zTree or would like to fund the continued development of zTree, you can make a donation.

  • zTree v3.0 divides the core code according to functions. Unnecessary code does not need to be loaded.

  • adopts lazy loading technology. Tens of thousands of nodes can be loaded easily, even under IE6, it can basically achieve instant killing

  • Compatible with IE, FireFox, Chrome, Opera, Safari and other browsers

  • Support JSON data

  • Support static and Ajax asynchronous loading of node data

  • Support any change of skin/custom icon (rely on css)

  • Supports extremely flexible checkbox or radio selection function

  • Provides a variety of eventsResponse callbacks

  • Flexible editing (add/delete/modify/check) functions, you can drag and drop nodes at will, and you can drag and drop multiple nodes

  • In one page Multiple Tree instances can be generated at the same time

  • Simple parameter configuration to achieve flexible and changeable functions

Original question

//添加结点, 产品和版本
function addNode(event) {
  rMenu.css({ "visibility": "hidden" });
  var treeNode = zTree.getSelectedNodes()[0];
  var pid;
  var nodeName;
  var treelevel;
  if (!treeNode && event.target.tagName.toLowerCase() != "button" && $(event.target).parents("a").length == 0) {
   //添加产品结点
pid = 0;
   treeNode = null;
   treelevel = 1;
  } else if (treeNode) {
   //添加版本结点
pid = treeNode.id;
  treelevel = 2;
  }
  $.post(
   "AddNode.action",
   { type: treelevel, id: pid },
   function(nodeIdAndName) {
    var params = /([^\|]+)\|([^\|]+)/.exec(nodeIdAndName);
    if (!((!treeNode && event.target.tagName.toLowerCase() != "button" && $(event.target).parents("a").length == 0) || treeNode.open)) {
     zTree.expandNode(treeNode, true);
    }
    treeNode = zTree.addNodes(treeNode, { id: params[1], pid: pid, isParent: "true", name: params[2], editable: "true", treelevel: treelevel });
   });
}
Copy after login

When I originally added child nodes directly, if the parent node was not expanded, two identical child nodes would be added (the first time); later I checked whether the parent node was expanded. However, if the parent node is expanded, two identical child nodes will be added (for the first time). How to solve this problem?

Method 1

The problem can be solved by changing

if (!((!treeNode && event.target.tagName.toLowerCase() != "button" && $(event.target).parents("a").length == 0) || treeNode.open)) {
     zTree.expandNode(treeNode, true);
    }
    treeNode = zTree.addNodes(treeNode, { id: params[1], pid: pid, isParent: "true", name: params[2], editable: "true", treelevel: treelevel });
Copy after login

to

if(!treeNode && event.target.tagName.toLowerCase() != "button" && $(event.target).parents("a").length == 0)
    {
     treeNode = zTree.addNodes(treeNode, { id: params[1], pid: pid, isParent: "true", name: params[2], editable: "true", treelevel: treelevel });
    }
    else if(treeNode.open)
    {
     if(treeNode.isParent)
     {
      zTree.reAsyncChildNodes(treeNode, "refresh");
     }
     else
     {
      treeNode.isParent=true;
      zTree.reAsyncChildNodes(treeNode, "refresh");
     }
    }
    else
    {
     zTree.expandNode(treeNode, true);
     treeNode = zTree.addNodes(treeNode, { id: params[1], pid: pid, isParent: "true", name: params[2], editable: "true", treelevel: treelevel });
    }
Copy after login

, but Is there any optimization? I feel like there are too many code categories after the change

The best way

It doesn’t seem to be so troublesome, right? I answered a similar question two days ago.

1. After clicking to add a child node, directly send ajax to the background to save the data and capture the success event

2. When ajax succeeds, you can use the treeNode.zAsync attribute to know whether the parent node is Asynchronous loading has been performed. If it is false, then directly reAsyncChildNodes is refreshed. If it is true, then use addN...

if ((!treeNode && event.target.tagName.toLowerCase() != "button" && $(event.target).parents("a").length == 0) || treeNode.zAsync) 
     treeNode = zTree.addNodes(treeNode, { id: params[1], pid: pid, isParent: "true", name: params[2], editable: "true", treelevel: treelevel });
    else
     zTree.reAsyncChildNodes(treeNode, "refresh");
Copy after login

I believe you have mastered it after reading the case in this article. Method, for more exciting information, please pay attention to other related articles on the php Chinese website!

Recommended reading:

How to discover hidden elements in blank spaces

How to achieve non-refresh linkage of drop-down boxes with Ajax

Detailed explanation of no new construction

jQuery implementation of drop-down menu navigation

The above is the detailed content of How to solve the problem that adding child nodes is always repeated when zTree is loaded asynchronously. For more information, please follow other related articles on the PHP Chinese website!

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

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)
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

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)

How to add a TV to Mijia How to add a TV to Mijia Mar 25, 2024 pm 05:00 PM

Many users are increasingly favoring the electronic ecosystem of Xiaomi smart home interconnection in modern life. After connecting to the Mijia APP, you can easily control the connected devices with your mobile phone. However, many users still don’t know how to add Mijia to their homes. app, then this tutorial guide will bring you the specific connection methods and steps, hoping to help everyone in need. 1. After downloading Mijia APP, create or log in to Xiaomi account. 2. Adding method: After the new device is powered on, bring the phone close to the device and turn on the Xiaomi TV. Under normal circumstances, a connection prompt will pop up. Select "OK" to enter the device connection process. If no prompt pops up, you can also add the device manually. The method is: after entering the smart home APP, click the 1st button on the lower left

Tutorial on adding a new hard drive in win11 Tutorial on adding a new hard drive in win11 Jan 05, 2024 am 09:39 AM

When buying a computer, we may not necessarily choose a large hard drive. At this time, if we want to add a new hard drive to win11, we can first install the new hard drive we purchased, and then add partitions to the computer. Tutorial on adding a new hard drive in win11: 1. First, we disassemble the host and find the slot of the hard drive. 2. After finding it, we first connect the "data cable", which usually has a fool-proof design. If it cannot be inserted, just reverse the direction. 3. Then insert the new hard drive into the hard drive slot. 4. After inserting, connect the other end of the data cable to the computer's motherboard. 5. After the installation is completed, you can put it back into the host and turn it on. 6. After booting, we right-click "This Computer" and open "Computer Management" 7. After opening, click "Disk Management" in the lower left corner 8. Then on the right you can

Tutorial to quickly create desktop shortcuts in Win11 Tutorial to quickly create desktop shortcuts in Win11 Dec 27, 2023 pm 04:29 PM

In win11, we can quickly start software or files on the desktop by adding desktop shortcuts, and we only need to right-click the required files to operate. Add a desktop shortcut in win11: 1. Open "This PC" and find the file or software you want to add a desktop shortcut to. 2. After finding it, right-click to select it and click "Show more options" 3. Then select "Send to" - "Desktop Shortcut" 4. After the operation is completed, you can find the shortcut on the desktop.

How to add a new script in Tampermonkey-How to delete a script in Tampermonkey How to add a new script in Tampermonkey-How to delete a script in Tampermonkey Mar 18, 2024 pm 12:10 PM

Tampermonkey Chrome extension is a user script management plug-in that improves user efficiency and browsing experience through scripts. So how does Tampermonkey add new scripts? How to delete the script? Let the editor give you the answer below! How to add a new script to Tampermonkey: 1. Take GreasyFork as an example. Open the GreasyFork web page and enter the script you want to follow. The editor here chooses one-click offline download. 2. Select a script. , after entering the script page, you can see the button to install this script. 3. Click to install this script to come to the installation interface. Just click here to install. 4. We can see the installed one-click in the installation script.

How to connect to Polygon network in MetaMask wallet? Tutorial guide for connecting MetaMask wallet to Polygon network How to connect to Polygon network in MetaMask wallet? Tutorial guide for connecting MetaMask wallet to Polygon network Jan 19, 2024 pm 04:36 PM

How to add a PolygonMainnet network To use MATIC (Polygon) with Metamask, you need to add a private network called "PolygonMainnet". Transferring in with the wrong network address can cause problems, so be sure to use the "PolygonMainnet" network before transferring out of $MATIC. The Metamask wallet is connected to the Ethereum mainnet by default, but we can simply add "PolygonMainnet" and use $MATIC. Just a few simple copy and paste steps and you're done. First, in the Metamask wallet, click on the network option in the upper right corner and select "C

How to add watermark to images in Vue? How to add watermark to images in Vue? Aug 19, 2023 pm 12:37 PM

How to add watermark to images in Vue? Vue is a popular JavaScript framework that is widely used for building web applications. Sometimes we need to add watermarks to images in Vue applications to protect the copyright of the image or increase the recognizability of the image. In this article, I will introduce you to a method of adding watermarks to images in Vue and provide corresponding code examples. The first step is to introduce a third-party library for adding watermarks to Vue. It is recommended to use watermarkj

Outlook stuck on adding account [Fixed] Outlook stuck on adding account [Fixed] Mar 23, 2024 pm 12:21 PM

When you encounter problems adding accounts in Outlook, you can try the following solutions to resolve it. Typically this can be caused by a faulty network connection, corrupted user profiles, or other temporary issues. Through the methods provided in this article, you can easily solve these problems and ensure that your Outlook can run normally. Outlook stuck on adding account If your Outlook is stuck on adding account, then use these fixes mentioned below: Disconnect and reconnect the internet Temporarily disable antivirus software Create a new Outlook profile Try adding account in safe mode Disable IPv6 Run Microsoft Support and Recovery Assistant Repair Office Application Outlook Add Account Required

How does the MetaMask little fox add the BSC network in 2023? (2023 latest version of MetaMask little fox adds BSC network tutorial) How does the MetaMask little fox add the BSC network in 2023? (2023 latest version of MetaMask little fox adds BSC network tutorial) Jan 19, 2024 pm 05:39 PM

The BSC network is a high-performance blockchain developed by the cryptocurrency exchange Binance. Its full English name is Binance Smart Chain. The BSC blockchain is compatible with the EVM network and therefore can be accessed and interacted with using the MetaMask wallet. So how does the Little Fox Wallet add the BSC network? Let’s take a look with the editor below. 1. MetaMask directly adds the BSC network. First open the MetaMask wallet, click the avatar in the upper right corner - Settings - Network and click the "Add Network" button to enter the network settings page of the Little Fox wallet. The latest version of Metamask has built-in popular blockchain networks, including currency An smart chain. We just need to order BNBSmartCha

See all articles