How to use multiselect in vue.js

In this article, we’ll look at how the Vue-Multiselect library can help improve our dropdown menus also how we can use multiselect in different way in vue.js.

Getting Started

  • To get started, we can install Vue-Multiselect by running:
npm install vue-multiselect --save
  • We can also add the library via script tags and add the CSS that’s associated with the package :
<script src="https://unpkg.com/vue-multiselect@2.1.0"></script>
<link rel="stylesheet" href="https://unpkg.com/vue-multiselect@2.1.0/dist/vue-multiselect.min.css">
  • Create multiselect.vue in component folder
  • So your multiselect.vue looks like below :
<template>
  <div class="row my-container">
    <div class="col-md-4">
      <label class="typo__label"
        >Please Select Preferabble Programing Language</label
      >
      <multiselect
        v-model="value"
        tag-placeholder="Add this as new tag"
        placeholder="Select Programming Language"
        label="label"
        track-by="id"
        :options="options"
        :multiple="true"
        :taggable="true"
      ></multiselect>
    </div>
  </div>
</template>

<script>
import Multiselect from "vue-multiselect";

export default {
  components: {
    Multiselect,
  },
  data() {
    return {
      value: [{ label: "Javascript", id: "1" }],
      options: [
        { label: "Vue.js", id: "2" },
        { label: "C#", id: "3" },
        { label: "React.js", id: "4" },
      ],
    };
  },
  
};
</script>

<style scoped>
.my-container{
    justify-content: center;
}
</style>
  • App.vue
<template>
  <div id="app">
   <multiselect></multiselect>
  </div>
</template>

<script>
import Multiselect from './components/multiselect.vue'

export default {
  name: 'App',
  components: {
    Multiselect
  }
}
</script>

<style>
#app {
  font-family: Avenir, Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  text-align: center;
  color: #2c3e50;
  margin-top: 60px;
}
</style>
  • main.js
import Vue from 'vue'
import App from './App.vue'
import BootstrapVue from "bootstrap-vue";
import "bootstrap/dist/css/bootstrap.css";
import "bootstrap-vue/dist/bootstrap-vue.css";

Vue.use(BootstrapVue);
Vue.config.productionTip = false

new Vue({
  render: h => h(App),
}).$mount('#app')
  • Output :

 

 

 

 

 

 

Submit a Comment

Your email address will not be published.

Subscribe

Select Categories