easyui.hp.myTree.js
3.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
/**
* 显示一棵树,树的节点是一次性全部加载完 并且节点不再是children形式,而是通过id,pid的形式
* 依赖于easyui的tree
* 作者:黄平
* 日期:2012-10-17
*/
(function($) {
$.fn.myTree = function(options, param) {
var self = this;
if (typeof (options) == "string") {
var method = $.fn.myTree.methods[options];
if (method) {
return method.call(this, param);
} else {
return this.tree(options, param);
}
}
return this.each(function() {
var opt = $.extend({}, $.fn.myTree.defaults, options);
$(self).data("myTree", opt);
if (opt.ajaxParam && opt.ajaxParam.url) {
$.ajax($.extend({}, {
success : function(json) {
if (json) {
opt.dataList = json;
createTree(self, opt.rootPid);
}
},
dataType : "json"
}, opt.ajaxParam));
} else {
createTree(self, opt.rootPid);
}
});
};
/**
* 创建树
*/
function createTree(target, id) {
var opt = $(target).data("myTree");
$(target).tree($.extend({}, opt, {
data : _createChildRen(target, id)
}));
}
/**
* 查找子结点
*/
function _createChildRen(target, id) {
var opt = $(target).data("myTree");
var obj = {}, data = [];
var parentNode = _findChild(target, id);
if (!parentNode || parentNode.length == 0) {
return data;
}
$(parentNode).each(function(i, item) {
var dat = _createChildRen(target, item[opt.idField]);
obj = {
id : item[opt.idField],
text : item[opt.textField],
checked : item[opt.checkedField],
attributes : item,
children : dat
};
if (opt.allClosed) {
obj = $.extend({}, obj, {
state : "closed"
});
} else {
if (dat && dat.length > 0) {
obj = $.extend({}, obj, {
state : "closed"
});
}
}
data.push(obj);
});
return data;
}
/**
* 查找子结点
*/
function _findChild(target, id) {
var opt = $(target).data("myTree");
var arr = [];
if (opt.dataList && opt.dataList.length > 0) {
for ( var i = 0; i < opt.dataList.length; i++) {
if (opt.dataList[i][opt.pidField] == id) {
arr.push(opt.dataList.splice(i, 1)[0]);
i--;
}
}
}
return arr;
}
$.fn.myTree.methods = {
/**
* 获取所有被选中的checkbox(包括实心的节点)
*/
getCheckeds : function() {
var checked = $(this).tree("getChecked");
var checkbox2 = $(this).find("span.tree-checkbox2").parent();
$.each(checkbox2, function() {
var node = $.extend({}, $.data(this, "tree-node"), {
target : this
});
checked.push(node);
});
return checked;
},
/**
* 获取所有实心节点
*/
getSolidCheck : function() {
var checked = [];
var checkbox2 = $(this).find("span.tree-checkbox2").parent();
$.each(checkbox2, function() {
var node = $.extend({}, $.data(this, "tree-node"), {
target : this
});
checked.push(node);
});
return checked;
},
reload : function() {
return $(this).myTree($(this).data("myTree"));
}
};
$.fn.myTree.events = {};
$.fn.myTree.defaults = $.extend({}, $.fn.myTree.events, {
dataList : [],
ajaxParam : {},
idField : "id",
pidField : "pid",
textField : "text",
checkedField : "checked",
rootPid : 0,
allClosed : false //所有的节点都可以展开
});
})(jQuery);