当前位置:首页 > VUE

vue 穿梭框实现

2026-02-19 22:27:42VUE

Vue 穿梭框实现方法

穿梭框(Transfer)是一种常见的前端组件,用于在两个列表之间移动数据项。以下是几种实现 Vue 穿梭框的方法:

使用 Element UI 的 Transfer 组件

Element UI 提供了现成的穿梭框组件,可以直接使用:

<template>
  <el-transfer
    v-model="selectedItems"
    :data="items"
    :titles="['Source', 'Target']"
    @change="handleChange"
  ></el-transfer>
</template>

<script>
export default {
  data() {
    return {
      items: [
        { key: 1, label: 'Item 1' },
        { key: 2, label: 'Item 2' },
        { key: 3, label: 'Item 3' }
      ],
      selectedItems: []
    }
  },
  methods: {
    handleChange(value, direction, movedKeys) {
      console.log(value, direction, movedKeys)
    }
  }
}
</script>

自定义实现穿梭框

如果需要自定义样式或功能,可以手动实现:

<template>
  <div class="transfer-container">
    <div class="list left-list">
      <h3>Available Items</h3>
      <ul>
        <li v-for="item in leftItems" :key="item.id">
          <input type="checkbox" v-model="selectedLeft" :value="item.id">
          {{ item.name }}
        </li>
      </ul>
    </div>

    <div class="transfer-buttons">
      <button @click="moveToRight">→</button>
      <button @click="moveToLeft">←</button>
    </div>

    <div class="list right-list">
      <h3>Selected Items</h3>
      <ul>
        <li v-for="item in rightItems" :key="item.id">
          <input type="checkbox" v-model="selectedRight" :value="item.id">
          {{ item.name }}
        </li>
      </ul>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      leftItems: [
        { id: 1, name: 'Item 1' },
        { id: 2, name: 'Item 2' },
        { id: 3, name: 'Item 3' }
      ],
      rightItems: [],
      selectedLeft: [],
      selectedRight: []
    }
  },
  methods: {
    moveToRight() {
      const itemsToMove = this.leftItems.filter(item => 
        this.selectedLeft.includes(item.id)
      )
      this.rightItems = [...this.rightItems, ...itemsToMove]
      this.leftItems = this.leftItems.filter(item => 
        !this.selectedLeft.includes(item.id)
      )
      this.selectedLeft = []
    },
    moveToLeft() {
      const itemsToMove = this.rightItems.filter(item => 
        this.selectedRight.includes(item.id)
      )
      this.leftItems = [...this.leftItems, ...itemsToMove]
      this.rightItems = this.rightItems.filter(item => 
        !this.selectedRight.includes(item.id)
      )
      this.selectedRight = []
    }
  }
}
</script>

<style>
.transfer-container {
  display: flex;
  justify-content: center;
  align-items: center;
  gap: 20px;
}
.list {
  width: 200px;
  border: 1px solid #ddd;
  padding: 10px;
  height: 300px;
  overflow-y: auto;
}
.transfer-buttons {
  display: flex;
  flex-direction: column;
  gap: 10px;
}
</style>

使用第三方库

除了 Element UI,还有其他 Vue 组件库提供穿梭框实现:

  1. Ant Design Vue 的 Transfer 组件
  2. Vuetify 的 v-transfer 组件
  3. Vue Transfer 独立库

功能增强建议

基础穿梭框可以扩展以下功能:

vue 穿梭框实现

  • 搜索过滤功能
  • 拖拽排序
  • 全选/全不选
  • 自定义渲染内容
  • 异步数据加载
  • 多级穿梭框

实现时需要考虑性能优化,特别是处理大量数据时,可以使用虚拟滚动等技术。

标签: vue
分享给朋友:

相关文章

vue SSG实现

vue SSG实现

Vue SSG 实现方法 Vue 的静态站点生成(SSG)可以通过多种工具和框架实现,以下是几种常见的方法: 使用 VuePress VuePress 是一个基于 Vue 的静态站点生成器,适合文档…

vue实现switch

vue实现switch

Vue 实现 Switch 开关组件 在 Vue 中实现 Switch 开关组件可以通过自定义组件或使用现有 UI 库完成。以下是几种常见实现方式: 自定义 Switch 组件 创建一个基础 Sw…

vue实现RTMP

vue实现RTMP

Vue 中实现 RTMP 流播放 RTMP(Real-Time Messaging Protocol)是一种用于实时音视频流传输的协议。在 Vue 中实现 RTMP 播放通常需要借助第三方库或播放器。…

vue实现计算

vue实现计算

Vue 实现计算的方法 在 Vue 中实现计算功能可以通过多种方式完成,主要包括计算属性(computed)、方法(methods)和侦听器(watch)。以下是具体的实现方法: 计算属性(Comp…

vue实现前端

vue实现前端

Vue 实现前端的核心方法与步骤 安装 Vue 项目 使用 Vue CLI 或 Vite 创建项目。Vue CLI 是传统构建工具,Vite 是新一代轻量级工具。 npm init vue@la…

vue键盘实现

vue键盘实现

监听键盘事件 在Vue中监听键盘事件可以通过v-on指令或@缩写实现。常见的键盘事件包括keydown、keyup和keypress。例如监听回车键事件: <template> &l…