当前位置:首页 > VUE

vue穿梭框怎么实现

2026-01-21 10:08:57VUE

Vue 穿梭框实现方法

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

vue穿梭框怎么实现

使用 Element UI 组件

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

vue穿梭框怎么实现

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

使用第三方库

其他可选方案包括:

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

功能扩展建议

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

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

标签: vue
分享给朋友:

相关文章

vue实现详情

vue实现详情

Vue 实现详情页的常见方法 使用动态路由 在 Vue Router 中配置动态路由参数,通过 $route.params 获取详情 ID。示例路由配置: { path: '/detail/:i…

vue实现拍照

vue实现拍照

Vue实现拍照功能 在Vue中实现拍照功能通常需要结合浏览器的MediaDevices API和Canvas元素。以下是具体实现方法: 安装依赖(可选) 如果需要更简单的封装,可以安装vue-web…

vue滚动实现

vue滚动实现

Vue 滚动实现方法 使用原生滚动 在 Vue 中可以通过 ref 获取 DOM 元素,调用原生滚动方法实现滚动效果。适用于简单场景。 <template> <div ref=…

vue实现导出

vue实现导出

Vue 实现导出功能的方法 在 Vue 项目中实现导出功能通常涉及将数据导出为 Excel、CSV 或其他格式。以下是几种常见的实现方式: 使用 xlsx 库导出 Excel 安装 xlsx 库:…

vue实现Siri

vue实现Siri

Vue 实现 Siri 风格语音交互 实现类似 Siri 的语音交互功能需要结合语音识别、语音合成和前端交互逻辑。Vue 作为前端框架,可以很好地管理这些功能的交互状态。 语音识别集成 使用 Web…

vue拼图实现

vue拼图实现

实现 Vue 拼图游戏的方法 使用 Vue 组件和动态数据绑定 创建一个 Vue 组件来管理拼图的状态和逻辑。通过 v-for 动态渲染拼图块,利用 v-bind 绑定样式和位置。拼图块的数据可以存储…