表格穿梭框

本文展示了如何使用Vue.js和Element UI库创建一个穿梭框组件。代码详细地描述了两个表格之间的数据交换过程,利用selection-change事件监听选中项,并实现向左或向右移动数据的功能。该组件适用于多选数据转移场景。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

先看看成品

 

 

 多的不说  少的不唠  上代码

<template>
  <div class="bigbox">
    <!-- 左侧框 -->
    <div class="table-container">
      <!-- 左侧表格 -->
      <el-table
        ref="dxgrid"
        :data="currentPageData"
        highlight-current-row
        height="100%"
        @selection-change="leftArrChange"
        style="width: 100%; cursor: pointer"
      >
        <el-table-column type="selection" width="55" />
        <el-table-column
          prop="label"
          label="名称"
          show-overflow-tooltip
          width="520"
        >
        </el-table-column>
      </el-table>
    </div>
    <!-- 中间框 -->
    <div class="select-to-right">
      <el-button size="mini" @click="turnLeft">向左 </el-button>
      <el-button size="mini" @click="turnRight">向右 </el-button>
    </div>
    <!-- 右侧框 -->
    <div class="table-container">
      <!-- 右侧表格 -->
      <el-table ref="yxgrid" :data="yxData" height="100%" @selection-change="rightArrChange">
        <el-table-column type="selection" width="55" />
        <el-table-column
          prop="label"
          label="名称"
          show-overflow-tooltip
          width="450"
        >
        </el-table-column>
      </el-table>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      currentPageData: [ 
        {
          label: 1,
          id: 1,
        },
        {
          label: 2,
          id: 2,
        },
        {
          label: 3,
          id: 3,
        },
        {
          label: 4,
          id: 4,
        },
        {
          label: 5,
          id: 5,
        },
        {
          label: 6,
          id: 6,
        },
      ], //左边的数据
      yxData: [], // 右侧表格数据
      leftArr: [], // 左侧选择的数据
      rightArr: [], // 右侧选择的数据
    };
  },
  methods: {
    leftArrChange(val) {
      this.leftArr = val;
    },
    rightArrChange(val) {
      this.rightArr = val;
    },
    // 将右侧数据转到左边
    turnLeft(){
      // 将右侧选择的数据  进行循环遍历
      this.rightArr.forEach((item, index) => {
        // 将数据插入到左边
        this.currentPageData.push(item)
        //再将左侧的数据进行删除
        this.yxData.forEach((l, i) => {
          if (item.id === l.id){
            this.yxData.splice(i, 1)
          }
        })
      })
    },
    // 将左侧数据转到右边
    turnRight(){
      // 将左侧选择的数据  进行循环遍历
      this.leftArr.forEach((item, index) => {
        // 将数据插入到右边
        this.yxData.push(item)
        //再将左侧的数据进行删除
        this.currentPageData.forEach((l, i) => {
          if (item.id === l.id){
            this.currentPageData.splice(i, 1)
          }
        })
      })
    }
  },
};
</script>

<style scoped lang="scss">
.bigbox {
  display: flex;
  justify-content: flex-start;
}
</style>

讲讲思路吧!

这里首先画页面分为三块  左侧表格  中间按钮  右侧表格

用个大盒子套起来  弹性盒排版

穿梭框 主要实现原理就是  

通过elementui表格的selection-change事件得到选中的数据  循环遍历 添加到另一侧  然后在本侧删除掉

最后的新数据都在左右表格绑定的数据中

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值