-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmySelect.js
More file actions
191 lines (177 loc) · 6.35 KB
/
mySelect.js
File metadata and controls
191 lines (177 loc) · 6.35 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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
$(function () {
$json = [{"userId": "138910", "name": "916728"}, {"userId": "138929", "name": "df01"}, {
"userId": "138930",
"name": "df02"
}, {"userId": "138931", "name": "df03"}, {"userId": "138932", "name": "df04"}, {
"userId": "138933",
"name": "df05"
}, {"userId": "138934", "name": "df06"}, {"userId": "138935", "name": "df07"}, {
"userId": "138949",
"name": "张里平"
}, {"userId": "138948", "name": "唐毅"}, {"userId": "139826", "name": "李莉"}, {
"userId": "139827",
"name": "huang"
}, {"userId": "139861", "name": "zhujian"}, {"userId": "139884", "name": "王伟"}, {
"userId": "139231",
"name": "郭贺"
}, {"userId": "139233", "name": "张振振"}, {"userId": "139234", "name": "杨霆"}, {
"userId": "139235",
"name": "安诗全"
}, {"userId": "139236", "name": "陈培"}, {"userId": "139237", "name": "张进奇"}, {
"userId": "139267",
"name": "王总"
}, {"userId": "139304", "name": "teacher_12"}, {"userId": "139305", "name": "王秀燕"}, {
"userId": "139278",
"name": "胡莹莹"
}, {"userId": "139281", "name": "徐从瑾"}];
$item = ListToJson($json);
initMyLeftSelect();
function initMyLeftSelect() {
createLi($item);
createIndex();
autoSearch($item);
}
function createLi(obj) {
$('#leftUlSelect').html('');
$.each(obj, function (n, value) {
if (n) {
$li = '<li value="' + value.id + '" select=0 data-id="' + n + '">' +
'<span>' + value.value + '</span>' +
'<div class="icon_close"></div>' +
'</li>';
$('#leftUlSelect').append($li);
}
});
}
function createIndex() {
$('#leftUlSelect').listnav({
includeOther: true,
noMatchText: '没有内容',
includeAll: false, //包括'all'这个按钮,默认true
includeNums: false, //包括'0-9'这个按钮,默认true
includeOther: false,//包括 '...' 选项来过滤非英语字符的
showCounts: false //显示鼠标悬浮时列表项的数目
});
}
function autoSearch(data){
$('#to_search').AutoComplete({
'data': data,
'width': 195,
'listStyle': 'custom',
'maxHeight': 400,
'createItemHandler': function (index, data) {
var div = $("<li class='LeftRight_search_result' value=" + data.id + " select=0 ><span>" + data.value + "</span><div class='icon_close'></div></li>");
return div;
}
}).AutoComplete('show');
}
//选择多项
$("#addOne").click(function () {
$leftSelected = $("#leftUlSelect li[select=1]");
$leftSelected.attr('select', '0').removeClass('hover');
$leftSelected.clone().attr('style', 'display:block').appendTo("#rightUlSelect");
$leftSelected.attr('style', 'display:none');
$leftSelected.attr('selected','');
});
function getRValue() {
var rValue = [];
$("#rightUlSelect li").each(function () {
rValue.push($(this).attr('value'));
});
return rValue;
}
function getLValue() {
var lValue = [];
$("#leftUlSelect li").each(function () {
lValue.push($(this).attr('value'));
});
return lValue;
}
//移除一项
$('#rightUlSelect').hover(function () {
$('#rightUlSelect').find('.icon_close').mouseover(function () {
$(this).parent('li').attr('select', '1');
});
$('#rightUlSelect').find('.icon_close').mouseout(function () {
$(this).parent('li').attr('select', '0');
});
$('#rightUlSelect').find('.icon_close').click(function () {
$rightSelected = $("#rightUlSelect li[select=1]");
$value = $rightSelected.attr('value');
var lValue = getLValue();
var flag = 0;
for (var i = 0; i <= lValue.length; i++) {
if (lValue[i] == $value) {
$("#leftUlSelect li[value=" + $value + "]").attr('style', 'display:list-item').removeAttr('selected');
$rightSelected.remove();
flag = 1;
break;
}
}
if (flag == 0) {
$rightSelected.attr('select', '0').clone().appendTo('#leftUlSelect');
$rightSelected.remove();
}
});
});
//左选择器选中效果
$('#leftUlSelect').hover(function () {
$('#leftUlSelect').find('li').click(function () {
$select = $(this).attr('select');
if ($select == 0) {
$(this).addClass('hover');
$(this).attr('select', '1');
} else {
$(this).removeClass('hover');
$(this).attr('select', '0');
}
});
});
//搜索框选中添加一项
$('.ac').hover(function () {
$(this).find('.LeftRight_search_result').on('click', function () {
$value = $(this).attr('value');
$leftU = $("#leftUlSelect li[value=" + $value + "]");
var rValue = getRValue();
var flag = 0;
for (var i = 0; i <= rValue.length; i++) {
if (rValue[i] == $value) {
flag = 1;
break;
}
}
if (flag == 0) {
$(this).clone().appendTo("#rightUlSelect");
$leftU.attr('style', 'display:none');
$leftU.attr('selected');
}
});
});
});
//转换为后台json对象保存选择好的数据
function ToJson() {
var jsonAll;
var jsonAllS = new Array();
var name = null;
var id = null;
$("#rightUlSelect li").each(function () {
id = $(this).val();
name = $(this).children('span').text();
var json = new Object();
json.id = id;
json.name = name;
jsonAllS.push(json);
});
jsonAll = JSON.stringify(jsonAllS)
return jsonAll;
}
//格式化json对象
function ListToJson(e) {
return $.map(e, function (row) {
return {
label: row.name,
value: row.name,
id : row.userId
}
})
}