Skip to content

Instantly share code, notes, and snippets.

@asvae
Created June 19, 2016 22:44
Show Gist options
  • Save asvae/fe1ce1d34fd73ba1b8dbcb391d2f8c94 to your computer and use it in GitHub Desktop.
Save asvae/fe1ce1d34fd73ba1b8dbcb391d2f8c94 to your computer and use it in GitHub Desktop.
ajax-select.html
<style>
</style>
<template>
<div>
<!-- Displays selected item only
after click on multiselect, then out of it.-->
<multiselect
:options.sync="options"
:selected="selected"
:multiple="false"
:placeholder="placeholder"
:clear-on-select="false"
:on-search-change="fetchData"
:on-change="onChange"
:key="key"
:label="label"
></multiselect>
<!-- Properly displays the selected item -->
<pre v-text="selected | json"></pre>
</div>
</template>
<script>
import {Multiselect} from 'vue-multiselect'
import _ from 'lodash'
export default{
data(){
return {
options: [],
}
},
props: {
source: {type: String, required: true},
selected: {type: Object, required: false},
placeholder: {type: String},
key: {type: String, default: 'id'},
label: {type: String, default: 'name'},
},
watch: {
selected (selected){
if (selected) {
this.placeholder = selected.name
}
}
},
components: {
Multiselect,
},
methods: {
fetchData (value){
let response = {filters: {}}
response.filters[this.label] = value
this.$api_demo.load(this.source, response)
.then(function (response) {
this.options = response.data
})
},
onChange (selected){
this.$emit('selected', _.cloneDeep(selected))
}
}
}
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment