-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSuggestion.js
60 lines (48 loc) · 1.39 KB
/
Suggestion.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
class Suggestion {
constructor(options) {
this.options = options
this.$input = $(this.options.input)
this.$input.wrap('<div class="ZakasSuggestion"></div>')
this.$wrapper = this.$input.parent()
this.$ol = $('<ol class="ZakasSuggestion-list"></ol>')
this.$input.after(this.$ol)
this.$loading = $('<div class="ZakasSuggestion-loading"></div>')
this.$loading.html(this.options.loading)
this.$input.after(this.$loading)
this.$empty = $('<div class="ZakasSuggestion-empty"></div>')
this.$empty.html(this.options.emptyHint)
this.$input.after(this.$empty)
this.eventBind()
}
eventBind() {
var timerID
this.$input.on('input', (e) => {
if (timerID) {
window.clearTimeout(timerID)
}
timerID = setTimeout(() => {
this.search(e.target.value)
}, 200)
})
this.$ol.on('click','li',(e)=>{
this.$input.val(e.target.innerText)
})
}
search(keyword) {
console.log('开始搜索')
this.$wrapper.addClass('loading')
this.options.search(keyword, (array) => {
this.$wrapper.removeClass('loading empty')
this.$ol.empty()
if (!array || array.length === 0){
this.$wrapper.addClass('empty')
console.log('kong')
}
array.forEach(text => {
var $li = $('<li></li>')
$li.text(text)
this.$ol.append($li)
})
})
}
}