当前位置: 移动技术网 > IT编程>脚本编程>AngularJs > 详解Angular结合zTree异步加载节点数据

详解Angular结合zTree异步加载节点数据

2018年01月25日  | 移动技术网IT编程  | 我要评论

1 前提准备

1.1 新建一个angular4项目

参考:

1.2 去ztree官网下载ztree

ztree官网:

三少使用的版本:点击前往

1.3 参考博客

2 编程步骤

  

从打印出ztree对象可以看出,ztree对象利用init方法来实现ztree结构;init方法接收三个参数

参数1:一个ul标签的dom节点对象

参数2:基本配置对象

参数3:标题信息数组

2.1 在中引入相关js、css

<!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);
 }
}
view code

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.1 官方文档

更改节点的图标

3.3.2 编程步骤

为treenode节点数据设置icon/iconopen/iconclose属性即可

3.4 自定义字体

3.4.1 官方文档

更改节点字体的样式

3.4.2 编程步骤

为treenode节点数据设置font属性即可,font属性的值是一个对象,该对象的内容和style的数据一样

3.4.3 效果展示

3.5 超链接

3.5.1 官方文档

点击节点标题就会自动跳转到对应的url

注意01:click属性只能进行最简单的 click 事件操作。相当于 的内容。 如果操作较复杂,请使用 onclick 事件回调函数。

3.5.2 编程步骤

为treenode节点数据设置url、click属性即可

技巧01:设置click属性时,属性值必须是一些简单的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 一级标题'}
 ]
view code

3.6 单击控制

3.6.1 官方文档

点击节点标题时触发相应的方法

技巧01:在angular中可以利用这个用法来实现路由跳转

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 效果展示

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网