npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

select2-vue

v1.0.8

Published

A Vue 3 component that integrates Select2

Downloads

488

Readme

Select2 Component for Vue

npm version

select2-vue is a Vue.js wrapper component for the popular jQuery Select2 library, providing you with a flexible and customizable select box with support for searching, tagging, remote data sources, and more. The package makes it easy to integrate the powerful features of Select2 into your Vue applications.

Features

  • Support for all Select2 features (search, tagging, remote data, infinite scrolling).
  • Fully reactive with Vue.js.
  • Easy integration with existing Select2 options.
  • Configurable using props for quick customization.
  • Emits native Select2 events for more control.

Installation

Install the package via npm:

npm install select2-vue --save

Or via yarn:

yarn add select2-vue

Basic Usage

Import and register the select2-vue component, using the <script setup> syntax for simplicity.

Example: Basic Select

<template>
    <select2-vue :options="selectOptions" v-model="selectedValue" placeholder="Select an option" />
</template>

<script setup>
import { ref } from 'vue';
import Select2 from 'select2-vue';

const selectedValue = ref(null);
const selectOptions = [
    { id: 1, text: 'Option 1' },
    { id: 2, text: 'Option 2' },
    { id: 3, text: 'Option 3' },
];
</script>

Styles

To ensure proper styling, include the Select2 CSS file in your project:

import 'select2-vue/dist/select2-vue.css';

Props

The select2-vue component accepts the following props for customization:

| Prop | Type | Default | Description | | ------------- | --------- | ------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- | | options | Array | [] | Array of select options. Each option should be an object with id and text properties. | | modelValue | Any | null | The currently selected value (can be bound with v-model). | | placeholder | String | "" | Placeholder text displayed in the select box. | | multiple | Boolean | false | Enables multiple selection. | | disabled | Boolean | false | Disables the select box. | | config | Object | {} | Custom settings to pass directly to the underlying Select2 instance. See Select2 documentation for available options. |

Events

The component emits several useful events for better control over the Select2 instance:

| Event | Description | | ------------------ | ----------------------------------------------------------------------------------- | | select2:select | Fired when an option is selected. The selected item is passed as the event payload. | | select2:unselect | Fired when an option is deselected (in multiple mode). | | select2:opening | Triggered when the dropdown is about to open. | | select2:closing | Triggered when the dropdown is about to close. | | change | Fired when the value changes, passing the new value. |

Example:

<template>
    <select2-vue
        :options="selectOptions"
        v-model="selectedValue"
        @select2:select="handleSelect"
        @select2:unselect="handleUnselect"
    />
</template>

<script setup>
import { ref } from 'vue';

const selectedValue = ref(null);
const selectOptions = [
    { id: 1, text: 'Option 1' },
    { id: 2, text: 'Option 2' },
    { id: 3, text: 'Option 3' },
];

function handleSelect(selected) {
    console.log('Option selected:', selected);
}

function handleUnselect(unselected) {
    console.log('Option unselected:', unselected);
}
</script>

Example: Using Remote Data

You can fetch data dynamically from a remote API using the ajax option:

<template>
    <select2-vue :settings="select2Settings" v-model="selectedValue" />
</template>

<script setup>
import { ref } from 'vue';

const selectedValue = ref(null);
const select2Settings = {
    ajax: {
        url: 'https://api.example.com/data',
        dataType: 'json',
        processResults: function (data) {
            return {
                results: data.items.map((item) => ({
                    id: item.id,
                    text: item.name,
                })),
            };
        },
    },
};
</script>

Example: Multiple Selection with Tagging

Here’s an example where you can add new tags or select multiple values:

<template>
    <select2-vue :settings="select2Settings" v-model="selectedTags" placeholder="Select or add tags" />
</template>

<script setup>
import { ref } from 'vue';

const selectedTags = ref([]);
const select2Settings = {
    tags: true,
    tokenSeparators: [',', ' '],
};
</script>

License

This project is licensed under the MIT License. See the LICENSE file for more details.