用ES6语法写的btnSelect下拉按钮

btnSelect下拉按钮

按钮支持多个按钮关联,带联想功能

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
192
193
194
195
196
197
198
199
200
201
202
203
// 下拉选择按钮 btnSelect
/* 暴露btnSelect类,假定全局存在jQuery */
export default class btnSelect {
constructor () {
this.btnSelectClass = ".btn__select"
}
init () {
var btnSelectList = {}
$(this.btnSelectClass).each(function(){
var name = $(this).children('input[type="hidden"]').attr("name");
var btnSelect = new btnSelectClass($(this))
btnSelectList[name] = btnSelect
btnSelect.init()
})
window.btnSelectList = btnSelectList
var btnSelectRelations = new btnSelectRelation(["province","city"])
btnSelectRelations.init()
}
}
/**
* 注册的组件放在window.btnSelectList下 取input[type='hidden']的name值为键名
<span class="btn__select">
<input type="text" placeholder="专业">
<input type="hidden" name="qwe">
<button type="button"></button>
<ul>
<li name="caa">123</li>
<li name="tafa">456</li>
<li name="xafa">789</li>
</ul>
</span>
*
* 简写 在实例化的时候会补全,不过这样是没有初始数据的
<span class="btn__select">
<input type="text" placeholder="专业">
<input type="hidden" name="qwe">
</span>
*
* 导入数据
window.btnSelectList.qwe.set({
caa: "中国美术学院",
tafa: "天津美术学院",
xafa: "西安美术学院"
})
*
* 获取
window.btnSelectList.qwe.get()
*
*/
class btnSelectClass {
constructor (element, data = null){
this.btn__select = element
this.data = data
this.preText = ""
this.name = undefined
this.btnSelectRelation = null
this.set = data => {
this.data = data
this.reBuildDom(data)
}
this.get = () => this.btn__select.children("input[type='hidden']").val()
}
init () {
var _this = this
this.data == null && this.buildBtnSelect()
// 选择 取值
this.btn__select.on("click","li",function(){
_this.setValue($(this).attr("name"), $(this).text())
typeof _this.btnSelectRelation === "function" && _this.btnSelectRelation(_this.name,$(this).attr("name"))
})
// 监听输入动作,触发搜索功能
this.btn__select.children("input[type='text']").keyup(e => {
var val = $(e.target).val()
val !== this.preText && this.search(val)
})
this.setName()
}
setValue (name, value) {
this.btn__select.children("input[type='text']").val(value)
this.btn__select.children("input[type='hidden']").val(name)
}
setName () {
this.name = this.btn__select.children('input[type="hidden"]').attr("name")
}
buildBtnSelect () {
var ul = this.btn__select.children("ul")
var button = this.btn__select.children("button")
button.length == 0 && this.btn__select.children("input[type='hidden']").after('<button type="button"></button>')
if(ul.length == 0){
this.btn__select.append("<ul></ul>")
this.data = {}
}else{
var data = {}
var items = this.btn__select.find("li")
items.each(function(){
data[$(this).attr("name")] = $(this).text()
})
this.data = data
}
}
reBuildDom (data) {
var ul = this.btn__select.children("ul")
ul.empty()
var items = ""
$.each(data, function (k,v) {
items += `<li name="${k}">${v}</li>`
})
ul.append(items)
}
search (val) {
this.preText = val
var data = {}
if(val != ""){
$.each(this.data,(k, v) => {
(v.indexOf(val) > -1) && (data[k] = v)
})
this.reBuildDom(data)
}else{
this.reBuildDom(this.data)
}
}
}
/*["province","city"]*/
/**
* btnSelect关联器
* 在btnSelect初始化之后 才能 将关联器调用初始化
*
* 在实例化关联器是传入与关联按钮的name数组,按关联顺序排序
* 如下:province的下一级是city
*
* 关于初始化的板栗:
var btnSelectRelations = new btnSelectRelation(["province","city"])
btnSelectRelations.init()
*
* 设置数据:
window.btnSelectList.province.setSource({
zj: "浙江省",
hn: "湖南省",
sc: "四川省"
})
window.btnSelectList.city.setSource({
zj: {
hz:"杭州市",
sx:"绍兴市",
jh:"金华市"
},
hn: {
cs:"长沙市",
yy:"岳阳市",
cd:"常德市"
},
sc: {
cd:"成都市",
my:"绵阳市",
dy:"德阳市"
}
})
*
*/
class btnSelectRelation {
constructor (relationType) {
this.relationType = relationType
this.relationData = []
this.set = (childName, data) => {
var index = this.getChildIndex(childName)
this.relationData[index] = data
index == 0 && this.setChildData(index, data)
}
}
init () {
$.each(this.relationType,(k, v) => {
window.btnSelectList[v].setSource = data => {
var name = v
this.set(name,data)
}
window.btnSelectList[v].btnSelectRelation = (btnName,key) => {
var index = this.getChildIndex(btnName) + 1
index > 0 && index < this.relationData.length && this.filter(index, key)
}
})
}
getChildIndex (childName) {
return this.relationType.indexOf(childName)
}
filter (index, key) {
this.setChildData(index, this.relationData[index][key])
}
setChildData (index, data) {
window.btnSelectList[this.relationType[index]].set(data)
}
}

补上css,这里是用stylus语法

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
/* 清除按钮样式 */
btn-clean()
border none
outline none
box-shadow none
background none
/* 下拉按钮样式 */
dropDown()
padding 0
border-left 6px solid transparent
border-right 6px solid transparent
border-top 10px solid mt-orange
.btn__select
btn-clean()
border 1px solid #ccc
border-radius 2px
font-size 14px
color gray-a7
padding 0.5em 0.7em
position relative
input,
button
btn-clean()
&:focus~ul
display block
button
dropDown()
ul
position absolute
top 100%
left 0
width 100%
border-radius 0 0 2px 2px
box-shadow 0 2px 2px rgba(0,0,0,.3)
display none
&:hover
display block
li
padding 0.5em 0.3em
border 1px solid #ccc
&:not(:last-child)
border-bottom none
&:last-child
border-radius 0 0 2px 2px