Newer
Older
<div class="autocomplete-container" :class="$vuetify.breakpoint.name">
v-model="select"
:loading="loading"
:items="items"
:search-input.sync="search"
item-text="label"
:menu-props="{maxHeight: $vuetify.breakpoint.name === 'xs' ? 190 : 500, transition: 'fade-transition'}"
:append-icon="null"
full-width
>
<template v-slot:item="data">
<span class="search-hit">{{data.item.label}} </span> ({{data.item.lang_set ? Array.from(data.item.lang_set).sort().join(', ') : 'fritekstsøk'}})
</template>
import debounce from "debounce"
},
data: function() {
return {
loading: false,
items: [],
search: null,
debounced: debounce(function(q, self) {
self.loading = true
return axios.get(self.endpoint + 'suggest?q=' + encodeURIComponent(q))
.then(
function(response) {
response.data.forEach((item, i) => {
let match = encodeURIComponent(item.match)
if (! hits[0] || hits[0].word != match) {
hits.splice(0, 0, {q: encodeURIComponent(q), lang_set: new Set(), word: match, articles: []})
}
hits[0].lang_set.add(item.dictionary == 'bob' ? 'bm' : 'nn')
hits[0].articles.push(item)
});
hits.forEach(function (hit) {
if (hit.lang_set) {
hit.label = decodeURIComponent(hit.word)
}
});
hits.reverse()
hits = hits.slice(0, 9)
let val1 = h1.label.length * 10 + (h1.label[0].toLowerCase() === h1.label[0] ? 0 : 1)
let val2 = h2.label.length * 10 + (h2.label[0].toLowerCase() === h2.label[0] ? 0 : 1)
return val1 - val2
let currentSearch = self.$refs.autocomplete.searchInput
if (q == currentSearch) {
self.items = hits
}
self.items.push({currentSearch: encodeURIComponent(currentSearch), label: currentSearch + ' '})
}
}
},
watch: {
search (val) {
if (! val) {
this.items = []
} else {
this.run_query(val)
}
if (item) {
if (typeof item === 'string') {
item = {"q": encodeURIComponent(item)}
}
setTimeout(() => this.$refs.autocomplete.blur(), 1)
}
},
methods: {
run_query(q) {
<style scoped>
.search-hit {
font-weight: bold;
margin-right: 5px;
color: var(--v-primary-base);
padding-left: 10px;
padding-right: 10px;