当前位置:首页 > VUE

vue 多选实现思路

2026-01-18 15:43:11VUE

多选框组件实现

使用Vue内置的v-model指令绑定数组类型数据,当选中多个选项时,会自动将值添加到数组中

<template>
  <div>
    <label v-for="option in options" :key="option.value">
      <input 
        type="checkbox" 
        v-model="selectedValues" 
        :value="option.value"
      >
      {{ option.label }}
    </label>
  </div>
</template>

<script>
export default {
  data() {
    return {
      options: [
        { value: 'apple', label: '苹果' },
        { value: 'banana', label: '香蕉' },
        { value: 'orange', label: '橙子' }
      ],
      selectedValues: []
    }
  }
}
</script>

自定义多选组件封装

创建可复用的多选组件,支持自定义样式和功能扩展

vue 多选实现思路

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

<script>
export default {
  props: {
    options: Array,
    value: Array
  },
  methods: {
    toggleSelect(item) {
      const selected = [...this.value]
      const index = selected.indexOf(item.value)

      index === -1 
        ? selected.push(item.value) 
        : selected.splice(index, 1)

      this.$emit('input', selected)
    },
    isSelected(item) {
      return this.value.includes(item.value)
    }
  }
}
</script>

结合Element UI实现

使用Element UI的el-checkbox-group组件快速实现多选功能

vue 多选实现思路

<template>
  <el-checkbox-group v-model="checkedCities">
    <el-checkbox 
      v-for="city in cities" 
      :label="city" 
      :key="city"
    >
      {{ city }}
    </el-checkbox>
  </el-checkbox-group>
</template>

<script>
export default {
  data() {
    return {
      cities: ['上海', '北京', '广州', '深圳'],
      checkedCities: []
    }
  }
}
</script>

多选表格实现

在表格中实现多选功能,通常需要配合全选/反选操作

<template>
  <el-table
    ref="multipleTable"
    :data="tableData"
    @selection-change="handleSelectionChange"
  >
    <el-table-column type="selection" width="55"></el-table-column>
    <el-table-column prop="date" label="日期"></el-table-column>
    <el-table-column prop="name" label="姓名"></el-table-column>
  </el-table>
</template>

<script>
export default {
  data() {
    return {
      tableData: [...],
      multipleSelection: []
    }
  },
  methods: {
    handleSelectionChange(val) {
      this.multipleSelection = val
    },
    toggleSelection(rows) {
      rows.forEach(row => {
        this.$refs.multipleTable.toggleRowSelection(row)
      })
    }
  }
}
</script>

多选搜索过滤

实现可搜索的多选功能,提升用户体验

<template>
  <div>
    <el-input 
      v-model="searchText" 
      placeholder="搜索选项"
    ></el-input>

    <el-checkbox-group v-model="selectedOptions">
      <el-checkbox 
        v-for="item in filteredOptions" 
        :label="item.value" 
        :key="item.value"
      >
        {{ item.label }}
      </el-checkbox>
    </el-checkbox-group>
  </div>
</template>

<script>
export default {
  data() {
    return {
      options: [...],
      selectedOptions: [],
      searchText: ''
    }
  },
  computed: {
    filteredOptions() {
      return this.options.filter(option => 
        option.label.toLowerCase().includes(this.searchText.toLowerCase())
      )
    }
  }
}
</script>

标签: 多选思路
分享给朋友:

相关文章

vue实现列表多选

vue实现列表多选

Vue 实现列表多选的方法 使用 v-model 绑定数组 通过 v-model 绑定一个数组,可以轻松实现多选功能。当复选框被选中或取消选中时,数组会自动更新。 <template>…

vue实现tab多选

vue实现tab多选

Vue 实现 Tab 多选功能 在 Vue 中实现 Tab 多选功能可以通过以下方法完成。假设需要实现一个可多选的 Tab 组件,用户可以通过点击 Tab 来选择多个选项。 数据结构和状态管理 定义…

vue实现单选多选

vue实现单选多选

Vue 实现单选功能 使用 v-model 绑定单选按钮的值,确保同一组单选按钮的 name 属性相同。 <template> <div> <lab…

vue tree 实现思路

vue tree 实现思路

Vue Tree 组件实现思路 数据结构设计 树形组件的数据通常采用嵌套结构,每个节点包含 label、children 等属性。例如: const treeData = [ { lab…

vue实现全选 多选

vue实现全选 多选

Vue实现全选与多选功能 在Vue中实现全选和多选功能通常涉及数据绑定、事件处理和状态管理。以下是两种常见的实现方式: 基于v-model的双向绑定 使用v-model绑定复选框组,结合计算…

vue实现多选框

vue实现多选框

基础实现 使用 v-model 绑定数组实现多选功能。在 Vue 中,多选框的 value 属性会随选中状态自动更新到绑定的数组。 <template> <div>…