当前位置:首页 > 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 实现弹幕

vue 实现弹幕

vue 实现弹幕的方法 使用 CSS 动画和动态渲染 在 Vue 中实现弹幕效果,可以通过动态渲染弹幕元素并结合 CSS 动画实现。以下是具体步骤: 数据准备 创建一个数组存储弹幕数据,每条弹幕…

vue实现高亮

vue实现高亮

Vue 实现文本高亮的方法 在 Vue 中实现文本高亮可以通过多种方式完成,以下是几种常见的方法: 方法一:使用 v-html 指令 通过 v-html 指令可以动态插入 HTML 内容,将需要高…

分页实现vue

分页实现vue

分页实现(Vue) 在Vue中实现分页功能通常需要结合前端分页逻辑和后端API支持。以下是两种常见的实现方式: 前端分页实现 适用于数据量较小的情况,直接在客户端完成分页逻辑。 <tem…

vue实现frame

vue实现frame

Vue 中实现 iframe 的方法 在 Vue 中可以通过直接使用 <iframe> 标签或动态绑定 src 属性来实现 iframe 功能。 基本用法 <template&g…

vue实现toast

vue实现toast

Vue 实现 Toast 的方法 使用第三方库(推荐) 对于快速实现 Toast 功能,推荐使用成熟的第三方库如 vue-toastification 或 vant 的 Toast 组件。 安装…

vue如何实现

vue如何实现

Vue 实现方法 在 Vue 中实现功能通常涉及组件化开发、状态管理和响应式数据绑定。以下是几种常见的实现方式: 组件化开发 使用 Vue 的单文件组件(.vue 文件)结构,将 UI 拆分为可复用…