最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

詳解Angular結(jié)合zTree異步加載節(jié)點(diǎn)數(shù)據(jù)

 更新時(shí)間:2018年01月20日 10:55:02   作者:NeverCtrl_C  
本篇文章主要給大家分享了Angular結(jié)合zTree異步加載節(jié)點(diǎn)數(shù)據(jù)的難點(diǎn)以及方法,有這方面需求的朋友參考下吧。

1 前提準(zhǔn)備

1.1 新建一個(gè)angular4項(xiàng)目

參考:http://www.fzitv.net/article/119668.htm

1.2 去zTree官網(wǎng)下載zTree

zTree官網(wǎng):點(diǎn)擊前往

三少使用的版本:點(diǎn)擊前往

1.3 參考博客

http://www.fzitv.net/article/133284.htm

2 編程步驟

  

從打印出zTree對(duì)象可以看出,zTree對(duì)象利用init方法來(lái)實(shí)現(xiàn)zTree結(jié)構(gòu);init方法接收三個(gè)參數(shù)

參數(shù)1:一個(gè)ul標(biāo)簽的DOM節(jié)點(diǎn)對(duì)象

參數(shù)2:基本配置對(duì)象

參數(shù)3:標(biāo)題信息數(shù)組

2.1 在index.html中引入相關(guān)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對(duì)象

declare var $ : any;

2.3 在TS文件中編寫(xiě)代碼

    

 
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 一級(jí)標(biāo)題', 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 二級(jí)標(biāo)題', open: true, font:{'background-color':'skyblue', 'color':'white'}},
 // {id: 111, pId: 11, name: '1.1.1 三級(jí)標(biāo)題 -> 博客園', url: 'http://www.cnblogs.com/NeverCtrl-C/'},
 // {id: 112, pId: 11, name: '1.1.2 三級(jí)標(biāo)題 -> 單擊', click: "alert('你單擊了')"},
 // {id: 12, pId: 1, name: '1.2 二級(jí)標(biāo)題'},
 // {id: 2, pId: 0, name: '2 一級(jí)標(biāo)題'}
 // ]
 // 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 一級(jí)標(biāo)題'},
 {id: 11, pId: 1, name: '1.1 二級(jí)標(biāo)題'},
 {id: 111, pId: 11, name: '1.1.1 三級(jí)標(biāo)題'},
 {id: 112, pId: 11, name: '1.1.2 三級(jí)標(biāo)題'},
 {id: 12, pId: 1, name: '1.2 二級(jí)標(biāo)題'},
 {id: 2, pId: 0, name: '2 一級(jí)標(biāo)題'}
 ];
 constructor() { }
 ngOnInit() { 
 console.log($);
 console.log($.fn.zTree);
 $.fn.zTree.init($("#ztree"),this.setting,this.zNodes);
 }
}
View Code

2.4 在組件HTML中編寫(xiě)代碼

<ul class="ztree"><ul></ul>

2.5 效果展示

3 zTree基本功能

3.1 不顯示連接線

3.1.1 官方文檔

不顯示標(biāo)題之間的連接線

3.1.2 編程步驟

在基本配置對(duì)象中指定showLine屬性的值為false即可

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

3.2 不顯示節(jié)點(diǎn)圖標(biāo)

3.2.1 官方文檔

去掉節(jié)點(diǎn)前面的圖標(biāo)

3.2.2 編程步驟

將基本配置對(duì)象的showIcon屬性設(shè)為false即可

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

3.3 自定義節(jié)點(diǎn)圖標(biāo)

3.3.1 官方文檔

更改節(jié)點(diǎn)的圖標(biāo)

3.3.2 編程步驟

為treeNode節(jié)點(diǎn)數(shù)據(jù)設(shè)置icon/iconOpen/iconClose屬性即可

3.4 自定義字體

3.4.1 官方文檔

更改節(jié)點(diǎn)字體的樣式

3.4.2 編程步驟

為treeNode節(jié)點(diǎn)數(shù)據(jù)設(shè)置font屬性即可,font屬性的值是一個(gè)對(duì)象,該對(duì)象的內(nèi)容和style的數(shù)據(jù)一樣

3.4.3 效果展示

3.5 超鏈接

3.5.1 官方文檔

點(diǎn)擊節(jié)點(diǎn)標(biāo)題就會(huì)自動(dòng)跳轉(zhuǎn)到對(duì)應(yīng)的url

注意01:click屬性只能進(jìn)行最簡(jiǎn)單的 click 事件操作。相當(dāng)于 的內(nèi)容。 如果操作較復(fù)雜,請(qǐng)使用 onClick 事件回調(diào)函數(shù)。

3.5.2 編程步驟

為treeNode節(jié)點(diǎn)數(shù)據(jù)設(shè)置url、click屬性即可

技巧01:設(shè)置click屬性時(shí),屬性值必須是一些簡(jiǎn)單的onClick事件

技巧02:設(shè)置target屬性時(shí),屬性值有 _blank 和 _self

_blank -> 用一個(gè)新窗口打開(kāi)

