当前位置:首页 > VUE

vue实现穿梭框

2026-01-14 03:07:20VUE

Vue 实现穿梭框组件

穿梭框(Transfer)是一种常见的UI组件,用于在两栏之间移动元素。以下是基于Vue的实现方法:

基本结构实现

创建两个列表容器和操作按钮区域:

<template>
  <div class="transfer-container">
    <div class="transfer-list">
      <h3>源列表</h3>
      <ul>
        <li v-for="item in sourceList" :key="item.id">
          <input type="checkbox" v-model="selectedSource" :value="item.id">
          {{ item.label }}
        </li>
      </ul>
    </div>

    <div class="transfer-actions">
      <button @click="moveToTarget">→</button>
      <button @click="moveToSource">←</button>
    </div>

    <div class="transfer-list">
      <h3>目标列表</h3>
      <ul>
        <li v-for="item in targetList" :key="item.id">
          <input type="checkbox" v-model="selectedTarget" :value="item.id">
          {{ item.label }}
        </li>
      </ul>
    </div>
  </div>
</template>

数据与逻辑处理

<script>
export default {
  data() {
    return {
      sourceList: [
        { id: 1, label: '选项1' },
        { id: 2, label: '选项2' },
        { id: 3, label: '选项3' }
      ],
      targetList: [],
      selectedSource: [],
      selectedTarget: []
    }
  },
  methods: {
    moveToTarget() {
      this.selectedSource.forEach(id => {
        const index = this.sourceList.findIndex(item => item.id === id)
        if (index !== -1) {
          this.targetList.push(this.sourceList[index])
          this.sourceList.splice(index, 1)
        }
      })
      this.selectedSource = []
    },
    moveToSource() {
      this.selectedTarget.forEach(id => {
        const index = this.targetList.findIndex(item => item.id === id)
        if (index !== -1) {
          this.sourceList.push(this.targetList[index])
          this.targetList.splice(index, 1)
        }
      })
      this.selectedTarget = []
    }
  }
}
</script>

样式优化

<style>
.transfer-container {
  display: flex;
  align-items: center;
  gap: 20px;
}

.transfer-list {
  border: 1px solid #ddd;
  width: 200px;
  height: 300px;
  overflow-y: auto;
}

.transfer-list ul {
  list-style: none;
  padding: 0;
  margin: 0;
}

.transfer-list li {
  padding: 8px;
  border-bottom: 1px solid #eee;
}

.transfer-actions {
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.transfer-actions button {
  padding: 5px 10px;
  cursor: pointer;
}
</style>

高级功能扩展

  1. 搜索过滤功能:

    computed: {
    filteredSourceList() {
     return this.sourceList.filter(item => 
       item.label.toLowerCase().includes(this.sourceSearch.toLowerCase())
     )
    },
    filteredTargetList() {
     return this.targetList.filter(item =>
       item.label.toLowerCase().includes(this.targetSearch.toLowerCase())
     )
    }
    }
  2. 全选功能:

    vue实现穿梭框

    methods: {
    selectAllSource() {
     this.selectedSource = this.filteredSourceList.map(item => item.id)
    },
    selectAllTarget() {
     this.selectedTarget = this.filteredTargetList.map(item => item.id)
    }
    }
  3. 拖拽排序支持:

    <li 
    v-for="item in targetList" 
    :key="item.id"
    draggable="true"
    @dragstart="dragStart(item)"
    @dragover.prevent
    @drop="dropOnTarget($event, item)"
    >

组件封装建议

  1. 通过props接收初始数据:

    vue实现穿梭框

    props: {
    data: {
     type: Array,
     default: () => []
    },
    value: {
     type: Array,
     default: () => []
    }
    }
  2. 使用v-model实现双向绑定:

    watch: {
    targetList(newVal) {
     this.$emit('input', newVal)
    }
    }
  3. 提供插槽自定义渲染:

    <slot name="item" v-bind="{ item }">
    {{ item.label }}
    </slot>

使用第三方库

对于生产环境,可以考虑使用成熟的UI库:

  • Element UI的Transfer组件
  • Ant Design Vue的Transfer组件
  • Vuetify的v-data-iterator实现类似功能

这些库提供了更完善的API和样式,可以直接集成使用。

标签: vue
分享给朋友:

相关文章

vue实现报表

vue实现报表

vue实现报表的方法 使用Vue实现报表通常需要结合图表库或表格组件。以下是几种常见方法: 使用ECharts 安装ECharts库: npm install echarts vue-echart…

vue实现删除

vue实现删除

Vue 实现删除功能的方法 在 Vue 中实现删除功能通常涉及以下几个步骤: 数据绑定与列表渲染 使用 v-for 指令渲染列表数据,并为每个项绑定唯一标识符(如 id)。例如: <ul&g…

vue实现剪辑

vue实现剪辑

Vue 实现视频剪辑功能 在Vue中实现视频剪辑功能通常需要结合HTML5的<video>元素和第三方库(如FFmpeg.js或Video.js)。以下是几种常见的实现方法: 使用FFm…

vue实现试卷

vue实现试卷

Vue实现试卷系统的基本思路 使用Vue.js构建试卷系统需要结合组件化开发、状态管理和后端数据交互。核心功能包括题目展示、答题逻辑、计时器和提交答案。 核心功能模块划分 试卷组件结构 试卷容器组…

实现vue datepicker

实现vue datepicker

安装依赖 使用 vue-datepicker 或第三方库如 v-calendar 或 vue2-datepicker。以 vue2-datepicker 为例: npm install vue2…

vue 菜单实现

vue 菜单实现

Vue 菜单实现方法 在Vue中实现菜单功能可以通过多种方式完成,以下是几种常见的实现方法: 使用v-for动态生成菜单 通过数据驱动的方式动态渲染菜单项,适合菜单内容可能变化的场景: <t…