当前位置:首页 > VUE

vue实现传输列表

2026-02-17 11:02:58VUE

Vue 实现传输列表

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

双向数据绑定与事件处理

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

vue实现传输列表

<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 组件,可以直接使用。

vue实现传输列表

以 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实现tablegrid

vue实现tablegrid

Vue 实现 TableGrid 的方法 使用 Element UI 的 Table 组件 Element UI 提供了强大的 Table 组件,可以快速实现表格布局。安装 Element UI 后,…

vue实现效果

vue实现效果

以下是Vue实现常见效果的几种典型方法,分为不同场景说明: 数据绑定与响应式更新 使用v-model实现表单双向绑定: <template> <input v-model="…

vue实现画图

vue实现画图

Vue 实现画图功能 在 Vue 中实现画图功能通常需要结合 HTML5 的 <canvas> 元素或第三方库。以下是几种常见的方法: 使用原生 Canvas API 通过 Vue 直接…

vue 实现href

vue 实现href

Vue 实现 href 的方法 在 Vue 中实现 href 功能可以通过多种方式,具体取决于需求场景。以下是几种常见的方法: 使用 <a> 标签 直接使用 HTML 的 <a&g…

vue实现list

vue实现list

Vue 实现列表渲染 在 Vue 中,可以使用 v-for 指令来渲染列表。v-for 指令基于一个数组或对象来渲染一个列表,可以遍历数组或对象的属性。 基本列表渲染 <template>…

vue mvvm实现

vue mvvm实现

Vue MVVM 实现原理 Vue.js 是一个基于 MVVM(Model-View-ViewModel)模式的框架,其核心是通过数据绑定和响应式系统实现视图与数据的自动同步。以下是 Vue 实现 M…