이 글은 주로 zTree와 결합된 Angular를 사용하여 노드 데이터를 비동기적으로 로드하는 방법과 어려움을 공유합니다. 이와 관련하여 필요한 친구들이 참고할 수 있기를 바랍니다.
1 전제 조건
1.1 새 Angle4 프로젝트 만들기
1.2 zTree 공식 웹사이트로 이동하여 zTree
zTree 공식 웹사이트로 이동: 클릭하여
Sanshao에서 사용하는 버전: 클릭하여
로 이동2 프로그래밍 단계
zTree 객체를 인쇄해 보면 알 수 있듯이 zTree 객체는 init 메소드를 사용하여 zTree 구조를 구현합니다. init 메소드는 세 개의 매개변수를 받습니다.
매개변수 1: DOM 노드 Ul Tag
Parameter 2 : 기본 구성 객체
Parameter 3 : 제목 정보 배열
2.1 인덱스에 관련 JS 및 CSS를 소개합니다. html
<!doctype html> <html lang="en"> <head> <meta charset="utf-8"> <title>TestZtree</title> <base href="/" rel="external nofollow" > <meta name="viewport" content="width=device-width, initial-scale=1"> <link rel="icon" type="image/x-icon" href="favicon.ico" rel="external nofollow" > <link rel="stylesheet" type="text/css" href="./assets/zTree/css/zTreeStyle/zTreeStyle.css" rel="external nofollow" > <link rel="stylesheet" type="text/css" href="./assets/zTree/css/demo.css" rel="external nofollow" > <script src="./assets/zTree/js/jquery-1.4.4.min.js"></script> <script src="./assets/zTree/js/jquery.ztree.core.js"></script> </head> <body> <app-root></app-root> </body> </html>
view code
2.2 TS 파일에 jquery 객체 선언
declare var $ : any;
2.3 TS 파일에 코드 작성
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); } }
코드 보기
2.4 HTML에 코드 작성
<ul class="ztree"><ul></ul>
2.5 효과 표시
3 zTree 기본 기능
3.1 연결선을 표시하지 않음
3.1.1 공식 문서
제목 사이에 연결선을 표시하지 않음
3.1.2 프로그래밍 단계
showLine 값만 지정하기 기본 구성 객체의 속성을 false로
setting = { data: { simpleData: { enable: true } }, view: { showLine: false } };
3.2 노드 아이콘을 표시하지 않음
3.2.1 공식 문서
노드 앞의 아이콘을 제거
3.2.2 프로그래밍 단계
기본 구성 객체의 showicon 속성을 false
setting = { data: { simpleData: { enable: true } }, view: { showLine: false, showIcon: false } };
view code
3.3에 설정하십시오.
3.3.2 프로그래밍 단계 아이콘 설정/ treeNode 노드 데이터 iconOpen /iconClose 속성을 사용할 수 있습니다3.4.2 프로그래밍 단계treeNode 노드 데이터에 대한 글꼴 속성을 설정합니다. 글꼴 속성의 값은 개체이며 개체의 내용은 스타일의 데이터와 동일합니다
3.5 하이퍼링크
참고 01: 클릭 속성은 가장 간단한 클릭 이벤트 작업만 수행할 수 있습니다. 에 해당하는 콘텐츠입니다. 작업이 더 복잡하다면 onClick 이벤트 콜백 함수를 사용하세요.
팁 02: target 속성 속성 값이 _blank 및 _self
_blank인 경우 -> 새 창에서 열기
_self -> 원래 창에서 열기
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 一级标题'} ]
코드 보기
3.6.1 공식 문서
노드 제목을 클릭하면 해당 메소드가 트리거됩니다
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 < responseData.length; i++) { // responseData[i].name += "动态节点数据" + responseData[i].id; // } // } // return responseData; // } onCzTreeOnClick(event, treeId, treeNode, clickFlag) { alert(treeNode.name); } constructor() { } ngOnInit() { console.log('打印输出jquery对象'); console.log($); console.log('但因输出zTree对象'); console.log($.fn.zTree); $.fn.zTree.init($("#ztree"),this.setting,this.zNodes); // $.fn.zTree.init($("#ztree"),this.setting); } }
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 < responseData.length; i++) { responseData[i].name += "动态节点数据" + responseData[i].id; } } return responseData; }
View Code
3.7.3 代码总汇
{ "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" } ] }
模拟后台响应数据
<ul class="ztree"><ul></ul>
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 < responseData.length; i++) { responseData[i].name += "动态节点数据" + responseData[i].id; } } return responseData; } onCzTreeOnClick(event, treeId, treeNode, clickFlag) { alert(treeNode.name); } constructor() { } ngOnInit() { console.log('打印输出jquery对象'); console.log($); console.log('但因输出zTree对象'); console.log($.fn.zTree); // $.fn.zTree.init($("#ztree"),this.setting,this.zNodes); $.fn.zTree.init($("#ztree"),this.setting); } }
TS
3.7.4 效果展示
相关推荐:
위 내용은 Angular는 zTree와 결합하여 노드 데이터 인스턴스 공유를 비동기식으로 로드합니다.의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!