3 |
4 |
Autocomplete Demo
5 |
6 |
7 |
Api data source (github api)
8 |
Using the source prop we can set an api endpoint for the autocomplete to hit.
9 |
If results are returned under a specific key you can set that via apiResultsProperty key
10 |
11 |
12 |
Example string source
13 |
19 |
20 |
21 |
22 | <autocomplete
23 | source="https://api.github.com/search/repositories?q="
24 | results-property="items"
25 | results-display="full_name">
26 | </autocomplete>
27 |
28 |
29 |
30 |
31 |
Example string-valued function
32 |
38 |
39 |
40 |
41 | <autocomplete
42 | :source="urlFunction"
43 | results-property="items"
44 | results-display="full_name">
45 | </autocomplete>
46 |
47 | methods: {
48 | urlFunction (input) {
49 | return 'https://api.github.com/search/repositories?q=' + input
50 | }
51 | }
52 |
53 |
54 |
55 |
Results:
56 |
{{ apiResults }}
57 |
58 |
59 |
60 |
61 |
62 |
63 |
64 |
65 |
Object data source
66 |
Optionally the data source can be set by an Object.
67 |
If there are any preset values you can define them with initialValue and initialDisplay props
68 |
69 |
Example
70 |
78 |
79 |
80 |
81 |
Results:
82 |
{{ objectResults }}
83 |
84 |
85 |
86 | <autocomplete
87 | :source="[{id:1,name:'abc',other:'!'},{id:2,name:'def',other:'?'}]"
88 | :results-display="function(obj) {return obj.name + obj.other}"
89 | :initial-display="'abc'"
90 | :initial-value="1">
91 | </autocomplete>
92 |
93 |
94 |
95 |
96 |
97 |
Binding with v-model
98 |
99 | The autocomplete components sets two values on selection: value and display.
100 | display is used as the input display value and value is used for the selected value.
101 | By default these are id and name.
102 |
103 |
When using v-model the value property is bound.
104 |
105 |
Example
106 |
109 |
110 |
111 |
112 |
Results:
113 |
{{ demo1 }}
114 |
115 |
116 |
117 | <autocomplete
118 | :source="[{id:1,name:'abc'},{id:2,name:'def'}]"
119 | v-model="demo1">
120 | </autocomplete>
121 |
122 |
123 |
124 |
125 |
126 |
Overriding results with a slot
127 |
This example is listening to the noResults event to insert html into the default results list.
128 |
129 |
135 | Click to create a new one
136 |
137 |
138 |
139 | <autocomplete
140 | :source="'https://api.github.com/search/users?q='"
141 | results-property="items"
142 | results-display="login"
143 | @noResults="ask = true">
144 | <li v-if="ask" slot="results" class="autocomplete__results__item" @click="create">Click to create a new one</li>
145 | </autocomplete>
146 |
147 |
148 |
149 |
150 |
151 |
Adding an always present result item with a slot
152 |
153 |
157 | Click to create a new one
158 |
159 |
160 |
161 | <autocomplete
162 | :source="'https://api.github.com/search/users?q='"
163 | results-property="items"
164 | results-display="login">
165 | <li slot="lastResult" class="autocomplete__results__item" @click="create">Click to create a new one</li>
166 | </autocomplete>
167 |
168 |
169 |
170 |
171 |
172 |
Using function for resultsDisplay and returning html
173 |
174 |
175 |
Example
176 |
182 |
183 |
184 |
185 |
Results:
186 |
{{ apiResults }}
187 |
188 |
189 |
190 | <autocomplete
191 | source="https://api.github.com/search/repositories?q="
192 | results-property="items"
193 | :results-display="formatDisplay">
194 | </autocomplete>
195 |
196 |
197 |
198 |
199 |
200 |
201 |
202 |