Angularjs的directive封装ztree

[size=large]一般我们做web开发都会用到树,恰好ztree为我们提供了多种风格的树插件。[/size]

[size=large]接下来就看看怎么用Angularjs的directive封装ztree[/size]
<!DOCTYPE html>
<html ng-app="ceshiapp" ng-controller="ceshicontroller">
<head>
<title>liuxu.html</title>
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="this is my page">
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<link rel="stylesheet" href="../cstorage/plugins/zTreeStyle.css"
type="text/css"></link>
<link rel="stylesheet"href="../standard/plugins/bootstrap/css/bootstrap.css" type="text/css"></link>
</head>
<body>

<zcheckboxtree id="tree" async="true" binding="/unit/user"datatype="json" text="Name" kind="get" ng-model="checked"
ng-click="auth_treenode_onclick(checked)"></zcheckboxtree>
<div>
<h1>已选择</h1>
[list]
<li ng-repeat="item in user track by $index">{{item.Name}}</li>
[/list]
</div>
</body>
<script type="text/javascript"
src="../standard/plugins/jquery/jquery.min.js"></script>
<script type="text/javascript" src="../cstorage/plugins/ztree/js/jquery.ztree.core.min.js"></script>
<script type="text/javascript"
src="../standard/plugins/ztree/js/jquery.ztree.all.js"></script>
<script type="text/javascript"
src="../standard/plugins/angular/angular.js"></script>
<script type="text/javascript">
var ceshiapp = angular.module("ceshiapp", []);
//动态加载模板的指令
ceshiapp.directive('zcheckboxtree',function(){
var option = {
restrict : 'E',
require: '?ngModel',
replace : true,
transclude: true,
template : "<ul class='ztree' ></ul> ",
scope:true,
link : function($scope, $element, $attrs, ngModel) {
$scope.config={};
$scope.config.id = $attrs.id; // 控件id
$scope.config.async = $attrs.async; // 是否异步加载,默认异步加载
$scope.config.binding = "/api/1.0/unit/user"; // 绑定数据的url
$scope.config.kind = $attrs.kind; // 请求数据方式post get
$scope.config.datatype = $attrs.datatype; //提交数据方式json
$scope.config.text = $attrs.text; //提交数据方式json
$scope.config.user = []; //选中用户信息
$scope.config.flag = true; //标志位
if ($scope.async == "true" || $scope.async == undefined) {
var setting = {
async : {
enable : true,
url : '/api/1.0/unit/user',
autoParam : [ "id" ],
type : 'get'
},
check : {
enable : true,
chkStyle : "checkbox",
chkboxType : {
"Y" : "s",
"N" : "ps"
},
},
data : {
simpleData : {
enable : true,
idKey : "id", // 指定节点属性名
pIdKey : "parentid", // 指定父节点属性名
rootPId : -1
},
key : {
name : "Name"
}
},
callback : {
onCheck : function(event, treeId, treeNode) {
if (treeNode.checked == false) {
cancelParentNodeChecked(treeNode);
}
getRootOnde();
treeNode.expand=false;
treeNode.user=$scope.config.user;
$scope.$apply(function() {
ngModel.$setViewValue(treeNode);
});
},
onExpand : function(event, treeId, treeNode) {
//父节点展开勾选子节点
if (treeNode.checked && treeNode.isParent) {
cancelChecked(treeNode);
}
}
}
};


//为了实现百度网盘的分享人员树,自定义方法
//递归去除父类节点的选中
function cancelParentNodeChecked(node) {
zTree = $.fn.zTree.getZTreeObj("tree");
if (node.getParentNode()) {
zTree.checkNode(node.getParentNode(), false, false);
cancelParentNodeChecked(node.getParentNode());
}
}
//递归勾选子类
function cancelChecked(node) {
if (node.isParent) {//判断是否为父节点
if (node.zAsync) {//判断该节点是否异步加载过子节点(有木有展开)
zTree = $.fn.zTree.getZTreeObj("tree");
var childs = node.children;
for ( var i = 0; i < childs.length; i++) {
zTree.checkNode(childs[i], true, false);//勾选子节点
cancelChecked(childs[i]);
}
}
}
}
function getRootOnde() {
$scope.config.user = [];
var treeObj = $.fn.zTree.getZTreeObj("tree");
var nodes = treeObj.getCheckedNodes(true);
for ( var i = 0; i < nodes.length; i++) {
var node = nodes[i].getParentNode();
if (node == null || nodes[i].getParentNode().checked == false) {
angular.forEach($scope.config.user, function(item, index) {
if (nodes[i].id == item.id && $scope.config.flag) {
$scope.config.flag = false;
}
});
if ($scope.config.flag) {
$scope.config.user.push(nodes[i]);
}
$scope.config.flag = true;
} else {
while (node != null) {
if (node.getParentNode() == null
|| node.getParentNode().checked == false) {
angular.forEach($scope.config.user, function(item, index) {
if (node.id == item.id && $scope.config.flag) {
$scope.config.flag = false;
}
});
if ($scope.config.flag) {
$scope.config.user.push(node);
}
$scope.config.flag = true;
break;
}
node = node.getParentNode();
}
}
}
$scope.$apply();
}
// 初始化树
eval("$.fn.zTree.init($('#"+ $scope.config.id+"'), setting)");
} else {

}
}
};
return option;
});


ceshiapp.controller("ceshicontroller", function($scope, $http) {
$scope.user = [];
$scope.auth_treenode_onclick=function(checked){
if (checked.expand == false || checked.expand == undefined) {
$scope.user =checked.user;
checked.expand = true;
} else {
return;
}
};
});
</script>

</html>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
AngularJS中,你可以使用自定义指令或服务来实现装防抖和节流函数。下是一个示例: 1. 创建一个自指令或服务,例如名为`debThrottle`的自定义指令: ```javascript app.directive('debounceThrottle', function($timeout) { return { restrict: 'A', scope: { debounceThrottle: '&', debounceDelay: '=', throttleDelay: '=' }, link: function(scope, element) { var timeoutId; element.on('input', function() { if (timeoutId) { $timeout.cancel(timeoutId); } if (scope.debounceDelay) { timeoutId = $timeout(function() { scope.debounceThrottle(); }, scope.debounceDelay); } else if (scope.throttleDelay) { timeoutId = $timeout(function() { scope.debounceThrottle(); timeoutId = undefined; }, scope.throttleDelay); } else { scope.debounceThrottle(); } }); } }; }); ``` 2. 在你的HTML中使用该自定义指令,并传递相应的参数: ```html <input type="text" debounce-throttle="handleInput()" debounce-delay="500" throttle-delay="1000"> ``` 在上述示例中,我们通过`debounce-delay`和`throttle-delay`属性分别设置了防抖和节流的延迟时间(以毫秒为单位)。 3. 在你的AngularJS控制器中,实现相应的处理函数: ```javascript $scope.handleInput = function() { // 处理输入事件 }; ``` 这样,当用户在输入框中输入时,`handleInput()`函数将根据防抖和节流的设置进行触发。 通过自定义指令或服务的方式,你可以在AngularJS中方便地封装防抖和节流函数,并在需要的地方进行复用。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值