Created
July 16, 2017 12:23
-
-
Save vladimirmyshkovski/df651131e8977a2435366c4539b75195 to your computer and use it in GitHub Desktop.
vue.js search autocomplete for Django
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<div id="base"> | |
<div class="btn-group" :class="{show : dropDown}"> | |
<input class="form-control mr-sm-2" placeholder="{% trans 'Search' %}" v-model="q" data-toggle="dropdown" aria-haspopup="true" :aria-expanded="{true : dropDown}"> | |
<div class="dropdown-menu"> | |
<search-item | |
v-for="item in search" | |
:item="item" | |
:key="item.id"> | |
</search-item> | |
</div> | |
</div> | |
</div> | |
<script type="text/javascript"> | |
Vue.component('search-item', { | |
props: ['item', 'value'], | |
delimiters: ["#{", "}"], | |
template: '#search-item', | |
}) | |
Vue.http.headers.common['X-CSRF-TOKEN'] = csrftoken | |
var base = new Vue({ | |
el: '#base', | |
delimiters: ["#{", "}"], | |
data: { | |
searchendpoint: 'http://' + window.location.host + '/search/autocomplete/?q=', | |
q: '', | |
search: [], | |
//show: true | |
}, | |
computed: { | |
dropDown: function() { | |
if ( Object.keys(this.search).length > 0) { | |
return true | |
} else { | |
return false | |
} | |
} | |
}, | |
watch: { | |
q: function() { | |
if (this.q.length > 0) { | |
this.$http.post(this.searchendpoint + this.q).then( | |
function(response){ | |
this.search = response.data | |
}, function(error){ | |
//console.log(error) | |
}) | |
} | |
} | |
} | |
}) | |
</script> | |
<script type="text/x-template" id="search-item"> | |
<a :href="item.link" class="btn dropdown-item"> | |
#{ item.title } | |
</a> | |
</script> | |
<script type="text/javascript"> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment