当前位置:首页 > 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>

样式部分

vue实现selectable

<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 的多选表格:

    vue实现selectable

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

移动端适配

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

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

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

标签: vueselectable
分享给朋友:

相关文章

vue实现atm

vue实现atm

Vue实现ATM机模拟功能 使用Vue.js实现ATM机模拟功能需要结合组件化开发、状态管理以及用户交互逻辑。以下是关键实现步骤: 核心组件结构 创建以下Vue组件构建ATM界面: Car…

vue实现横向导航

vue实现横向导航

vue实现横向导航的方法 使用Flex布局实现横向导航 在Vue中可以通过Flex布局快速实现横向导航。创建一个组件,使用display: flex和flex-direction: row属性使导航项…

vue如何实现到期提醒

vue如何实现到期提醒

实现 Vue 到期提醒功能 使用计算属性计算剩余时间 在 Vue 组件中定义一个计算属性,计算目标日期与当前日期的差值。通过 Date 对象获取时间戳并计算剩余天数或小时数。 computed:…

vue实现安全免登录

vue实现安全免登录

Vue 实现安全免登录的方案 免登录功能通常通过持久化认证状态(如 Token)实现,但需兼顾安全性。以下是基于 Vue 的常见实现方案: 基于 Token 的免登录 登录成功后存储 Token…

vue实现展开与收起

vue实现展开与收起

Vue 实现展开与收起功能 在 Vue 中实现展开与收起功能可以通过多种方式完成,以下是一些常见的方法: 使用 v-show 或 v-if 控制显示 通过 Vue 的指令 v-show 或 v-i…

vue实现继承语法糖

vue实现继承语法糖

在Vue中实现继承可以通过多种方式模拟类继承的效果,以下是常见的几种方法: 使用mixins实现逻辑复用 通过Vue的mixins机制共享组件选项,达到类似继承的效果: const Bas…