当前位置:首页 > VUE

vue实现传输列表

2026-02-17 11:02:58VUE

Vue 实现传输列表

在 Vue 中实现传输列表(Transfer List)功能,通常涉及两个列表之间的数据转移。以下是实现方法:

双向数据绑定与事件处理

定义两个列表数据,使用 v-modelv-for 渲染列表项,并通过事件处理函数实现数据转移。

<template>
  <div class="transfer-list">
    <div class="list">
      <h3>源列表</h3>
      <ul>
        <li v-for="item in sourceList" :key="item.id">
          <input type="checkbox" v-model="selectedItems" :value="item.id" />
          {{ item.name }}
        </li>
      </ul>
    </div>
    <div class="controls">
      <button @click="moveToTarget">>></button>
      <button @click="moveToSource"><<</button>
    </div>
    <div class="list">
      <h3>目标列表</h3>
      <ul>
        <li v-for="item in targetList" :key="item.id">
          <input type="checkbox" v-model="selectedItems" :value="item.id" />
          {{ item.name }}
        </li>
      </ul>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      sourceList: [
        { id: 1, name: 'Item 1' },
        { id: 2, name: 'Item 2' },
        { id: 3, name: 'Item 3' }
      ],
      targetList: [],
      selectedItems: []
    };
  },
  methods: {
    moveToTarget() {
      this.selectedItems.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.selectedItems = [];
    },
    moveToSource() {
      this.selectedItems.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.selectedItems = [];
    }
  }
};
</script>

<style>
.transfer-list {
  display: flex;
  gap: 20px;
}
.list {
  border: 1px solid #ccc;
  padding: 10px;
  width: 200px;
}
.controls {
  display: flex;
  flex-direction: column;
  justify-content: center;
}
</style>

使用第三方组件库

许多 Vue 组件库(如 Element UI、Ant Design Vue)提供了现成的 Transfer 组件,可以直接使用。

以 Element UI 为例:

<template>
  <el-transfer
    v-model="targetList"
    :data="sourceList"
    :titles="['源列表', '目标列表']"
    :button-texts="['向左移动', '向右移动']"
    :format="{
      noChecked: '${total}',
      hasChecked: '${checked}/${total}'
    }"
  />
</template>

<script>
export default {
  data() {
    return {
      sourceList: [
        { key: 1, label: 'Item 1' },
        { key: 2, label: 'Item 2' },
        { key: 3, label: 'Item 3' }
      ],
      targetList: []
    };
  }
};
</script>

自定义拖拽功能

如果需要更灵活的交互,可以通过 HTML5 的拖拽 API 实现拖拽转移。

vue实现传输列表

<template>
  <div class="drag-transfer">
    <div class="list" @drop="dropSource" @dragover.prevent>
      <h3>源列表</h3>
      <ul>
        <li
          v-for="item in sourceList"
          :key="item.id"
          draggable="true"
          @dragstart="dragStart(item)"
        >
          {{ item.name }}
        </li>
      </ul>
    </div>
    <div class="list" @drop="dropTarget" @dragover.prevent>
      <h3>目标列表</h3>
      <ul>
        <li
          v-for="item in targetList"
          :key="item.id"
          draggable="true"
          @dragstart="dragStart(item)"
        >
          {{ item.name }}
        </li>
      </ul>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      sourceList: [
        { id: 1, name: 'Item 1' },
        { id: 2, name: 'Item 2' },
        { id: 3, name: 'Item 3' }
      ],
      targetList: [],
      draggedItem: null
    };
  },
  methods: {
    dragStart(item) {
      this.draggedItem = item;
    },
    dropSource() {
      const index = this.targetList.findIndex(
        item => item.id === this.draggedItem.id
      );
      if (index !== -1) {
        this.sourceList.push(this.draggedItem);
        this.targetList.splice(index, 1);
      }
    },
    dropTarget() {
      const index = this.sourceList.findIndex(
        item => item.id === this.draggedItem.id
      );
      if (index !== -1) {
        this.targetList.push(this.draggedItem);
        this.sourceList.splice(index, 1);
      }
    }
  }
};
</script>

以上方法可以根据需求选择或组合使用,实现灵活的传输列表功能。

标签: 列表vue
分享给朋友:

相关文章

vue实现ai

vue实现ai

Vue 实现 AI 功能 Vue.js 可以通过集成第三方 AI 服务或本地 AI 模型来实现 AI 功能。以下是几种常见的方法: 集成第三方 AI API 使用 Vue 调用如 OpenAI、Go…

实现.vue文件

实现.vue文件

创建.vue文件的基本结构 一个标准的.vue文件包含三个部分:模板(template)、脚本(script)和样式(style)。这种结构允许开发者在一个文件中管理组件的HTML、JavaScrip…

vue原理实现

vue原理实现

Vue 原理实现的核心机制 Vue.js 的核心原理基于响应式系统、虚拟 DOM 和模板编译。以下是其核心实现机制的分解: 响应式系统 Vue 使用 Object.defineProperty(Vu…

vue实现radio

vue实现radio

Vue 实现 Radio 按钮 在 Vue 中实现 Radio 按钮可以通过 v-model 进行数据绑定,结合 input 元素的 type="radio" 属性。以下是一个基本示例: <t…

vue 实现跳转

vue 实现跳转

Vue 实现页面跳转的方法 在 Vue 中实现页面跳转通常可以通过以下几种方式实现,具体选择取决于项目需求和技术栈(如是否使用 Vue Router)。 使用 Vue Router 进行编程式导航…

vue实现幻灯

vue实现幻灯

Vue实现幻灯片的基础方法 使用Vue实现幻灯片功能可以通过多种方式完成,以下是几种常见方案: 方案一:基于Vue的过渡动画 通过Vue的<transition>或<transit…