-
Notifications
You must be signed in to change notification settings - Fork 4
/
treeSelect.js
103 lines (96 loc) · 2.63 KB
/
treeSelect.js
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
var uid=0;
class TreeSelect{
static getUniquId(){
return uid++;
}
constructor(options){
var self=this;
var defaultOptions={
valueKey:'id'
}
self.options=options=$.extend(defaultOptions,options);
var uid=TreeSelect.getUniquId();
var tpl=`
<input type="text" class="form-control treeSelect-input "/>
<div class="ztree treeSelect-panel" id="treeSelect_panel_${uid}"></div>
`;
var ele=$(options.element);
ele.html(tpl);
var input=ele.find('.treeSelect-input');
var panel=ele.find('.treeSelect-panel');
self.element=ele;
self.input=input;
self.panel=panel;
ele.css({
'position':'relative'
});
input.on('keydown',function(){
//input.val(self.text);
return false;
});
input.click(function(){
if(!self.isOpen()){
self.open();
}else{
self.close();
}
});
if(options.url){
$.ajax({
type: options.type,
url: options.url,
dataType:'json',
data:options.param,
sucess:function(data){
self.render(data);
}
})
}else if(options.data){
self.render(options.data);
}
}
isOpen(){
var panel=this.panel;
return !(panel.css('display')=='none'||panel.height()==0||panel.css('opacity')==0)
}
render(data){
var self=this;
var panel=self.panel;
var setting={
callback:{
onClick:function(event, treeId, treeNode){
if(!treeNode.isParent){
self.input.val(treeNode.name);
self.value=treeNode[self.options.valueKey];
self.text=treeNode.name;
self.close();
}
}
}
};
self.ztree=$.fn.zTree.init(panel, setting, data);
}
open(){
var self=this;
var panel=self.panel;
panel.css({
height:'auto',
opacity:1
});
panel.show();
self.mask=$('<div class="treeSelect-mask"></div>');
$('body').append(self.mask);
self.mask.click(function(){
self.close();
})
}
close(){
var self=this;
//panel.animate({
// height:0,
// opacity:0
//},500);
self.panel.hide();
self.mask.remove();
}
}