当前位置:首页 > VUE

vue实现selectable

2026-01-12 23:49:14VUE

Vue 实现可选择的列表或表格

使用 Vue 实现可选择功能可以通过 v-model 结合自定义逻辑完成。以下是一个基础实现示例,适用于列表或表格项的选择。

模板部分

<template>
  <div>
    <ul>
      <li 
        v-for="item in items" 
        :key="item.id"
        @click="toggleSelect(item)"
        :class="{ 'selected': selectedItems.includes(item) }"
      >
        {{ item.name }}
      </li>
    </ul>
    <p>已选: {{ selectedItems.map(i => i.name).join(', ') }}</p>
  </div>
</template>

脚本部分

<script>
export default {
  data() {
    return {
      items: [
        { id: 1, name: '选项1' },
        { id: 2, name: '选项2' },
        { id: 3, name: '选项3' }
      ],
      selectedItems: []
    }
  },
  methods: {
    toggleSelect(item) {
      const index = this.selectedItems.findIndex(i => i.id === item.id)
      if (index >= 0) {
        this.selectedItems.splice(index, 1)
      } else {
        this.selectedItems.push(item)
      }
    }
  }
}
</script>

样式部分

<style>
.selected {
  background-color: #42b983;
  color: white;
}
</style>

表格多选实现

对于表格场景,可以结合复选框实现批量选择:

<template>
  <table>
    <thead>
      <tr>
        <th><input type="checkbox" v-model="selectAll" @change="toggleAll"></th>
        <th>名称</th>
      </tr>
    </thead>
    <tbody>
      <tr v-for="item in items" :key="item.id">
        <td><input type="checkbox" v-model="selectedItems" :value="item"></td>
        <td>{{ item.name }}</td>
      </tr>
    </tbody>
  </table>
</template>
<script>
export default {
  data() {
    return {
      items: [
        { id: 1, name: '项目A' },
        { id: 2, name: '项目B' }
      ],
      selectedItems: [],
      selectAll: false
    }
  },
  methods: {
    toggleAll() {
      this.selectedItems = this.selectAll ? [...this.items] : []
    }
  },
  watch: {
    selectedItems(val) {
      this.selectAll = val.length === this.items.length
    }
  }
}
</script>

第三方组件方案

使用现成组件库可以快速实现高级选择功能:

  1. Element UI 的多选表格:

    <el-table
    :data="tableData"
    @selection-change="handleSelectionChange">
    <el-table-column type="selection" width="55"></el-table-column>
    </el-table>
  2. Vuetify 的选择控件:

    <v-select
    v-model="selected"
    :items="items"
    multiple
    label="选择项"
    ></v-select>

性能优化建议

对于大型数据集,建议采用虚拟滚动技术(如 vue-virtual-scroller)避免渲染所有DOM节点。选择状态应存储在独立的ID数组而非对象引用,以减少内存占用。

// 优化后的选择逻辑
toggleSelect(id) {
  const index = this.selectedIds.indexOf(id)
  index >= 0 
    ? this.selectedIds.splice(index, 1) 
    : this.selectedIds.push(id)
}

移动端适配

在移动设备上,长按手势可触发选择模式:

vue实现selectable

// 添加触摸事件监听
<li
  @touchstart="startTouch"
  @touchend="endTouch"
  @click="handleClick"
></li>

// 判断长按
startTouch() {
  this.touchTimer = setTimeout(() => {
    this.enterSelectionMode()
  }, 500)
},
endTouch() {
  clearTimeout(this.touchTimer)
}

标签: vueselectable
分享给朋友:

相关文章

vue实现签约合作弹窗

vue实现签约合作弹窗

实现签约合作弹窗的步骤 使用Vue实现签约合作弹窗可以通过组件化方式完成,结合模态框和表单交互逻辑。以下是具体实现方法: 1. 创建弹窗组件 新建一个Vue组件文件(如SignContractDia…

vue computed 实现

vue computed 实现

computed 的基本用法 在 Vue 中,computed 用于声明计算属性,这些属性基于响应式依赖进行缓存。只有当依赖发生变化时,计算属性才会重新计算。 new Vue({ data: {…

vue筛选实现

vue筛选实现

Vue 筛选实现方法 使用计算属性实现筛选 计算属性是 Vue 中实现数据筛选的常用方式。通过定义一个计算属性,可以根据输入的条件动态过滤数据。 <template> <div…

vue实现picker

vue实现picker

Vue 实现 Picker 组件的方法 在 Vue 中实现 Picker 组件可以通过多种方式完成,以下是几种常见的实现方法: 使用原生 HTML 和 CSS 实现 通过 Vue 的模板和样式绑定,…

vue实现心电图

vue实现心电图

Vue 实现心电图效果 在 Vue 中实现心电图效果通常需要结合 Canvas 或 SVG 进行动态绘图,模拟心电图数据的实时变化。以下是两种实现方式的核心思路和代码示例: 使用 Canvas 绘制…

vue 绑定实现

vue 绑定实现

Vue 绑定实现 Vue 提供了多种数据绑定的方式,包括文本插值、属性绑定、事件绑定、双向绑定等。以下是常见的绑定实现方法: 文本插值 使用双大括号 {{ }} 进行文本插值,将数据动态渲染到 DO…