I need to implement a multiselect
in the view I'm working on, the idea is to have several options selected in the same select
. I have tried some components that I found on the web but without good results.
Controller route and method in Laravel works fine.
I have to feed the Multiselect from a method that brings the data, If you can help me it would be great, I leave you the code
event.vue
<template>
<div>
<div class="col-sm-12">
<select class="form-control form-control-line">
<option v-for="coin in coins" :key="coin.id" value="coin.id">
{{ coin.name }}
</option>
</select>
</div>
</template>
<script>
export default {
data () {
coins: [],
},
created() {
this.getCoins();
},
methods: {
getCoins(){
let urlCoin = '/dashboard/coins';
axios.get(urlCoin)
.then((response) => {
this.coins = response.data;
})
.catch((err) => {
})
}
}
</script>
The idea is to achieve this
I use Laravel 5.6
andVueJS 2
You can use this library
vue-multiselect
to achieve the expected result, on your page there is the basic configuration. And since you are using the Single File Component it will be even easier.The basic parts is
:options
that it will contain the array that is obtained from PHP and assigned tocoins
after the component has been mounted (mounted()) , as it will possibly be added to a subsequent request perhaps, it is usedv-model
to bind data , it will contain the selected elements .Label
, will be what will be displayed simulating thattext
of aoption
,track-by
simulating thatvalue
of aoption
You must first install through
npm
the library or through acdn
Then it will be possible to use it in the component (the names of the coins and coin properties are left to your preference)
Component.vue