当前位置:首页 > 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实现多用户登录

vue实现多用户登录

实现多用户登录的基本思路 在Vue中实现多用户登录通常需要结合后端API完成身份验证,并通过前端路由、状态管理(如Vuex或Pinia)和本地存储(如localStorage)来管理用户会话。以下是关…

vue实现活动倒计时

vue实现活动倒计时

实现活动倒计时的基本思路 在Vue中实现活动倒计时通常需要获取当前时间与活动结束时间的差值,并将其转换为天、小时、分钟、秒的格式展示。通过定时器每秒更新显示的时间。 定义倒计时数据 在Vue组件的d…

vue实现报表

vue实现报表

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

vue实现ide

vue实现ide

Vue 实现 IDE 的基本思路 使用 Vue 构建一个简易的集成开发环境(IDE)需要整合代码编辑器、文件管理、终端模拟等功能。以下是关键实现步骤和推荐工具。 核心功能模块 代码编辑器 使用…

vue 实现上传

vue 实现上传

Vue 实现文件上传 使用 Vue 实现文件上传可以通过原生 HTML 的 <input type="file"> 或第三方库(如 axios、vue-upload-component)完…

vue实现抽屉

vue实现抽屉

Vue 实现抽屉组件 抽屉组件是一种常见的 UI 模式,通常用于从屏幕边缘滑出内容。以下是几种实现抽屉的方法: 使用 Vue 原生实现 创建一个基本的抽屉组件,利用 Vue 的过渡和条件渲染功能。…