_self -> 在原來(lái)的窗口打開(kāi)

 zNodes = [
 {id: 1, pId: 0, name: '1 一級(jí)標(biāo)題', 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 二級(jí)標(biāo)題', open: true, font:{'background-color':'skyblue', 'color':'white'}},
 {id: 111, pId: 11, name: '1.1.1 三級(jí)標(biāo)題 -> 博客園1', url: 'http://www.cnblogs.com/NeverCtrl-C/', target: '_blank'},
 {id: 113, pId: 11, name: '1.1.1 三級(jí)標(biāo)題 -> 博客園2', url: 'http://www.cnblogs.com/NeverCtrl-C/', target: '_self'},
 {id: 112, pId: 11, name: '1.1.2 三級(jí)標(biāo)題 -> 單擊', click: "alert('你單擊了')"},
 {id: 12, pId: 1, name: '1.2 二級(jí)標(biāo)題'},
 {id: 2, pId: 0, name: '2 一級(jí)標(biāo)題'}
 ]
View Code

3.6 單擊控制

3.6.1 官方文檔

點(diǎn)擊節(jié)點(diǎn)標(biāo)題時(shí)觸發(fā)相應(yīng)的方法

技巧01:在angular中可以利用這個(gè)用法來(lái)實(shí)現(xiàn)路由跳轉(zhuǎn)

3.6.2 編程步驟

設(shè)置基本配置對(duì)象的onClick屬性

技巧01:onClick屬性值是一個(gè)方法的引用,我們需要自己編寫(xiě)這個(gè)方法

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

編寫(xiě)onClick觸發(fā)方法

 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 一級(jí)標(biāo)題', 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 二級(jí)標(biāo)題', open: true, font:{'background-color':'skyblue', 'color':'white'}},
 {id: 111, pId: 11, name: '1.1.1 三級(jí)標(biāo)題 -> 博客園1', url: 'http://www.cnblogs.com/NeverCtrl-C/', target: '_blank'},
 {id: 113, pId: 11, name: '1.1.1 三級(jí)標(biāo)題 -> 博客園2', url: 'http://www.cnblogs.com/NeverCtrl-C/', target: '_self'},
 {id: 112, pId: 11, name: '1.1.2 三級(jí)標(biāo)題 -> 單擊', click: "alert('你單擊了')"},
 {id: 12, pId: 1, name: '1.2 二級(jí)標(biāo)題'},
 {id: 2, pId: 0, name: '2 一級(jí)標(biāo)題'}
 ]
 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 += "動(dòng)態(tài)節(jié)點(diǎn)數(shù)據(jù)" + responseData[i].id;
 // }
 // }
 // return responseData;
 // }
 onCzTreeOnClick(event, treeId, treeNode, clickFlag) {
 alert(treeNode.name);
 } 
 constructor() { } 
 ngOnInit() { 
 console.log('打印輸出jquery對(duì)象');
 console.log($);
 console.log('但因輸出zTree對(duì)象');
 console.log($.fn.zTree);
 $.fn.zTree.init($("#ztree"),this.setting,this.zNodes);
 // $.fn.zTree.init($("#ztree"),this.setting);
 }
}
View Code

3.7 異步加載節(jié)點(diǎn)數(shù)據(jù)

3.7.1 官方文檔

節(jié)點(diǎn)的數(shù)據(jù)是從后臺(tái)進(jìn)行獲取的

3.7.2 編程步驟

技巧01:異步加載節(jié)點(diǎn)數(shù)據(jù)時(shí)init方法不用傳遞第三個(gè)參數(shù)

> 準(zhǔn)備一個(gè)后臺(tái)用于返回JSON格式的數(shù)據(jù)

技巧01:返回的JSON數(shù)據(jù)是一個(gè)列表,格式為

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

技巧02:三少偷懶,是利用json-server模擬的后臺(tái)數(shù)據(jù),哈哈;json-server

> 設(shè)置基本配置對(duì)象的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

> 編寫(xiě)響應(yīng)數(shù)據(jù)處理方法

 filter(treeId, parentNode,responseData) {
 console.log(responseData);
 if (responseData) {
 for(var i =0; i < responseData.length; i++) {
 responseData[i].name += "動(dòng)態(tài)節(jié)點(diǎn)數(shù)據(jù)" + 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"
 }
 ]
}
模擬后臺(tái)響應(yīng)數(shù)據(jù)
<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 一級(jí)標(biāo)題', 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 二級(jí)標(biāo)題', open: true, font:{'background-color':'skyblue', 'color':'white'}},
 // {id: 111, pId: 11, name: '1.1.1 三級(jí)標(biāo)題 -> 博客園1', url: 'http://www.cnblogs.com/NeverCtrl-C/', target: '_blank'},
 // {id: 113, pId: 11, name: '1.1.1 三級(jí)標(biāo)題 -> 博客園2', url: 'http://www.cnblogs.com/NeverCtrl-C/', target: '_self'},
 // {id: 112, pId: 11, name: '1.1.2 三級(jí)標(biāo)題 -> 單擊', click: "alert('你單擊了')"},
 // {id: 12, pId: 1, name: '1.2 二級(jí)標(biāo)題'},
 // {id: 2, pId: 0, name: '2 一級(jí)標(biāo)題'}
 // ]

 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 += "動(dòng)態(tài)節(jié)點(diǎn)數(shù)據(jù)" + responseData[i].id;
 }
 }
 return responseData;
 }

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

 constructor() { }
 
 ngOnInit() { 
 console.log('打印輸出jquery對(duì)象');
 console.log($);
 console.log('但因輸出zTree對(duì)象');
 console.log($.fn.zTree);
 // $.fn.zTree.init($("#ztree"),this.setting,this.zNodes);
 $.fn.zTree.init($("#ztree"),this.setting);
 }
}
TS

