btnSelect下拉按鈕
按鈕支持多個按鈕關(guān)聯(lián),帶聯(lián)想功能
// 下拉選擇按鈕 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="專業(yè)">
<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>
*
* 簡寫 在實例化的時候會補全,不過這樣是沒有初始數(shù)據(jù)的
<span class="btn__select">
<input type="text" placeholder="專業(yè)">
<input type="hidden" name="qwe">
</span>
*
* 導(dǎo)入數(shù)據(jù)
window.btnSelectList.qwe.set({
caa: "中國美術(shù)學(xué)院",
tafa: "天津美術(shù)學(xué)院",
xafa: "西安美術(shù)學(xué)院"
})
*
* 獲取
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"))
})
// 監(jiān)聽輸入動作偏塞,觸發(fā)搜索功能
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關(guān)聯(lián)器
* 在btnSelect初始化之后 才能 將關(guān)聯(lián)器調(diào)用初始化
*
* 在實例化關(guān)聯(lián)器是傳入與關(guān)聯(lián)按鈕的name數(shù)組吆你,按關(guān)聯(lián)順序排序
* 如下:province的下一級是city
*
* 關(guān)于初始化的板栗:
var btnSelectRelations = new btnSelectRelation(["province","city"])
btnSelectRelations.init()
*
* 設(shè)置數(shù)據(jù):
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語法
/* 清除按鈕樣式 */
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