在AngularJS中使用jQuery的zTree插件的方法
前段時間一直在看AngularJS的資料,感覺是個很好的框架,很想有機(jī)會嘗試用它做點(diǎn)什么。
jQuery ZTree是國內(nèi)非常不錯的JQuery插件,功能齊全,文檔和API也非常的友好,之前項(xiàng)目上常用此插件。
AngularJS 功能雖然非常強(qiáng)大,但UI上提供的插件不像JQuery那么多,而且只能通過directive定義擴(kuò)展的UI插件,雖然國外已經(jīng)提供了一些基于 directive的Tree功能實(shí)現(xiàn),但畢竟不像ZTree那樣強(qiáng)大,而且Tree是做項(xiàng)目中很長用的一個基本功能。
因此,花了一點(diǎn)時間做了一個例子將ZTree應(yīng)用到AngularJS中。
zTree和后臺數(shù)據(jù)的交互
首先,肯定是在頁面中引入Angularjs的相關(guān)腳本,例如模塊(e.g. app.js)、控制器(e.g. controller.js)、Angularjs的腳本并進(jìn)行相關(guān)標(biāo)記的使用,然后引入zTree的樣式包和zTreed 腳本,可以參看一下代碼:
<!DOCTYPE html> <html lang="zh-CN" ng-app="app"> <head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1"> <title>樹型菜單</title> <link href="plugins/bootstrap-3.3.5/css/bootstrap.min.css" rel="stylesheet"> <link href="css/zTreeStyle.css" rel="stylesheet"> </head> <% include ./../include/header.html %> <% include ./../include/top-menu.html %> <div id="content" class="content clearfix" ng-controller="wtConfigInfo"> <ul tree id="tree" style="font:normal 12px/35px 'Arial';color:#dcdcdc;" class="ztree" ng-model="selectNode" value="1" > </div> <% include ./../include/footer.html %> <script src="plugins/jquery-1.11.3.min.js" type="text/javascript"></script> <script src="plugins/bootstrap-3.3.5/js/bootstrap.min.js" type="text/javascript"></script> <script src="..//js/angular.min.js" type="text/javascript"></script> <script src="..//js/angular/app.js" type="text/javascript"></script> <script src="..//js/angular/controllers.js" type="text/javascript"></script> <script src="../plugins/zTree/jquery.ztree.all-3.5.js" type="text/javascript"></script> </body> </html>
在上面的在ul標(biāo)簽中添加了指令tree,這樣在加載angularjs中,就可通過指令 tree來進(jìn)行菜單數(shù)據(jù)的獲取。具體的代碼可參考以下代碼:
var app = angular.module('app', []); //樹形結(jié)構(gòu) app.directive('tree',function(){ return{ require:'?ngModel', restrict:'A', link:function($scope,element,attrs,ngModel){ var setting = { data :{ simpleData:{ enable:true } }, callback:{ beforeClick:function(treeId, treeNode) {//點(diǎn)擊菜單時進(jìn)行的處理 var zTree = $.fn.zTree.getZTreeObj("tree"); if (treeNode.isParent) { zTree.expandNode(treeNode); return false; } else { window.location.href=treeNode.url; return true; } } } }; //向控制器發(fā)送消息,進(jìn)行菜單數(shù)據(jù)的獲取 $scope.$emit("menu",attrs["value"]);//此處attrs["value"]為ul中的value值,此處作為標(biāo)記使用 //接受控制器返回的菜單的消息 $scope.$on("menuData",function(event,data){ $.fn.zTree.init(element, setting, data);//進(jìn)行初始化樹形菜單 var zTree = $.fn.zTree.getZTreeObj("tree"); var selectName = $("#selectName").val(); if(typeof selectName == "undefined" || selectName == ""){ zTree.selectNode(zTree.getNodeByParam("id","1"));//默認(rèn)第一個選中 $("#selectName").val(zTree.getSelectedNodes()[0].code);//賦值 }else{ for(var i =0; i<data.length;i++){ if(data[i]["code"] == selectName ){ zTree.selectNode(zTree.getNodeByParam("code", data[i]["code"])); } } } }); } } });
在上述代碼中,使用$scope.$emit("menu",attrs["value"]);向父控制器發(fā)送請求數(shù)據(jù),在控制器代碼中可以接受此消息,并使用$http向后臺進(jìn)行數(shù)據(jù)的請求,并將從數(shù)據(jù)庫中請求來的數(shù)據(jù)發(fā)送個子控制器??刂破鞯拇a可參考如下:
function wtConfigInfo($scope,$http){ //接受子控制器發(fā)送的消息 $scope.$on("menu",function(event,params){ $http.get("/commonfuncode").success(function(data){ //發(fā)送消息給子控制器 $scope.$broadcast("menuData",dealMenuData(data,params)); }); }); }
這樣,就完成了zTree和后臺數(shù)據(jù)的交互。
利用指令集成ZTree的實(shí)例
<!doctype html> <html lang="en" ng-app="app"> <head> <meta charset="utf-8"> <title>ZTree</title> <link rel="stylesheet" href="css/app.css"> <link rel="stylesheet" href="css/bootstrap.css"> <link rel="stylesheet" href="css/animations.css"> <link rel="stylesheet" href="css/zTreeStyle/zTreeStyle.css"> <script src="lib/jquery-1.10.2.min.js"></script> <script src="lib/jquery.ztree.all-3.5.js"></script> <script src="lib/angular.min.js"></script> <script src="app.js"></script> </head> <body> <body ng-controller='MyController'> <ul tree class="ztree" ng-model="selectNode"></ul> </body> <pre> {{selectNode | json}} </pre> </body> </html>
app.js
'use strict'; /* App Module */ var appModule = angular.module('app', []); appModule.directive('tree', function () { return { require: '?ngModel', restrict: 'A', link: function ($scope, element, attrs, ngModel) { //var opts = angular.extend({}, $scope.$eval(attrs.nlUploadify)); var setting = { data: { key: { title: "t" }, simpleData: { enable: true } }, callback: { onClick: function (event, treeId, treeNode, clickFlag) { $scope.$apply(function () { ngModel.$setViewValue(treeNode); }); } } }; var zNodes = [ { id: 1, pId: 0, name: "普通的父節(jié)點(diǎn)", t: "我很普通,隨便點(diǎn)我吧", open: true }, { id: 11, pId: 1, name: "葉子節(jié)點(diǎn) - 1", t: "我很普通,隨便點(diǎn)我吧" }, { id: 12, pId: 1, name: "葉子節(jié)點(diǎn) - 2", t: "我很普通,隨便點(diǎn)我吧" }, { id: 13, pId: 1, name: "葉子節(jié)點(diǎn) - 3", t: "我很普通,隨便點(diǎn)我吧" }, { id: 2, pId: 0, name: "NB的父節(jié)點(diǎn)", t: "點(diǎn)我可以,但是不能點(diǎn)我的子節(jié)點(diǎn),有本事點(diǎn)一個你試試看?", open: true }, { id: 21, pId: 2, name: "葉子節(jié)點(diǎn)2 - 1", t: "你哪個單位的?敢隨便點(diǎn)我?小心點(diǎn)兒..", click: false }, { id: 22, pId: 2, name: "葉子節(jié)點(diǎn)2 - 2", t: "我有老爸罩著呢,點(diǎn)擊我的小心點(diǎn)兒..", click: false }, { id: 23, pId: 2, name: "葉子節(jié)點(diǎn)2 - 3", t: "好歹我也是個領(lǐng)導(dǎo),別普通群眾就來點(diǎn)擊我..", click: false }, { id: 3, pId: 0, name: "郁悶的父節(jié)點(diǎn)", t: "別點(diǎn)我,我好害怕...我的子節(jié)點(diǎn)隨便點(diǎn)吧...", open: true, click: false }, { id: 31, pId: 3, name: "葉子節(jié)點(diǎn)3 - 1", t: "唉,隨便點(diǎn)我吧" }, { id: 32, pId: 3, name: "葉子節(jié)點(diǎn)3 - 2", t: "唉,隨便點(diǎn)我吧" }, { id: 33, pId: 3, name: "葉子節(jié)點(diǎn)3 - 3", t: "唉,隨便點(diǎn)我吧" } ]; $.fn.zTree.init(element, setting, zNodes); } }; }); appModule.controller('MyController', function ($scope) { });
實(shí)現(xiàn)功能:定義tree這個屬性,使<ul tree class="ztree" ng-model="selectNode"></ul>自動變成一個有數(shù)據(jù)的tree,點(diǎn)擊樹節(jié)點(diǎn),自動變更model 的selectNode。
- Jquery樹插件zTree用法入門教程
- jQuery zTree樹插件簡單使用教程
- jQuery插件zTree實(shí)現(xiàn)的多選樹效果示例
- jQuery使用zTree插件實(shí)現(xiàn)樹形菜單和異步加載
- zTree jQuery 樹插件的使用(實(shí)例講解)
- jQuery插件zTree實(shí)現(xiàn)獲取當(dāng)前選中節(jié)點(diǎn)在同級節(jié)點(diǎn)中序號的方法
- 無限樹Jquery插件zTree的常用功能特性總結(jié)
- 多功能jQuery樹插件zTree實(shí)現(xiàn)權(quán)限列表簡單實(shí)例
- jQuery樹插件zTree使用方法詳解
- jQuery zTree插件使用簡單教程
相關(guān)文章
cnpm加速Angular項(xiàng)目創(chuàng)建的方法
這篇文章主要介紹了cnpm加速Angular項(xiàng)目創(chuàng)建的方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-09-09angularjs 的數(shù)據(jù)綁定實(shí)現(xiàn)原理
本篇文章主要介紹了angularjs 的數(shù)據(jù)綁定實(shí)現(xiàn)原理,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-07-07angularJS與bootstrap結(jié)合實(shí)現(xiàn)動態(tài)加載彈出提示內(nèi)容
這篇文章主要介紹了angularJS與bootstrap結(jié)合實(shí)現(xiàn)動態(tài)加載彈出提示內(nèi)容,通過bootstrp彈出提示。感興趣的朋友可以參考下本篇文章2015-10-10Angular2-primeNG文件上傳模塊FileUpload使用詳解
這篇文章主要為大家詳細(xì)介紹了Angular2-primeNG文件上傳模塊FileUpload使用,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-01-01AngularJS實(shí)現(xiàn)自定義指令及指令配置項(xiàng)的方法
這篇文章主要介紹了AngularJS實(shí)現(xiàn)自定義指令及指令配置項(xiàng)的方法,結(jié)合實(shí)例形式簡單總結(jié)分析了AngularJS自定義指令及指令配置項(xiàng)的實(shí)現(xiàn)技巧,需要的朋友可以參考下2017-11-11Angular.js實(shí)現(xiàn)多個checkbox只能選擇一個的方法示例
這篇文章主要給大家介紹了利用Angular.js實(shí)現(xiàn)多個checkbox只能選擇一個的方法,文中給出了詳細(xì)的示例代碼,相信對大家具有一定的參考價值,下面來一起看看吧。2017-02-02Angular將填入表單的數(shù)據(jù)渲染到表格的方法
這篇文章主要介紹了Angular將填入表單的數(shù)據(jù)渲染到表格的方法,非常具有實(shí)用價值,需要的朋友可以參考下2017-09-09