当前位置:首页 > VUE

vue 实现列表多选

2026-02-17 03:07:09VUE

实现多选列表的基础方法

使用 v-model 绑定数组实现多选,结合 v-for 渲染列表项。复选框的 value 属性决定选中时存入数组的值。

<template>
  <div>
    <div v-for="item in list" :key="item.id">
      <input 
        type="checkbox" 
        :id="item.id" 
        :value="item.value" 
        v-model="selectedItems"
      >
      <label :for="item.id">{{ item.label }}</label>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      list: [
        { id: 1, value: 'apple', label: 'Apple' },
        { id: 2, value: 'banana', label: 'Banana' },
        { id: 3, value: 'orange', label: 'Orange' }
      ],
      selectedItems: []
    }
  }
}
</script>

使用计算属性处理选中状态

通过计算属性派生全选/反选状态,实现批量操作功能。

<template>
  <div>
    <input type="checkbox" v-model="selectAll"> 全选
    <div v-for="item in list" :key="item.id">
      <input 
        type="checkbox" 
        :value="item.value" 
        v-model="selectedItems"
      >
      {{ item.label }}
    </div>
  </div>
</template>

<script>
export default {
  computed: {
    selectAll: {
      get() {
        return this.selectedItems.length === this.list.length
      },
      set(value) {
        this.selectedItems = value 
          ? this.list.map(item => item.value) 
          : []
      }
    }
  }
}
</script>

结合表格实现多选

在表格场景中,可通过行选择配合复选框实现专业的多选交互。

<template>
  <table>
    <thead>
      <tr>
        <th><input type="checkbox" v-model="selectAll"></th>
        <th>名称</th>
        <th>价格</th>
      </tr>
    </thead>
    <tbody>
      <tr v-for="product in products" :key="product.id">
        <td><input type="checkbox" :value="product.id" v-model="selectedIds"></td>
        <td>{{ product.name }}</td>
        <td>{{ product.price }}</td>
      </tr>
    </tbody>
  </table>
</template>

<script>
export default {
  data() {
    return {
      products: [
        { id: 101, name: '手机', price: 5999 },
        { id: 102, name: '笔记本', price: 8999 }
      ],
      selectedIds: []
    }
  },
  computed: {
    selectAll: {
      get() {
        return this.selectedIds.length === this.products.length
      },
      set(val) {
        this.selectedIds = val 
          ? this.products.map(p => p.id) 
          : []
      }
    }
  }
}
</script>

自定义多选组件封装

封装可复用的多选组件,通过 v-model 实现双向数据绑定。

<!-- MultiSelect.vue -->
<template>
  <div class="multi-select">
    <div 
      v-for="option in options" 
      :key="option.value"
      class="select-item"
      :class="{ 'selected': isSelected(option.value) }"
      @click="toggleSelect(option.value)"
    >
      {{ option.label }}
    </div>
  </div>
</template>

<script>
export default {
  props: {
    options: Array,
    value: Array
  },
  methods: {
    isSelected(val) {
      return this.value.includes(val)
    },
    toggleSelect(val) {
      const newValue = [...this.value]
      const index = newValue.indexOf(val)

      index === -1 
        ? newValue.push(val) 
        : newValue.splice(index, 1)

      this.$emit('input', newValue)
    }
  }
}
</script>

<style>
.select-item {
  padding: 8px;
  border: 1px solid #eee;
  cursor: pointer;
}
.selected {
  background-color: #42b983;
  color: white;
}
</style>

使用第三方库增强功能

对于复杂需求,可引入如 vue-multiselect 等专业组件库。

安装依赖:

npm install vue-multiselect

基础用法示例:

vue 实现列表多选

<template>
  <multiselect
    v-model="selected"
    :options="options"
    :multiple="true"
    :close-on-select="false"
    placeholder="选择项目"
  ></multiselect>
</template>

<script>
import Multiselect from 'vue-multiselect'
export default {
  components: { Multiselect },
  data() {
    return {
      selected: [],
      options: ['选项1', '选项2', '选项3']
    }
  }
}
</script>

标签: 多选列表
分享给朋友:

相关文章

vue实现奇偶列表

vue实现奇偶列表

Vue实现奇偶列表样式 在Vue中实现列表项的奇偶不同样式,可以通过多种方式实现。以下是几种常见方法: 使用v-for和动态class绑定 通过v-for循环生成列表时,利用索引值判断奇偶性并绑定…

vue 实现多选功能

vue 实现多选功能

Vue 实现多选功能的方法 使用 v-model 绑定数组 在 Vue 中,可以通过 v-model 绑定一个数组来实现多选功能。适用于复选框组(checkbox)或下拉多选(select multi…

vue实现影片列表

vue实现影片列表

Vue 实现影片列表 使用 Vue CLI 创建项目 确保已安装 Vue CLI,通过以下命令创建新项目: vue create movie-list cd movie-list 安装依赖 根据…

weex  vue 实现列表

weex vue 实现列表

weex + Vue 实现列表的方法 使用<list>和<cell>组件 在weex中,列表通常通过<list>和<cell>组件实现。<list…

vue实现列表过渡

vue实现列表过渡

vue实现列表过渡的方法 在Vue中,可以使用内置的<transition-group>组件来实现列表项的过渡效果。该组件专门为动态列表设计,能够处理元素的添加、删除和顺序变化。 基本用…

vue实现纵向列表

vue实现纵向列表

实现纵向列表的基本方法 在Vue中实现纵向列表可以通过v-for指令结合数组数据渲染。核心是利用循环遍历数据生成列表项,并设置CSS控制纵向排列。 <template> <di…