当前位置:首页 > VUE

vue穿梭框怎么实现

2026-01-21 10:08:57VUE

Vue 穿梭框实现方法

Vue 穿梭框(Transfer)通常用于在两个列表之间移动数据项,以下是常见的实现方式:

使用 Element UI 组件

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

<template>
  <el-transfer
    v-model="selectedItems"
    :data="allItems"
    :titles="['可选列表', '已选列表']"
    :props="{ key: 'value', label: 'desc' }"
  />
</template>

<script>
export default {
  data() {
    return {
      allItems: [
        { value: '1', desc: '选项1' },
        { value: '2', desc: '选项2' }
      ],
      selectedItems: []
    }
  }
}
</script>

自定义实现

如需完全自定义穿梭框功能,可通过以下方式构建:

<template>
  <div class="transfer-container">
    <div class="list">
      <h3>源列表</h3>
      <div v-for="item in sourceList" :key="item.id" @click="selectItem(item)">
        {{ item.name }}
      </div>
    </div>

    <div class="actions">
      <button @click="moveToRight">→</button>
      <button @click="moveToLeft">←</button>
    </div>

    <div class="list">
      <h3>目标列表</h3>
      <div v-for="item in targetList" :key="item.id" @click="deselectItem(item)">
        {{ item.name }}
      </div>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      sourceList: [
        { id: 1, name: '项目A' },
        { id: 2, name: '项目B' }
      ],
      targetList: [],
      selectedItems: []
    }
  },
  methods: {
    selectItem(item) {
      this.selectedItems.push(item)
    },
    deselectItem(item) {
      this.selectedItems = this.selectedItems.filter(i => i.id !== item.id)
    },
    moveToRight() {
      this.targetList = [...this.targetList, ...this.selectedItems]
      this.sourceList = this.sourceList.filter(
        item => !this.selectedItems.some(sel => sel.id === item.id)
      )
      this.selectedItems = []
    },
    moveToLeft() {
      this.sourceList = [...this.sourceList, ...this.selectedItems]
      this.targetList = this.targetList.filter(
        item => !this.selectedItems.some(sel => sel.id === item.id)
      )
      this.selectedItems = []
    }
  }
}
</script>

<style>
.transfer-container {
  display: flex;
}
.list {
  width: 200px;
  border: 1px solid #ddd;
  padding: 10px;
}
.actions {
  display: flex;
  flex-direction: column;
  justify-content: center;
  padding: 0 10px;
}
</style>

使用第三方库

其他可选方案包括:

vue穿梭框怎么实现

  • Vuetify 的 v-transfer 组件
  • Ant Design Vue 的 a-transfer 组件
  • vue-draggable 实现拖拽式穿梭框

功能扩展建议

  1. 添加搜索过滤功能,在穿梭框顶部增加搜索框
  2. 实现全选/全不选功能
  3. 添加拖拽排序支持
  4. 自定义项渲染模板
  5. 添加动画过渡效果

以上方法可根据项目需求选择现成组件或自定义实现,Element UI 的方案适合快速开发,自定义实现则更灵活可控。

标签: vue
分享给朋友:

相关文章

vue实现popup

vue实现popup

Vue 实现 Popup 弹窗 使用 Vue 原生组件 创建一个基本的 Vue 组件作为弹窗,通过 v-if 或 v-show 控制显示隐藏。 <template> <div&…

vue 实现全屏

vue 实现全屏

Vue 实现全屏功能 在 Vue 中实现全屏功能可以通过浏览器提供的 Fullscreen API 完成。以下是一个完整的实现方案: 安装依赖(可选) 如果需要跨浏览器兼容性更好,可以安装…

vue实现曲线

vue实现曲线

Vue 实现曲线的方法 在 Vue 中实现曲线可以通过多种方式,包括使用 SVG、Canvas 或第三方库如 D3.js、ECharts 等。以下是几种常见的方法: 使用 SVG 绘制曲线 SVG…

vue实现路径

vue实现路径

Vue 实现路径的方法 在 Vue 中实现路径管理通常涉及 Vue Router 的使用,以下是一些常见的实现方法: 安装 Vue Router 通过 npm 或 yarn 安装 Vue Route…

vue实现watch

vue实现watch

监听数据变化 在Vue中,watch用于监听响应式数据的变化并执行回调函数。基本语法如下: watch: { propertyName(newValue, oldValue) { //…

vue实现秒表

vue实现秒表

实现秒表功能 在Vue中实现秒表功能需要处理时间显示、开始、暂停和重置逻辑。以下是具体实现方式: 基础实现代码 <template> <div> <div&…