当前位置:首页 > 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 ssr实现思路

vue ssr实现思路

Vue SSR 实现思路 Vue SSR(Server-Side Rendering)的核心目标是在服务端生成完整的 HTML 页面,提升首屏加载速度和 SEO 友好性。以下是关键实现思路: 基础…

vue实现多选联动

vue实现多选联动

实现多选联动的基本思路 在Vue中实现多选联动功能,通常需要利用v-model绑定数据、监听选项变化以及动态更新关联选项。以下是几种常见的实现方式: 基于v-model和计算属性 通过v-model…

vue实现考试多选功能

vue实现考试多选功能

Vue实现考试多选功能 数据绑定与选项渲染 使用v-model绑定用户选择的答案数组,结合v-for循环渲染选项列表。示例代码: <template> <div>…

vue路由实现思路

vue路由实现思路

Vue 路由实现思路 Vue 路由的核心是通过 Vue Router 库实现单页应用(SPA)的页面切换,避免整页刷新。以下是关键实现思路: 安装与引入 Vue Router 通过 npm 或 y…

elementui多选

elementui多选

ElementUI 多选组件使用指南 ElementUI 提供了 el-select 组件支持多选功能,通过设置 multiple 属性即可启用。以下是具体实现方法和常见场景示例: 基础多选实现…

vue实现多选答题

vue实现多选答题

实现多选答题功能 在Vue中实现多选答题功能,可以通过以下方法完成: 数据准备 定义一个questions数组,每个问题包含题目和选项,选项是一个对象数组,每个选项有文本和是否被选中的状态。 da…