Vue Select2 Component

0.4.7 · maintenance · verified Sun Apr 19

The `v-select2-component` package, currently at version 0.4.7, provides a Vue 2 wrapper for the popular jQuery Select2 plugin. It enables developers to integrate enhanced `<select>` elements with search, tagging, and remote data capabilities into their Vue 2 applications. The library directly leverages jQuery and the Select2 library, meaning these are required dependencies and must be loaded in the application. As indicated by its reference to Vue CLI v2.9.1 and a pre-1.0 version number, the project appears to be in a maintenance state, primarily supporting Vue 2 applications, with no active development for Vue 3 or modern front-end tooling. Its primary differentiator is its direct, unopinionated wrapper approach for Select2 within the Vue 2 ecosystem.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates basic usage of `v-select2-component` with `v-model`, options, custom settings, and event handling for both single and multiple selections.

<template>
  <div id="app">
    <h3>v-select2-component Quickstart</h3>
    <Select2 v-model="myValue" :options="myOptions" :settings="select2Settings" @change="myChangeEvent" @select="mySelectEvent" />
    <p>Selected Value: <strong>{{ myValue }}</strong></p>
    <button @click="myValue = 'op2'">Set to 'op2'</button>
    <button @click="myValue = { id: 'op4', text: 'Option 4' }">Set to 'Option 4' (object)</button>
    <hr />
    <p>Multiple Select Example:</p>
    <Select2 v-model="multiValue" :options="multiOptions" :settings="{ multiple: true, placeholder: 'Select multiple options' }" @change="multiChangeEvent" />
    <p>Multiple Selected Values: <strong>{{ multiValue }}</strong></p>
  </div>
</template>

<script lang="ts">
import Vue from 'vue';
import Select2 from 'v-select2-component';
// IMPORTANT: Ensure jQuery and Select2 CSS/JS are loaded globally or via your build process.
// For example, in your main.ts or main.js:
// import 'jquery';
// import 'select2';
// import 'select2/dist/css/select2.min.css';

interface Select2Option {
  id: string | number;
  text: string;
}

export default Vue.extend({
  name: 'App',
  components: {
    Select2
  },
  data() {
    return {
      myValue: 'op1' as string | Select2Option,
      myOptions: ['op1', 'op2', 'op3', { id: 'op4', text: 'Option 4' }] as Array<string | Select2Option>,
      select2Settings: {
        dropdownParent: 'body',
        width: '100%',
        minimumResultsForSearch: 5
      },
      multiValue: ['itemA'] as Array<string | number>,
      multiOptions: [
        { id: 'itemA', text: 'Item A' },
        { id: 'itemB', text: 'Item B' },
        { id: 'itemC', text: 'Item C' }
      ] as Array<Select2Option>
    };
  },
  methods: {
    myChangeEvent(val: string | Select2Option | Array<string | Select2Option>): void {
      console.log('Single select change event:', val);
    },
    mySelectEvent(option: { id: string | number; text: string }): void {
      console.log('Single select option selected:', option);
    },
    multiChangeEvent(val: Array<string | Select2Option>): void {
      console.log('Multiple select change event:', val);
    }
  }
});
</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>

view raw JSON →