Home > Article > Web Front-end > How to use jQuery’s zTree plug-in in AngularJS_AngularJS
I have been reading the information about AngularJS some time ago, and I think it is a very good framework. I would like to have the opportunity to try to do something with it.
jQuery ZTree is a very good JQuery plug-in in China. It has complete functions, and the documentation and API are also very friendly. This plug-in has been commonly used in previous projects.
Although AngularJS is very powerful, the UI does not provide as many plug-ins as JQuery, and extended UI plug-ins can only be defined through directives. Although some directive-based Tree function implementations have been provided abroad, they are not as powerful as ZTree after all. And Tree is a basic function that is often used in projects.
Therefore, I spent a little time making an example to apply ZTree to AngularJS.
Interaction between zTree and background data
First of all, you must introduce Angularjs related scripts into the page, such as modules (e.g. app.js), controllers (e.g. controller.js), Angularjs scripts and use related tags, and then introduce zTree style packages and zTreed script, you can refer to the code:
<!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>
In the above, the instruction tree is added to the ul tag, so that when loading angularjs, the menu data can be obtained through the instruction tree. For specific codes, please refer to the following codes:
var app = angular.module('app', []); //树形结构 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) {//点击菜单时进行的处理 var zTree = $.fn.zTree.getZTreeObj("tree"); if (treeNode.isParent) { zTree.expandNode(treeNode); return false; } else { window.location.href=treeNode.url; return true; } } } }; //向控制器发送消息,进行菜单数据的获取 $scope.$emit("menu",attrs["value"]);//此处attrs["value"]为ul中的value值,此处作为标记使用 //接受控制器返回的菜单的消息 $scope.$on("menuData",function(event,data){ $.fn.zTree.init(element, setting, data);//进行初始化树形菜单 var zTree = $.fn.zTree.getZTreeObj("tree"); var selectName = $("#selectName").val(); if(typeof selectName == "undefined" || selectName == ""){ zTree.selectNode(zTree.getNodeByParam("id","1"));//默认第一个选中 $("#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"])); } } } }); } } });
In the above code, use $scope.$emit("menu",attrs["value"]); to send request data to the parent controller. You can accept this message in the controller code and use $http to The background performs data requests and sends the data requested from the database to the sub-controller. The code of the controller can be referenced as follows:
function wtConfigInfo($scope,$http){ //接受子控制器发送的消息 $scope.$on("menu",function(event,params){ $http.get("/commonfuncode").success(function(data){ //发送消息给子控制器 $scope.$broadcast("menuData",dealMenuData(data,params)); }); }); }
In this way, the interaction between zTree and background data is completed.
Use instructions to integrate ZTree examples
<!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 class="brush:php;toolbar:false"> {{selectNode | json}}