当前位置:首页 > VUE

vue实现多选下拉

2026-01-16 21:38:23VUE

Vue 实现多选下拉

使用 Vue 实现多选下拉可以通过原生 HTML <select> 元素结合 Vue 的双向绑定,或者使用第三方组件库如 Element UI、Vuetify 等。以下是几种实现方式:

原生 HTML <select> 实现多选

通过设置 <select>multiple 属性,结合 Vue 的 v-model 实现多选下拉。

vue实现多选下拉

<template>
  <div>
    <select v-model="selectedItems" multiple>
      <option v-for="item in items" :key="item.value" :value="item.value">
        {{ item.text }}
      </option>
    </select>
    <p>Selected items: {{ selectedItems }}</p>
  </div>
</template>

<script>
export default {
  data() {
    return {
      items: [
        { value: '1', text: 'Option 1' },
        { value: '2', text: 'Option 2' },
        { value: '3', text: 'Option 3' },
      ],
      selectedItems: [],
    };
  },
};
</script>

使用 Element UI 的 el-select 组件

Element UI 提供了功能丰富的多选下拉组件 el-select,支持多选、搜索、自定义模板等功能。

vue实现多选下拉

<template>
  <div>
    <el-select v-model="selectedItems" multiple placeholder="Select">
      <el-option
        v-for="item in items"
        :key="item.value"
        :label="item.text"
        :value="item.value"
      />
    </el-select>
    <p>Selected items: {{ selectedItems }}</p>
  </div>
</template>

<script>
export default {
  data() {
    return {
      items: [
        { value: '1', text: 'Option 1' },
        { value: '2', text: 'Option 2' },
        { value: '3', text: 'Option 3' },
      ],
      selectedItems: [],
    };
  },
};
</script>

使用 Vuetify 的 v-select 组件

Vuetify 的 v-select 组件也支持多选功能,适合 Material Design 风格的项目。

<template>
  <div>
    <v-select
      v-model="selectedItems"
      :items="items"
      multiple
      label="Select"
    ></v-select>
    <p>Selected items: {{ selectedItems }}</p>
  </div>
</template>

<script>
export default {
  data() {
    return {
      items: [
        { value: '1', text: 'Option 1' },
        { value: '2', text: 'Option 2' },
        { value: '3', text: 'Option 3' },
      ],
      selectedItems: [],
    };
  },
};
</script>

自定义多选下拉组件

如果需要完全自定义的多选下拉功能,可以通过 Vue 实现一个自定义组件。

<template>
  <div class="custom-multiselect">
    <div class="selected-items" @click="toggleDropdown">
      <span v-if="selectedItems.length === 0">Select options</span>
      <span v-else>
        {{ selectedItems.map(item => item.text).join(', ') }}
      </span>
    </div>
    <div v-if="isOpen" class="dropdown">
      <div
        v-for="item in items"
        :key="item.value"
        @click="toggleItem(item)"
        :class="{ selected: isSelected(item) }"
      >
        {{ item.text }}
      </div>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      items: [
        { value: '1', text: 'Option 1' },
        { value: '2', text: 'Option 2' },
        { value: '3', text: 'Option 3' },
      ],
      selectedItems: [],
      isOpen: false,
    };
  },
  methods: {
    toggleDropdown() {
      this.isOpen = !this.isOpen;
    },
    toggleItem(item) {
      const index = this.selectedItems.findIndex(
        selected => selected.value === item.value
      );
      if (index === -1) {
        this.selectedItems.push(item);
      } else {
        this.selectedItems.splice(index, 1);
      }
    },
    isSelected(item) {
      return this.selectedItems.some(
        selected => selected.value === item.value
      );
    },
  },
};
</script>

<style>
.custom-multiselect {
  position: relative;
  width: 200px;
}
.selected-items {
  border: 1px solid #ccc;
  padding: 8px;
  cursor: pointer;
}
.dropdown {
  position: absolute;
  width: 100%;
  border: 1px solid #ccc;
  background: white;
  z-index: 100;
}
.dropdown div {
  padding: 8px;
  cursor: pointer;
}
.dropdown div.selected {
  background: #eee;
}
</style>

以上方法可以根据项目需求选择合适的方式实现多选下拉功能。原生 HTML <select> 适合简单需求,而第三方组件库或自定义组件适合复杂场景。

标签: 多选vue
分享给朋友:

相关文章

vue实现发送值接受值

vue实现发送值接受值

Vue 实现父子组件传值 父组件向子组件传值(props) 父组件通过 v-bind 或简写 : 向子组件传递数据。子组件通过 props 接收数据。 父组件代码: <template&g…

vue datepicker 实现

vue datepicker 实现

实现 Vue Datepicker 的基本方法 在 Vue 项目中实现日期选择功能,可以使用第三方库如 vue-datepicker 或 v-calendar。以下是两种常见实现方式: 安装 vue…

vue树形实现

vue树形实现

Vue 树形组件实现方法 使用递归组件实现树形结构 递归组件是 Vue 中实现树形结构的常见方法。通过组件调用自身的方式,可以轻松构建多层嵌套的树形结构。 <template> &l…

vue筛选实现

vue筛选实现

Vue 筛选实现方法 使用计算属性实现筛选 计算属性是 Vue 中实现数据筛选的常用方式。通过定义一个计算属性,可以根据输入的条件动态过滤数据。 <template> <div…

vue nexttrick实现

vue nexttrick实现

Vue 中 nextTick 的实现原理 nextTick 是 Vue 提供的一个异步方法,用于在下次 DOM 更新循环结束之后执行延迟回调。它利用了 JavaScript 的事件循环机制,确保回调在…

用vue实现全选

用vue实现全选

实现全选功能的基本思路 在Vue中实现全选功能通常涉及以下核心逻辑:通过一个布尔值控制全选状态,遍历子选项并同步其选中状态。以下是具体实现方法。 使用v-model绑定全选状态 在模板中,使用v-m…