3.7.4 效果展示

相關(guān)文章

  • Angular2使用Guard和Resolve進(jìn)行驗(yàn)證和權(quán)限控制

    Angular2使用Guard和Resolve進(jìn)行驗(yàn)證和權(quán)限控制

    本篇文章主要介紹了Angular2使用Guard和Resolve進(jìn)行驗(yàn)證和權(quán)限控制,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2017-04-04
  • 基于Angularjs+mybatis實(shí)現(xiàn)二級(jí)評(píng)論系統(tǒng)(仿簡(jiǎn)書(shū))

    基于Angularjs+mybatis實(shí)現(xiàn)二級(jí)評(píng)論系統(tǒng)(仿簡(jiǎn)書(shū))

    這篇文章主要為大家詳細(xì)介紹了基于Angularjs+mybatis實(shí)現(xiàn)二級(jí)評(píng)論系統(tǒng),模仿簡(jiǎn)書(shū)效果制作,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-02-02
  • angularjs過(guò)濾器--filter與ng-repeat配合有奇效

    angularjs過(guò)濾器--filter與ng-repeat配合有奇效

    本篇文章主要介紹了angularjs過(guò)濾器-filter與ng-repeat的相關(guān)知識(shí)。具有很好的參考價(jià)值。下面跟著小編一起來(lái)看下吧
    2017-04-04
  • 理解Angular數(shù)據(jù)雙向綁定

    理解Angular數(shù)據(jù)雙向綁定

    AngularJS誕生于2009年,由Misko Hevery 等人創(chuàng)建,后為Google所收購(gòu)。這篇文章主要帶著大家理解Angular數(shù)據(jù)雙向綁定,感興趣的小伙伴們可以參考一下
    2016-01-01
  • AngularJS使用指令增強(qiáng)標(biāo)準(zhǔn)表單元素功能

    AngularJS使用指令增強(qiáng)標(biāo)準(zhǔn)表單元素功能

    這篇文章主要介紹了AngularJS使用指令增強(qiáng)標(biāo)準(zhǔn)表單元素功能,包括數(shù)據(jù)綁定、建立模型屬性、驗(yàn)證表單等,感興趣的小伙伴們可以參考一下
    2016-07-07
  • Angular中響應(yīng)式表單的三種更新值方法詳析

    Angular中響應(yīng)式表單的三種更新值方法詳析

    這篇文章主要給大家詳細(xì)解析了關(guān)于Angular中響應(yīng)式表單的三種更新值方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面跟著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧。
    2017-08-08
  • 關(guān)于angular表單動(dòng)態(tài)驗(yàn)證的一種新思路分享

    關(guān)于angular表單動(dòng)態(tài)驗(yàn)證的一種新思路分享

    在Angular?中不管是模板驅(qū)動(dòng)表單還是響應(yīng)式表單,對(duì)于動(dòng)態(tài)創(chuàng)建表單的支持都很好,下面這篇文章主要給大家介紹了關(guān)于angular表單動(dòng)態(tài)驗(yàn)證的一種新思路,需要的朋友可以參考下
    2022-03-03
  • AngularJS 中的事件詳解

    AngularJS 中的事件詳解

    本文主要介紹AngularJS 事件,這里整理了相關(guān)資料,比較詳細(xì)的介紹了AngularJS的使用方法,有需要的小伙伴參考下
    2016-07-07
  • AngularJs 指令詳解及示例代碼

    AngularJs 指令詳解及示例代碼

    本文主要介紹AngularJs 指令的知識(shí),這里整理了詳細(xì)的資料和簡(jiǎn)單示例代碼幫助大家學(xué)習(xí)理解應(yīng)用,有興趣的小伙伴可以參考下
    2016-09-09
  • AngularJS 多指令Scope問(wèn)題的解決

    AngularJS 多指令Scope問(wèn)題的解決

    本文介紹了AngularJS 多指令Scope問(wèn)題的解決,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2018-10-10

最新評(píng)論

大关县| 新竹市| 岗巴县| 平和县| 金塔县| 浠水县| 太白县| 万全县| 三原县| 如皋市| 义马市| 英德市| 平原县| 阳西县| 龙南县| 阜新| 剑阁县| 彝良县| 五河县| 敦煌市| 安福县| 富川| 西丰县| 甘泉县| 兴国县| 兰州市| 那曲县| 江阴市| 长治县| 来凤县| 九江县| 农安县| 五峰| 庆元县| 镇雄县| 克什克腾旗| 新巴尔虎左旗| 龙陵县| 海城市| 莱阳市| 喀喇沁旗|