search
HomeWeb Front-endJS TutorialAngular combines with zTree to asynchronously load node data instance sharing

This article mainly shares with you the difficulties and methods of asynchronously loading node data using Angular combined with zTree. Friends who have needs in this regard can refer to it. I hope it can help everyone.

1 Prerequisite preparation

1.1 Create a new angular4 project

1.2 Go to zTree official website to download zTree

zTree official website: Click to go to

Version used by Sanshao: Click to go to

##2 Programming steps

 

It can be seen from printing out the zTree object that the zTree object uses the init method to implement the zTree structure; the init method receives three parameters

Parameter 1: A DOM node object with an ul tag

Parameters 2: Basic configuration object

Parameter 3: Title information array

2.1 Introduce relevant js and css into index.html



nbsp;html>


 <meta>
 <title>TestZtree</title>
 <base>

 <meta>
 <link>

 <link>
 <link>
 <script></script>
 <script></script>


 <app-root></app-root>

View Code

2.2 Declare the jquery object in the TS file


declare var $ : any;
2.3 Write code in TS file

##

import { Component, OnInit } from '@angular/core';
declare var $ : any;
@Component({
 selector: 'app-root',
 templateUrl: './app.component.html',
 styleUrls: ['./app.component.scss']
})
export class AppComponent implements OnInit {

 // setting = {
 // view: {
 // showLine: true,
 // showIcon: true,
 // fontCss: this.getFont
 // },
 // data: {
 // simpleData: {
 // enable: true,
 // idKey: 'id',
 // pIdKey: 'pId'
 // }
 // },
 // callback: {
 // onClick: this.onCzTreeOnClick
 // }
 // };
 // zNodes = [
 // {id: 1, pId: 0, name: '1 一级标题', open: true, iconOpen:"assets/zTree/css/zTreeStyle/img/diy/1_open.png", iconClose:"assets/zTree/css/zTreeStyle/img/diy/1_close.png"},
 // {id: 11, pId: 1, name: '1.1 二级标题', open: true, font:{'background-color':'skyblue', 'color':'white'}},
 // {id: 111, pId: 11, name: '1.1.1 三级标题 -> 博客园', url: 'http://www.cnblogs.com/NeverCtrl-C/'},
 // {id: 112, pId: 11, name: '1.1.2 三级标题 -> 单击', click: "alert('你单击了')"},
 // {id: 12, pId: 1, name: '1.2 二级标题'},
 // {id: 2, pId: 0, name: '2 一级标题'}
 // ]
 // getFont(treeId, node) {
 // return node.font ? node.font : {};
 // }
 // onCzTreeOnClick(event, treeId, treeNode, clickFlag) {
 // alert(treeNode.name);
 // } 
 setting = {
 data: {
 simpleData: {
 enable: true
 }
 }
 };
 zNodes = [
 {id: 1, pId: 0, name: '1 一级标题'},
 {id: 11, pId: 1, name: '1.1 二级标题'},
 {id: 111, pId: 11, name: '1.1.1 三级标题'},
 {id: 112, pId: 11, name: '1.1.2 三级标题'},
 {id: 12, pId: 1, name: '1.2 二级标题'},
 {id: 2, pId: 0, name: '2 一级标题'}
 ];
 constructor() { }
 ngOnInit() { 
 console.log($);
 console.log($.fn.zTree);
 $.fn.zTree.init($("#ztree"),this.setting,this.zNodes);
 }
}

View Code

