溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

Angular整合zTree的示例代碼

發(fā)布時(shí)間:2020-10-17 04:17:46 來源:腳本之家 閱讀:162 作者:NeverCtrl_C 欄目:web開發(fā)

1 前提準(zhǔn)備

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

參考博文:點(diǎn)擊前往

Angular整合zTree的示例代碼

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

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

Angular整合zTree的示例代碼

2 編程步驟

Angular整合zTree的示例代碼

從打印出zTree對(duì)象可以看出,zTree對(duì)象利用init方法來實(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

Angular整合zTree的示例代碼

<!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>

2.2 在TS文件中聲明jquery對(duì)象

declare var $ : any;

2.3 在TS文件中編寫代碼

Angular整合zTree的示例代碼

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);
 }
}

2.4 在組件HTML中編寫代碼

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

2.5 效果展示

Angular整合zTree的示例代碼

3 zTree基本功能

3.1 不顯示連接線

3.1.1 官方文檔

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

Angular整合zTree的示例代碼

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)

Angular整合zTree的示例代碼

3.2.2 編程步驟

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

Angular整合zTree的示例代碼

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

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

3.3.1 官方文檔

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

Angular整合zTree的示例代碼

3.3.2 編程步驟

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

Angular整合zTree的示例代碼

3.4 自定義字體

3.4.1 官方文檔

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

Angular整合zTree的示例代碼

3.4.2 編程步驟

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

Angular整合zTree的示例代碼

3.4.3 效果展示

Angular整合zTree的示例代碼

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)于 onclick="..." 的內(nèi)容。 如果操作較復(fù)雜,請(qǐng)使用 onClick 事件回調(diào)函數(shù)。

Angular整合zTree的示例代碼

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è)新窗口打開

_self -> 在原來的窗口打開

Angular整合zTree的示例代碼

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)題'}
 ]

3.6 單擊控制

3.6.1 官方文檔

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

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

Angular整合zTree的示例代碼

3.6.2 編程步驟

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

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

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

Angular整合zTree的示例代碼

編寫onClick觸發(fā)方法

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

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持億速云。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI