当前位置:首页 > VUE

vue列表多选实现思路

2026-01-20 21:25:43VUE

Vue列表多选实现思路

基础实现:v-model绑定数组

通过v-model绑定一个数组,配合checkboxvalue属性实现多选。选中时自动将值添加到数组,取消选中时从数组中移除。

vue列表多选实现思路

<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>

自定义组件实现

封装可复用的多选列表组件,通过props接收选项列表,通过emit事件返回选中值。

vue列表多选实现思路

<template>
  <div>
    <div v-for="item in options" :key="item.id">
      <input 
        type="checkbox" 
        :id="item.id" 
        :value="item.value" 
        v-model="internalSelected"
        @change="handleChange"
      >
      <label :for="item.id">{{ item.label }}</label>
    </div>
  </div>
</template>

<script>
export default {
  props: {
    options: Array,
    value: Array
  },
  data() {
    return {
      internalSelected: this.value || []
    }
  },
  methods: {
    handleChange() {
      this.$emit('input', this.internalSelected)
    }
  }
}
</script>

表格行多选

结合表格实现行选择功能,通常需要配合selection样式和全选功能。

<template>
  <div>
    <table>
      <thead>
        <tr>
          <th><input type="checkbox" v-model="selectAll"></th>
          <th>Name</th>
          <th>Age</th>
        </tr>
      </thead>
      <tbody>
        <tr v-for="(item, index) in tableData" :key="index">
          <td><input type="checkbox" v-model="selectedRows" :value="item"></td>
          <td>{{ item.name }}</td>
          <td>{{ item.age }}</td>
        </tr>
      </tbody>
    </table>
  </div>
</template>

<script>
export default {
  data() {
    return {
      tableData: [
        { name: 'John', age: 25 },
        { name: 'Jane', age: 22 },
        { name: 'Doe', age: 30 }
      ],
      selectedRows: [],
      selectAll: false
    }
  },
  watch: {
    selectAll(val) {
      this.selectedRows = val ? [...this.tableData] : []
    }
  }
}
</script>

虚拟列表优化

对于大数据量列表,结合虚拟滚动技术优化性能,只渲染可视区域内的选项。

<template>
  <div class="virtual-list" @scroll="handleScroll">
    <div class="list-phantom" :style="{ height: totalHeight + 'px' }"></div>
    <div class="list-content" :style="{ transform: `translateY(${startOffset}px)` }">
      <div 
        v-for="item in visibleData" 
        :key="item.id" 
        class="list-item"
      >
        <input type="checkbox" v-model="selectedItems" :value="item.value">
        <span>{{ item.label }}</span>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  props: {
    listData: Array,
    itemHeight: Number
  },
  data() {
    return {
      startIndex: 0,
      endIndex: 20,
      screenHeight: 500,
      selectedItems: []
    }
  },
  computed: {
    totalHeight() {
      return this.listData.length * this.itemHeight
    },
    visibleData() {
      return this.listData.slice(this.startIndex, this.endIndex)
    },
    startOffset() {
      return this.startIndex * this.itemHeight
    }
  },
  methods: {
    handleScroll(e) {
      const scrollTop = e.target.scrollTop
      this.startIndex = Math.floor(scrollTop / this.itemHeight)
      this.endIndex = this.startIndex + Math.ceil(this.screenHeight / this.itemHeight)
    }
  }
}
</script>

注意事项

  1. 大数据量情况下避免直接使用v-for渲染所有选项,考虑分页或虚拟滚动
  2. 选中状态管理应使用Vue响应式数据,避免直接操作DOM
  3. 自定义组件时注意实现v-model的双向绑定协议
  4. 移动端适配可能需要特殊处理触摸事件和选择样式

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

相关文章

vue多选实现

vue多选实现

Vue多选实现方法 在Vue中实现多选功能可以通过多种方式完成,以下是常见的几种方法: 使用v-model绑定数组 通过v-model绑定一个数组,当复选框被选中时,值会自动添加到数组中。 &…

Vue实现思路

Vue实现思路

Vue 实现思路 Vue 是一个渐进式 JavaScript 框架,适用于构建用户界面。其核心思想是数据驱动和组件化开发。以下是 Vue 实现的主要思路和方法: 数据驱动与响应式 Vue 通过数据…

vue路由实现思路

vue路由实现思路

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

jquery 多选

jquery 多选

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

vue实现div多选

vue实现div多选

Vue 实现 Div 多选功能 基础实现思路 通过 Vue 的数据绑定和事件处理,可以轻松实现 div 的多选功能。核心是维护一个数组来存储选中的 div 的标识符,并通过 v-bind 和 v-on…

js 多选 实现

js 多选 实现

实现多选的 JavaScript 方法 使用复选框(Checkbox)实现多选 通过 HTML 的 checkbox 元素配合 JavaScript 实现多选功能。用户可以选择多个选项,提交时获取所有…