2.4 In component HTML Write code in



      2.5 Effect display

      3 zTree basic functions

      3.1 Not displayed Connecting lines

      3.1.1 Official documentation

      Does not display connecting lines between titles

      3.1.2 Programming steps

      Just specify the value of showLine attribute as false in the basic configuration object

     setting = {
     data: {
     simpleData: {
     enable: true
     }
     },
     view: {
     showLine: false
     }
     };

    3.2 Do not display the node icon

    3.2.1 Official document

    Remove the icon in front of the node

    #3.2.2 Programming steps

    Set the showIcon attribute of the basic configuration object to false


    ##

    setting = {
     data: {
     simpleData: {
     enable: true
     }
     },
     view: {
     showLine: false,
     showIcon: false
     }
     };
    View Code


    3.3 Custom node icon

    3.3.1 Official document

    Change the icon of the node

    3.3.2 Programming steps

    Set icon/iconOpen for treeNode node data /iconClose attribute

    3.4 Custom font

    3.4.1 Official document

    Change the style of node font

    3.4.2 Programming steps

    Just set the font attribute for the treeNode node data. The value of the font attribute is an object, and the content of the object is the same as the style data

    3.4.3 Effect display

    ##3.5 Hyperlink

    3.5.1 Official document

    Clicking on the node title will automatically jump to the corresponding url

    Note 01: The click attribute can only perform the simplest click event operation. content equivalent to . If the operation is more complex, use the onClick event callback function.

    3.5.2 Programming steps

    Set the url and click attributes for the treeNode node data

    Tip 01: When setting the click attribute , the attribute value must be some simple onClick event

    Tip 02: When setting the target attribute, the attribute values ​​​​are _blank and _self

    _blank -> Open in a new window

    _self -> Open in the original window

    ##
     zNodes = [
     {id: 1, pId: 0, name: '1 一级标题', open: true, iconOpen:"assets/zTree/css/zTreeStyle/img/diy/1_open.png", iconClose:"assets/zTree/css/zTreeStyle/img/diy/1_close.png"},
     {id: 11, pId: 1, name: '1.1 二级标题', open: true, font:{'background-color':'skyblue', 'color':'white'}},
     {id: 111, pId: 11, name: '1.1.1 三级标题 -> 博客园1', url: 'http://www.cnblogs.com/NeverCtrl-C/', target: '_blank'},
     {id: 113, pId: 11, name: '1.1.1 三级标题 -> 博客园2', url: 'http://www.cnblogs.com/NeverCtrl-C/', target: '_self'},
     {id: 112, pId: 11, name: '1.1.2 三级标题 -> 单击', click: "alert('你单击了')"},
     {id: 12, pId: 1, name: '1.2 二级标题'},
     {id: 2, pId: 0, name: '2 一级标题'}
     ]

    View Code

    3.6 Click control


    3.6.1 Official document

    The corresponding method is triggered when the node title is clicked

    Tips 01: You can use this usage in angular Implement route jump

    3.6.2 编程步骤

    设置基本配置对象的onClick属性

    技巧01:onClick属性值是一个方法的引用,我们需要自己编写这个方法



     setting = {
     view: {
     showLine: true,
     showIcon: true,
     fontCss: this.getFont
     },
     data: {
     simpleData: {
     enable: true,
     idKey: 'id',
     pIdKey: 'pId'
     }
     },
     callback: {
     onClick: this.onCzTreeOnClick
     }
     };

    View Code

    编写onClick触发方法



     onCzTreeOnClick(event, treeId, treeNode, clickFlag) {
     alert(treeNode.name);
     }

    View Code

    3.6.3 代码汇总



    import { Component, OnInit } from '@angular/core';
    declare var $ : any;
    @Component({
     selector: 'app-root',
     templateUrl: './app.component.html',
     styleUrls: ['./app.component.scss']
    })
    export class AppComponent implements OnInit {
     setting = {
     view: {
     showLine: true,
     showIcon: true,
     fontCss: this.getFont
     },
     data: {
     simpleData: {
     enable: true,
     idKey: 'id',
     pIdKey: 'pId'
     }
     },
     callback: {
     onClick: this.onCzTreeOnClick
     },
     // async: {
     // enable: true,
     // url:"http://localhost:3000/data",
     // type: "get",
     // // autoParam:["id", "name=n", "level=lv"],
     // // otherParam:{"otherParam":"zTreeAsyncTest"},
     // dataFilter: this.filter
     // }
     };
     zNodes = [
     {id: 1, pId: 0, name: '1 一级标题', open: true, iconOpen:"assets/zTree/css/zTreeStyle/img/diy/1_open.png", iconClose:"assets/zTree/css/zTreeStyle/img/diy/1_close.png"},
     {id: 11, pId: 1, name: '1.1 二级标题', open: true, font:{'background-color':'skyblue', 'color':'white'}},
     {id: 111, pId: 11, name: '1.1.1 三级标题 -> 博客园1', url: 'http://www.cnblogs.com/NeverCtrl-C/', target: '_blank'},
     {id: 113, pId: 11, name: '1.1.1 三级标题 -> 博客园2', url: 'http://www.cnblogs.com/NeverCtrl-C/', target: '_self'},
     {id: 112, pId: 11, name: '1.1.2 三级标题 -> 单击', click: "alert('你单击了')"},
     {id: 12, pId: 1, name: '1.2 二级标题'},
     {id: 2, pId: 0, name: '2 一级标题'}
     ]
     getFont(treeId, node) {
     return node.font ? node.font : {};
     }
     // filter(treeId, parentNode,responseData) {
     // console.log(responseData);
     // if (responseData) {
     // for(var i =0; i 

    View Code

    3.7 异步加载节点数据

    3.7.1 官方文档

    节点的数据是从后台进行获取的

    3.7.2 编程步骤

    技巧01:异步加载节点数据时init方法不用传递第三个参数

    > 准备一个后台用于返回JSON格式的数据

    技巧01:返回的JSON数据是一个列表,格式为


    [
     {
     "id": 1,
     "pId": 0,
     "name": "1 one"
     },
     {
     "id": 2,
     "pId": 0,
     "name": "2 two"
     }
     ]

    技巧02:三少偷懒,是利用json-server模拟的后台数据,哈哈;json-server

    > 设置基本配置对象的async属性



     setting = {
     view: {
     showLine: true,
     showIcon: true,
     fontCss: this.getFont
     },
     data: {
     simpleData: {
     enable: true,
     idKey: 'id',
     pIdKey: 'pId'
     }
     },
     callback: {
     onClick: this.onCzTreeOnClick
     },
     async: {
     enable: true,
     url:"http://localhost:3000/data",
     type: "get",
     // autoParam:["id", "name=n", "level=lv"],
     // otherParam:{"otherParam":"zTreeAsyncTest"},
     dataFilter: this.filter
     }
     };

    View Code

    > 编写响应数据处理方法



     filter(treeId, parentNode,responseData) {
     console.log(responseData);
     if (responseData) {
     for(var i =0; i <p>View Code</p><p>3.7.3 代码总汇</p><p class="cnblogs_code"><br></p><p class="cnblogs_code_hide"><br></p><pre class="brush:php;toolbar:false">{
     "data": 
     [
     {
     "id": 1,
     "pId": 0,
     "name": "1 one"
     },
     {
     "id": 11,
     "pId": 1,
     "name": "1.1 oneToOne"
     },
     {
     "id": 12,
     "pId": 1,
     "name": "1.2 oneToTwo"
     },
     {
     "id": 2,
     "pId": 0,
     "name": "2 two"
     }
     ]
    }

    模拟后台响应数据



        HTML



      import { Component, OnInit } from '@angular/core';
      declare var $ : any;
      
      @Component({
       selector: 'app-root',
       templateUrl: './app.component.html',
       styleUrls: ['./app.component.scss']
      })
      export class AppComponent implements OnInit {
      
       setting = {
       view: {
       showLine: true,
       showIcon: true,
       fontCss: this.getFont
       },
       data: {
       simpleData: {
       enable: true,
       idKey: 'id',
       pIdKey: 'pId'
       }
       },
       callback: {
       onClick: this.onCzTreeOnClick
       },
       async: {
       enable: true,
       url:"http://localhost:3000/data",
       type: "get",
       // autoParam:["id", "name=n", "level=lv"],
       // otherParam:{"otherParam":"zTreeAsyncTest"},
       dataFilter: this.filter
       }
       };
      
       // zNodes = [
       // {id: 1, pId: 0, name: '1 一级标题', open: true, iconOpen:"assets/zTree/css/zTreeStyle/img/diy/1_open.png", iconClose:"assets/zTree/css/zTreeStyle/img/diy/1_close.png"},
       // {id: 11, pId: 1, name: '1.1 二级标题', open: true, font:{'background-color':'skyblue', 'color':'white'}},
       // {id: 111, pId: 11, name: '1.1.1 三级标题 -> 博客园1', url: 'http://www.cnblogs.com/NeverCtrl-C/', target: '_blank'},
       // {id: 113, pId: 11, name: '1.1.1 三级标题 -> 博客园2', url: 'http://www.cnblogs.com/NeverCtrl-C/', target: '_self'},
       // {id: 112, pId: 11, name: '1.1.2 三级标题 -> 单击', click: "alert('你单击了')"},
       // {id: 12, pId: 1, name: '1.2 二级标题'},
       // {id: 2, pId: 0, name: '2 一级标题'}
       // ]
      
       getFont(treeId, node) {
       return node.font ? node.font : {};
       }
      
       filter(treeId, parentNode,responseData) {
       console.log(responseData);
       if (responseData) {
       for(var i =0; i 

      TS

      3.7.4 效果展示

      相关推荐:

      jquery easyui tree异步加载子节点详解

      JavaScript文件的同步和异步加载的实现代码

      zTree异步加载展开第一级节点方法实现

      The above is the detailed content of Angular combines with zTree to asynchronously load node data instance sharing. For more information, please follow other related articles on the PHP Chinese website!

      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
      From Websites to Apps: The Diverse Applications of JavaScriptFrom Websites to Apps: The Diverse Applications of JavaScriptApr 22, 2025 am 12:02 AM

      JavaScript is widely used in websites, mobile applications, desktop applications and server-side programming. 1) In website development, JavaScript operates DOM together with HTML and CSS to achieve dynamic effects and supports frameworks such as jQuery and React. 2) Through ReactNative and Ionic, JavaScript is used to develop cross-platform mobile applications. 3) The Electron framework enables JavaScript to build desktop applications. 4) Node.js allows JavaScript to run on the server side and supports high concurrent requests.

      Python vs. JavaScript: Use Cases and Applications ComparedPython vs. JavaScript: Use Cases and Applications ComparedApr 21, 2025 am 12:01 AM

      Python is more suitable for data science and automation, while JavaScript is more suitable for front-end and full-stack development. 1. Python performs well in data science and machine learning, using libraries such as NumPy and Pandas for data processing and modeling. 2. Python is concise and efficient in automation and scripting. 3. JavaScript is indispensable in front-end development and is used to build dynamic web pages and single-page applications. 4. JavaScript plays a role in back-end development through Node.js and supports full-stack development.

      The Role of C/C   in JavaScript Interpreters and CompilersThe Role of C/C in JavaScript Interpreters and CompilersApr 20, 2025 am 12:01 AM

      C and C play a vital role in the JavaScript engine, mainly used to implement interpreters and JIT compilers. 1) C is used to parse JavaScript source code and generate an abstract syntax tree. 2) C is responsible for generating and executing bytecode. 3) C implements the JIT compiler, optimizes and compiles hot-spot code at runtime, and significantly improves the execution efficiency of JavaScript.

      JavaScript in Action: Real-World Examples and ProjectsJavaScript in Action: Real-World Examples and ProjectsApr 19, 2025 am 12:13 AM

      JavaScript's application in the real world includes front-end and back-end development. 1) Display front-end applications by building a TODO list application, involving DOM operations and event processing. 2) Build RESTfulAPI through Node.js and Express to demonstrate back-end applications.

      JavaScript and the Web: Core Functionality and Use CasesJavaScript and the Web: Core Functionality and Use CasesApr 18, 2025 am 12:19 AM

      The main uses of JavaScript in web development include client interaction, form verification and asynchronous communication. 1) Dynamic content update and user interaction through DOM operations; 2) Client verification is carried out before the user submits data to improve the user experience; 3) Refreshless communication with the server is achieved through AJAX technology.

      Understanding the JavaScript Engine: Implementation DetailsUnderstanding the JavaScript Engine: Implementation DetailsApr 17, 2025 am 12:05 AM

      Understanding how JavaScript engine works internally is important to developers because it helps write more efficient code and understand performance bottlenecks and optimization strategies. 1) The engine's workflow includes three stages: parsing, compiling and execution; 2) During the execution process, the engine will perform dynamic optimization, such as inline cache and hidden classes; 3) Best practices include avoiding global variables, optimizing loops, using const and lets, and avoiding excessive use of closures.

      Python vs. JavaScript: The Learning Curve and Ease of UsePython vs. JavaScript: The Learning Curve and Ease of UseApr 16, 2025 am 12:12 AM

      Python is more suitable for beginners, with a smooth learning curve and concise syntax; JavaScript is suitable for front-end development, with a steep learning curve and flexible syntax. 1. Python syntax is intuitive and suitable for data science and back-end development. 2. JavaScript is flexible and widely used in front-end and server-side programming.

      Python vs. JavaScript: Community, Libraries, and ResourcesPython vs. JavaScript: Community, Libraries, and ResourcesApr 15, 2025 am 12:16 AM

      Python and JavaScript have their own advantages and disadvantages in terms of community, libraries and resources. 1) The Python community is friendly and suitable for beginners, but the front-end development resources are not as rich as JavaScript. 2) Python is powerful in data science and machine learning libraries, while JavaScript is better in front-end development libraries and frameworks. 3) Both have rich learning resources, but Python is suitable for starting with official documents, while JavaScript is better with MDNWebDocs. The choice should be based on project needs and personal interests.

      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

      Video Face Swap

      Video Face Swap

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

      Hot Tools

      Dreamweaver CS6

      Dreamweaver CS6

      Visual web development tools

      SAP NetWeaver Server Adapter for Eclipse

      SAP NetWeaver Server Adapter for Eclipse

      Integrate Eclipse with SAP NetWeaver application server.

      MantisBT

      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.

      Zend Studio 13.0.1

      Zend Studio 13.0.1

      Powerful PHP integrated development environment

      PhpStorm Mac version

      PhpStorm Mac version

      The latest (2018.2.1) professional PHP integrated development tool