easyui.hp.myCombotree.js
2.34 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
/**
* 树的节点是一次性全部加载完 并且节点不再是children形式,而是通过id,pid的形式 依赖于easyui.combotree 作者:黄平
* 日期:2014-06-12
*/
(function($) {
$.fn.myCombotree = function(options, param) {
var self = this;
if (typeof (options) == "string") {
var method = $.fn.myCombotree.methods[options];
if (method) {
return method.call(this, param);
} else {
return this.combotree(options, param);
}
}
return this.each(function() {
var opt = $.extend({}, $.fn.myCombotree.defaults, options);
$(self).data("myCombotree", 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("myCombotree");
$(target).combotree($.extend({}, opt, {
data : _createChildRen(target, id)
}));
}
/**
* 查找子结点
*/
function _createChildRen(target, id) {
var opt = $(target).data("myCombotree");
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 (dat && dat.length > 0) {
obj = $.extend({}, obj, {
state : "closed"
});
}
data.push(obj);
});
return data;
}
/**
* 查找子结点
*/
function _findChild(target, id) {
var opt = $(target).data("myCombotree");
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.myCombotree.methods = {
};
$.fn.myCombotree.events = {};
$.fn.myCombotree.defaults = $.extend({}, $.fn.myCombotree.events, {
dataList : [],
ajaxParam : {},
idField : "id",
pidField : "pid",
textField : "text",
checkedField : "checked",
rootPid : 0
});
})(jQuery);