|
1 | 1 | <template> |
2 | | - <div class="btn-group pull-left"> |
3 | | - <input ref="input" type="text" :id="id" class="form-control" v-model="input" autocomplete="off" |
| 2 | + <div class="btn-group input-group pull-left"> |
| 3 | + <input ref="input" type="text" class="form-control" v-model="input" autocomplete="off" |
4 | 4 | @keydown.down="keyDown" @keydown.up="keyUp" @keydown.enter="keyEnter" @blur="blur"> |
5 | 5 |
|
6 | | - <ul v-if="isItems" class="dropdown-menu drop-menu-right show" @mouseenter="focusIn" @mouseleave="focusOut"> |
7 | | - <li v-for="(title, i) in items" :class="{active: i === index}" :key="i"> |
8 | | - <a @click="onClick(i)" onclick="return false">{{title}}</a> |
9 | | - </li> |
10 | | - </ul> |
| 6 | + <label v-if="all" class="btn btn-default button" @click="getAll"> |
| 7 | + <span class="buttonText">...</span> |
| 8 | + </label> |
| 9 | + |
| 10 | + <div class="dropdown-items show" :class="{hidden: !isItems}"> |
| 11 | + <div :id="id" class="ul" @mouseenter="focusIn" @mouseleave="focusOut"> |
| 12 | + <div class="li" v-for="(item, i) in items" :class="{active: i === index}" :key="i"> |
| 13 | + <a @click="onClick(i)" onclick="return false" :title="item.description"> |
| 14 | + {{item.name}} <br><small>{{item.description}}</small> |
| 15 | + </a> |
| 16 | + </div> |
| 17 | + </div> |
| 18 | + </div> |
11 | 19 | </div> |
12 | 20 | </template> |
13 | 21 |
|
|
17 | 25 | export default { |
18 | 26 | components: {}, |
19 | 27 | props: { |
20 | | - value: String, |
21 | | - fetch: Function, |
22 | | - timeout: Number, |
| 28 | + value: String, |
| 29 | + fetch: Function, |
| 30 | + fetchAll: Function, |
| 31 | + timeout: Number, |
| 32 | + all: Boolean, |
23 | 33 | }, |
24 | 34 | name: 'InputAutocomplete', |
25 | 35 | data() { |
|
38 | 48 | if (this.fetch && this.input && this.input.trim()) { |
39 | 49 | this.fetch(this.input).then((items) => { |
40 | 50 | if (items && items.length > 0) { |
41 | | - this.$set(this, 'items', _.chunk(_.uniq([this.input].concat(items)), 7)[0]); |
| 51 | + this.$set(this, 'items', _.chunk(_.uniq([{ |
| 52 | + name: this.input, |
| 53 | + description: '' |
| 54 | + }].concat(items)), 5)[0]); |
42 | 55 | } |
43 | 56 | }); |
44 | 57 | } |
45 | | - }, this.timeout || 100), |
| 58 | + }, this.timeout || 300), |
46 | 59 | }; |
47 | 60 | }, |
48 | 61 | mounted() { |
49 | 62 | }, |
50 | 63 | methods: { |
| 64 | + bindScroll() { |
| 65 | + $(`#${this.id}`).slimScroll({ |
| 66 | + height: '233px', |
| 67 | + position: 'right', |
| 68 | + size: "5px", |
| 69 | + color: '#98a6ad', |
| 70 | + wheelStep: 3, |
| 71 | + }); |
| 72 | + }, |
| 73 | + unbindScroll() { |
| 74 | + let scroll = $(`#${this.id}`); |
| 75 | + scroll.slimScroll({ destroy: true }); |
| 76 | + scroll[0].style.height = 'auto'; |
| 77 | + }, |
| 78 | + getAll() { |
| 79 | + if (this.fetchAll) { |
| 80 | + this.$refs.input.focus(); |
| 81 | + this.$set(this, 'index', 0); |
| 82 | + this.$set(this, 'dropMenuFocus', false); |
| 83 | + this.$set(this, 'items', [this.$t('labels.loading')]); |
| 84 | + this.$nextTick(() => this.bindScroll()); |
| 85 | + this.fetchAll().then((items) => { |
| 86 | + this.$set(this, 'items', items); |
| 87 | + }); |
| 88 | + } |
| 89 | + }, |
51 | 90 | keyEnter() { |
52 | 91 | this.entered = true; |
53 | 92 |
|
|
72 | 111 | event.preventDefault(); |
73 | 112 | }, |
74 | 113 | onClick(index) { |
| 114 | + this.unbindScroll(); |
75 | 115 | this.entered = true; |
76 | 116 | this.setFromIndex(index); |
77 | 117 | }, |
|
93 | 133 | }, |
94 | 134 | setFromIndex(index = null) { |
95 | 135 | if (null !== index) { |
96 | | - this.$set(this, 'input', this.items[index]); |
| 136 | + this.$set(this, 'input', this.items[index].name); |
97 | 137 | } |
98 | 138 | this.reset(); |
99 | 139 | }, |
|
119 | 159 | this.$emit('update:value', value); |
120 | 160 | this.reset(); |
121 | 161 | this.fetchDebounce(); |
| 162 | + this.unbindScroll(); |
122 | 163 | }, |
123 | 164 | value(value) { |
124 | 165 | this.$set(this, 'input', value); |
|
131 | 172 | <style lang="less" scoped> |
132 | 173 | .btn-group { |
133 | 174 | width: 100%; |
| 175 | +
|
| 176 | + input[type="text"] { |
| 177 | + display: inline-block; |
| 178 | + width: calc(100% - 45px); |
| 179 | + } |
| 180 | + } |
| 181 | +
|
| 182 | + .button { |
| 183 | + float: none; |
| 184 | + height: 36px; |
| 185 | + width: 45px; |
| 186 | + overflow: hidden; |
| 187 | + border-radius: 3px !important; |
| 188 | + border-top-left-radius: 0 !important; |
| 189 | + border-bottom-left-radius: 0 !important; |
| 190 | + } |
| 191 | +
|
| 192 | + /* Dropdown */ |
| 193 | + .dropdown-items { |
| 194 | + display: block; |
| 195 | + position: absolute; |
| 196 | + width: 100%; |
| 197 | + padding: 4px 0; |
| 198 | + background-color: #5f6b77; |
| 199 | + transition: all 300ms ease; |
| 200 | + -moz-transition: all 300ms ease; |
| 201 | + -webkit-transition: all 300ms ease; |
| 202 | + -o-transition: all 300ms ease; |
| 203 | + -ms-transition: all 300ms ease; |
| 204 | + border: 0; |
| 205 | + box-shadow: 0 2px 5px 0 rgba(0, 0, 0, 0.26); |
| 206 | + border-radius: 3px; |
| 207 | + top: calc(100% + 2px); |
| 208 | + z-index: 2; |
134 | 209 | } |
135 | 210 |
|
136 | | - .dropdown-menu { |
| 211 | + .li, .li a { |
| 212 | + display: block; |
137 | 213 | width: 100%; |
| 214 | + cursor: default; |
| 215 | + } |
| 216 | +
|
| 217 | + .dropdown-items .li a { |
| 218 | + padding: 6px 20px; |
| 219 | + color: rgba(255, 255, 255, 0.5); |
| 220 | + font-weight: 900; |
| 221 | +
|
| 222 | + small { |
| 223 | + font-weight: 100; |
| 224 | + } |
| 225 | + } |
| 226 | +
|
| 227 | + .dropdown-items .li a:hover { |
| 228 | + background-color: rgba(255, 255, 255, 0.1); |
| 229 | + color: #ffffff !important; |
| 230 | + } |
| 231 | +
|
| 232 | + .dropdown-items .divider { |
| 233 | + background-color: #98a6ad; |
| 234 | + } |
| 235 | +
|
| 236 | + .dropdown-items .active a, |
| 237 | + .dropdown-items .active a:hover, |
| 238 | + .dropdown-items .active a:focus, |
| 239 | + .dropdown-items .li a:focus, |
| 240 | + .dropdown-items .li a:hover { |
| 241 | + background-color: rgba(255, 255, 255, 0.1); |
| 242 | + color: #ffffff !important; |
| 243 | + } |
| 244 | +
|
| 245 | + .dropup .dropdown-items { |
| 246 | + box-shadow: 0px -1px 5px 0 rgba(0, 0, 0, 0.26); |
| 247 | + } |
138 | 248 |
|
139 | | - li > a { |
| 249 | + .dropdown-items .ul { |
| 250 | + .li a { |
140 | 251 | white-space: nowrap; |
141 | 252 | text-overflow: ellipsis; |
142 | 253 | overflow: hidden; |
|
0 commit comments