当前位置:首页 > 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 实现拖拽转移。

<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
分享给朋友:

相关文章

验证码实现vue

验证码实现vue

验证码实现(Vue) 使用组件库(如Element UI) Element UI提供了现成的验证码组件,可直接集成到Vue项目中。 安装Element UI: npm install elem…

vue公共列表的实现

vue公共列表的实现

实现 Vue 公共列表组件 公共列表组件通常用于展示重复结构的数据,例如商品列表、用户列表等。以下是一个典型的实现方式: 基础实现 创建可复用的列表组件 List.vue: <tem…

vue聊天实现

vue聊天实现

Vue 聊天应用实现步骤 创建 Vue 项目 使用 Vue CLI 或 Vite 初始化项目,安装必要依赖(如 Vue 3、Element UI 或 Vuetify 用于 UI 组件)。 npm…

vue实现过滤

vue实现过滤

Vue实现过滤的方法 在Vue中实现数据过滤可以通过多种方式完成,以下是几种常见的方法: 使用计算属性过滤 计算属性是Vue中处理数据过滤的理想选择,它们基于响应式依赖进行缓存,只有在相关依赖发生改…

vue能实现

vue能实现

Vue.js 是一个流行的前端框架,能够实现多种功能。以下是 Vue 能够实现的主要功能和特性: 响应式数据绑定 Vue 的核心特性之一是响应式数据绑定。通过数据驱动视图,当数据发生变化时,视图会自…

vue监听实现

vue监听实现

Vue 监听实现方法 在 Vue 中可以通过多种方式实现监听数据变化或事件触发,以下是常见的几种方法: 使用 watch 选项 watch 是 Vue 提供的核心功能之一,用于观察和响应 Vue 实…