当前位置:首页 > VUE

vue 多选实现思路

2026-02-19 08:06:47VUE

多选框基础实现

使用v-model绑定数组类型数据,配合checkboxvalue属性实现多选功能。当勾选时,对应value会被添加到数组中;取消勾选时从数组中移除。

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

<script>
export default {
  data() {
    return {
      options: [
        { value: 'A', label: '选项A' },
        { value: 'B', label: '选项B' },
        { value: 'C', label: '选项C' }
      ],
      selectedValues: []
    }
  }
}
</script>

自定义多选组件封装

封装可复用的多选框组件,支持动态选项和自定义样式。通过props接收选项列表,通过$emit触发选择变化事件。

vue 多选实现思路

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

<script>
export default {
  props: {
    options: {
      type: Array,
      required: true
    },
    value: {
      type: Array,
      default: () => []
    }
  },
  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 scoped>
.select-item {
  padding: 8px 12px;
  border: 1px solid #ddd;
  cursor: pointer;
  margin-right: 5px;
  display: inline-block;
}
.select-item.active {
  background-color: #409eff;
  color: white;
}
</style>

全选/反选功能实现

添加全选控制逻辑,通过计算属性判断是否全选,并提供全选/取消全选的方法。

vue 多选实现思路

computed: {
  isAllSelected: {
    get() {
      return this.selectedValues.length === this.options.length
    },
    set(val) {
      this.selectedValues = val 
        ? this.options.map(item => item.value) 
        : []
    }
  }
}

多选表格实现

在表格中实现多选行功能,结合el-table等UI组件库使用时需要注意行数据的唯一标识。

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

<script>
export default {
  methods: {
    handleSelectionChange(val) {
      this.selectedRows = val
    },
    toggleSelection(rows) {
      rows.forEach(row => {
        this.$refs.multipleTable.toggleRowSelection(row)
      })
    }
  }
}
</script>

多选数据验证

对多选结果进行验证,确保至少选择N项或符合特定条件。

validateSelection() {
  if (this.selectedValues.length < 2) {
    this.$message.error('请至少选择两项')
    return false
  }
  return true
}

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

相关文章

jquery 多选

jquery 多选

jQuery 多选实现方法 使用 prop() 方法实现多选 通过 prop() 方法可以批量设置多个复选框的选中状态。以下是一个示例代码: $('input[type="checkbox"]').…

vue实现tab多选

vue实现tab多选

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

vue实现active多选

vue实现active多选

实现多选高亮(active)效果 在Vue中实现多选高亮效果可以通过以下方式完成: 使用v-for和v-bind:class 通过动态绑定class实现选中状态切换,适用于列表项多选场景: <…

vue 实现按钮多选

vue 实现按钮多选

实现按钮多选功能 在Vue中实现按钮多选功能可以通过以下方法完成,主要依赖v-model绑定数组和v-for动态渲染按钮。 数据绑定与渲染 定义一个数组selectedItems用于存储选中的按钮值…

vue实现多选日历

vue实现多选日历

Vue 实现多选日历 使用 Vue 实现多选日历功能可以借助现有的日历库或手动构建。以下是两种常见方法: 使用第三方库(如 V-Calendar) 安装 V-Calendar 库: npm ins…

vue实现按钮多选

vue实现按钮多选

实现按钮多选功能 在Vue中实现按钮多选功能,可以通过以下方法完成。这里提供两种常见的实现方式:基于数组管理和使用v-model绑定。 基于数组管理选中状态 定义一个数组来存储选中的按钮值,…