Vue.js 轻量级多选择/单选择组件 - vue-multi-select

Vue.js 轻量级多选择/单选择组件 - vue-multi-select
插件名称 vue-multi-select
发布时间 2020年10月9日
插件作者 IneoO

Vue multi-select是用于现代Vue.js开发的轻量级高级多/单选择组件。

变更日志:

v4.6.0(2020年12月23日)

  • 设置可能更改空数据的标签

安装和下载:

# Yarn
$ yarn add vue-multi-select

# NPM
$ npm install vue-multi-select --save

基本用法:

1.导入组件和样式表。

import vueMultiSelect from 'vue-multi-select';
import 'vue-multi-select/dist/lib/vue-multi-select.css';

2.创建一个基本的单选组件。

<template>
  <div>
    <vue-multi-select
      ref="multiSelect"
      v-model="values"
      search
      historyButton
      :options="options"
      :filters="filters"
      :btnLabel="btnLabel"
      @open="open"
      @close="close"
      :selectOptions="data">
      <template v-slot:option="{option}">
        <input type="checkbox" :checked="option.selected"/>
        <span>{{option.name}}</span>
      </template>
    </vue-multi-select>
    <button
      @click="openManually">
      Open manually
    </button>
  </div>
</template>
export default {
  data() {
    return {
      btnLabel: values => `A simple vue multi select (${values.length})`
      name: 'first group',
      values: [],
      data: [{
        name: 'first group',
        list: [
          { name: '0' },
          { name: '2' },
          { name: '3' },
          { name: '8' },
          { name: '9' },
          { name: '11' },
          { name: '13' },
          { name: '14' },
          { name: '15' },
          { name: '18' },
        ],
      }, {
        name: 'second group',
        list: [
          { name: '21' },
          { name: '22' },
          { name: '24' },
          { name: '27' },
          { name: '28' },
          { name: '29' },
          { name: '31' },
          { name: '33' },
          { name: '35' },
          { name: '39' },
        ],
      }],
      filters: [{
        nameAll: 'Select all',
        nameNotAll: 'Deselect all',
        func() {
          return true;
        },
      }, {
        nameAll: 'select <= 10',
        nameNotAll: 'Deselect <= 10',
        func(elem) {
          return elem.name <= 10;
        },
      }, {
        nameAll: 'Select contains 2',
        nameNotAll: 'Deselect contains 2',
        func(elem) {
          return elem.name.indexOf('2') !== -1;
        },
      }],
      options: {
        multi: true,
        groups: true,
      },
    };
  },
  methods: {
    openManually() {
      this.$refs.multiSelect.openMultiSelect();
    },
    open() {
      console.log('open');
    },
    close() {
      console.log('close');
    },
  },
  components: {
    vueMultiSelect,
  },
};

3.创建一个基本的多选组件。

<template>
  <div>
    <vue-multi-select
      v-model="values"
      search
      historyButton
      :filters="filters"
      :options="options"
      :selectOptions="data"/>
      <button
        @click="reloadFunction" >
        Change v-model
      </button>
  </div>
</template>
export default {
  data() {
    return {
      name: 'first group',
      values: [
        { label: '2' },
        { label: '3' },
      ],
      data: [{
        title: 'part one',
        elements: [
          { label: '0', disabled: true },
          { label: '2' },
          { label: '3' },
          { label: '8' },
          { label: '9' },
          { label: '11' },
          { label: '13' },
          { label: '14' },
          { label: '15' },
          { label: '18' },
        ],
      }, {
        title: 'part two',
        elements: [
          { label: '23' },
          { label: '25' },
          { label: '31' },
          { label: '42' },
          { label: '56' },
          { label: '76' },
          { label: '82' },
          { label: '42' },
          { label: '13' },
          { label: '21' },
        ],
      }],
      filters: [{
        nameAll: 'Select all',
        nameNotAll: 'Deselect all',
        func() {
          return true;
        },
      }],
      options: {
        multi: true,
        groups: true,
        labelName: 'label',
        labelList: 'elements',
        groupName: 'title',
        cssSelected: option => (option.selected ? { 'background-color': '#5764c6' } : ''),
      },
    };
  },
  methods: {
    reloadFunction() {
      this.values = [
        { label: '2' },
        { label: '3' },
      ];
    },
  },
  components: {
    vueMultiSelect,
  },
};

4.所有可能的道具。

options: {
  type: Object,
  default: () => ({}),
},
filters: {
  type: Array,
  default: () => [],
},
selectOptions: {
  type: Array,
  default: () => [],
},
eventName: {
  type: String,
  default: 'selectionChanged',
},
reloadInit: {
  type: Boolean,
  default: false,
},
value: {
  type: Array,
  default: () => ([]),
},
btnClass: {
  type: String,
  default: '',
},
popoverClass: {
  type: String,
  default: '',
},
btnLabel: {
  type: Function,
  default: () => ('multi-select'),
},
search: {
  type: Boolean,
  default: false,
},
searchPlaceholder: {
  type: String,
  default: 'Search...',
},
historyButton: {
  type: Boolean,
  default: false,
},
historyButtonText: {
  type: String,
  default: '↶',
},
position: {
  type: String,
  default: 'bottom-left',
},
disabled: {
  type: Boolean,
  default: false,
},
disabledUnSelect: {
  type: Boolean,
  default: false,
},