dictionary.js
2.09 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
/**
*
* @Description: TODO(字典转换相关)
* @author PanZhao
* @date 2016年6月20日 中午13:14:22
*
*/
var dictionaryUtils = (function(){
var dictionaryData = {};
//获取所有字典
$.get('/dictionary/all', function(ds){
//分组数据
$.each(ds, function(){
if(!dictionaryData[this.dGroup])
dictionaryData[this.dGroup] = {};
dictionaryData[this.dGroup][this.dCode] = this.dName;
});
});
var dictObject = {
//获取所有字典组
groups: function(){
return dictionaryData[0];
},
//获取字典组下的字典
getByGroup: function(group){
return dictionaryData[group];
},
//转换字典代码
transformCode: function(group, code){
return dictionaryData[group][code];
},
//转换页面DOM元素
transformDom: function(es){
$.each(es, function(i, e){
switch ($(e)[0].tagName) {
case 'SELECT':
transformSelect(e);
break;
case 'INPUT':
transformInput(e);
break;
default:
transformText(e);
break;
}
});
}
};
function transformText(e){
var group = $(e).data('group');
var code = $.trim($(e).text());
var name = dictionaryData[group][code];
if(name)
$(e).text(name);
else
console.log('字典转换失败', e);
}
function transformInput(e){
var group = $(e).data('group');
var code = $(e).data('code');
var name = dictionaryData[group][code];
if(name)
$(e).val(name);
else
console.log('字典转换失败', e);
}
function transformSelect(e){
var group = $(e).data('group');
var code = $(e).data('code');
var items = dictionaryData[group];
var ops = '';
for(var c in items){
if(c == code)
ops += '<option value="'+c+'" selected>'+items[c]+'</option>';
else
ops += '<option value="'+c+'">'+items[c]+'</option>';
}
//如果没有默认选中值
if(!code)
ops = '<option value="">请选择...</option>' + ops;
var newE = $(e).clone().removeClass('nt-dictionary').html(ops);
$(e).replaceWith(newE);
}
return dictObject;
})();