博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
vue2.0 + element ui 实现表格穿梭框
阅读量:4337 次
发布时间:2019-06-07

本文共 4649 字,大约阅读时间需要 15 分钟。

element ui 官网里介绍了穿梭框(Transfer),但在实际使用过程中,会出现一些问题:

1.穿梭框里能放置的内容太少,不能满足复杂的业务需求。

2.当选项过多时,穿梭框很难实现分页,左右两个框的分页是联动的,左边翻页了右边也会跟着翻页。若要取消这种关联关系,可参考这篇文章: https://www.cnblogs.com/alice-bj/articles/10703903.html#_label4

本文参考了穿梭框的实现思路,实现了可分页的表格穿梭框,同时涉及到了表格多选与表格里添加表单等知识点。

html结构:

查找

 js部分:

data() {  return {    listLoading: true,    staffTemp: {        phone: "",        nickName: "",        staffTypeId: ""      },    staffList: [],    selectedStaffList: [],    staffData: [],    selectedStaffData: [],    tableKey: 0,    rowKey: "rowKey",    staffOptions: [        { key: 28, display_name: "补货员" },        { key: 29, display_name: "测试员" }      ],  }},methods: {    // 从后台获取左边表格的数据    getStaffList() {      fetchStaffList(this.staffTemp).then(res => {        if (res.value.staff.length === 0) {          alert("查无此人");        }        this.staffList = res.value.staff;      });    },    // 将左边表格选择项存入staffData中    handleStaffChange(rows) {      this.staffData = [];      if (rows) {        rows.forEach(row => {          if (row) {            this.staffData.push(row);          }        });      }    },    // 左边表格选择项移到右边    addStaff() {      setTimeout(() => {        this.$refs["staffTable"].clearSelection();        this.$refs["selectedStaffTable"].clearSelection();      }, 0);      let repeat = false;      this.selectedStaffList.forEach(item => {        if (this.staffData[0] && item.phone === this.staffData[0].phone) {          repeat = true;          alert("此员工已添加");          return;        }      });      if (repeat === false) {        this.staffData.forEach(item => {          this.selectedStaffList.push(item);        });        for (let i = 0; i < this.staffList.length; i++) {          for (let j = 0; j < this.staffData.length; j++) {            if (              this.staffList[i] &&              this.staffData[j] &&              this.staffList[i].phone === this.staffData[j].phone            ) {              this.staffList.splice(i, 1);            }          }        }      }    },    // 右边表格选择项移到左边    removeStaff() {      setTimeout(() => {        this.$refs["staffTable"].clearSelection();        this.$refs["selectedStaffTable"].clearSelection();      }, 0);      this.selectedStaffData.forEach(item => {        this.staffList.push(item);      });      for (let i = 0; i < this.selectedStaffList.length; i++) {        for (let j = 0; j < this.selectedStaffData.length; j++) {          if (            this.selectedStaffList[i] &&            this.selectedStaffData[j] &&            this.selectedStaffList[i].phone === this.selectedStaffData[j].phone          ) {            this.selectedStaffList.splice(i, 1);          }        }      }    },    // 将右边表格选择项存入selectedStaffData中    handleSelectedStaffChange(rows) {      this.selectedStaffData = [];      if (rows) {        rows.forEach(row => {          if (row) {            this.selectedStaffData.push(row);          }        });      }    },    // 提交    modifyStaff() {      let isEmpty = false;      this.selectedStaffList.forEach(item => {        if (!item.staffTypeId) {          alert("请选择类型");          isEmpty = true;          return;        }      });      if (isEmpty === false) {        editStaff(this.selectedStaffList, this.deviceQuery.id).then(res => {          this.staffListVisible = false;          this.$notify({            title: "成功",            message: "修改成功",            type: "success",            duration: 2000          });        });      }    }}

多选表格:手动添加一个 el-table-column,设type属性为 selection 即可;当选择项发生变化时会触发 selection-change 事件,并将选择项作为参数传入。在这里,我们将左边表格的选择项缓存在staffData中,右边表格的选择项缓存在 selectedStaffData 中。

在移动选择项时,一是要将自身的该项删除,二是要将该项放入对方列表中(需要去重)。

关于分页功能可在左右两个表格分别添加,互不影响,具体可参考我之前的博客 

转载于:https://www.cnblogs.com/zdd2017/p/11188307.html

你可能感兴趣的文章
Ubuntu菜鸟入门(五)—— 一些编程相关工具
查看>>
valgrind检测linux程序内存泄露
查看>>
Hadoop以及组件介绍
查看>>
1020 Tree Traversals (25)(25 point(s))
查看>>
第一次作业
查看>>
“==”运算符与equals()
查看>>
单工、半双工和全双工的定义
查看>>
Hdu【线段树】基础题.cpp
查看>>
时钟系统
查看>>
BiTree
查看>>
5个基于HTML5的加载动画推荐
查看>>
水平权限漏洞的修复方案
查看>>
静态链接与动态链接的区别
查看>>
Android 关于悬浮窗权限的问题
查看>>
如何使用mysql
查看>>
linux下wc命令详解
查看>>
敏捷开发中软件测试团队的职责和产出是什么?
查看>>
在mvc3中使用ffmpeg对上传视频进行截图和转换格式
查看>>
python的字符串内建函数
查看>>
Spring - DI
查看>>