项目碰到一个需求是需要表格字段列进行顺序拖拽,查过一些资料,中途也碰到了很多坑,实现方式如下:

封装成公用组件操作

//父组件
<template>
<div>
<commonTable
:loading="loading"
:table-data="priceList"
:table-header-tit="tableHeaderTit"
:col-table-header-tit="colTableHeaderTit"
@columnChange="columnChange"
/>
</div>
</template>
<script>
import commonTable from '@/layout/components/common/commonTable.vue';
export default {
name: 'Table',
components: {
commonTable
},
data() {
tableHeaderTit: [],
colTableHeaderTit:[],
priceList:[],
loading:false,
},
async mounted() {
await this.initHandle();
},
methods:{
initHandle(){
//初始化调用获取默认用户表头数据接口(这边先用默认数据)
this.tableHeaderTit=[
{ key: 1, label: '价单编号', field: 'priceCode'},
{ key: 2, label: '价单名称', field: 'priceName' },
{ key: 3, label: '币种', field: 'currency' },
{ key: 4, label: '业务类型', field: 'businessTypeName'},
{ key: 5, label: '审批状态', field: 'auditStatusName'},
{ key: 6, label: '启用日期', field: 'startDate' },
{ key: 7, label: '截止日期', field: 'endDate'}
];
this.colTableHeaderTit=[
{ key: 1, label: '价单编号', field: 'priceCode'},
{ key: 2, label: '价单名称', field: 'priceName' },
{ key: 3, label: '币种', field: 'currency' },
{ key: 4, label: '业务类型', field: 'businessTypeName'},
{ key: 5, label: '审批状态', field: 'auditStatusName'},
{ key: 6, label: '启用日期', field: 'startDate' },
{ key: 7, label: '截止日期', field: 'endDate'}
]
},
columnChange(val) {
// 列拖拽操作(改变一次排序远程存储一次用户数据)
//调保存用户接口
}
}
}
</script>
//子组件  commonTable.vue
<template>
<div class="commonTable">
<el-table
ref="table"
v-loading="loading"
style="width: 100%"
class="table-wrap"
:data="tableData"
height="100%"
row-key="item"
stripe
border
header-cell-class-name="header-cell-color"
@selection-change="handleSelectionChange"
>
<el-table-column
type="selection"
width="55"
/>
<el-table-column v-for="(item, index) in colTableHeaderTit"
class-name="allowDrag"
:key="`colTableHeaderTit_${index}`"
:prop="tableHeaderTit[index].field"
:label="item.label" align="center">
</el-table-column>
</el-table>
</div>
</template>
<script>
import Sortable from 'sortablejs'
//需要下载sortablejs插件
//官方文档地址:https://github.com/SortableJS/Sortable
export default {
name:'commonTable',
props:['tableData','tableHeaderTit','colTableHeaderTit','loading'],
data() {
return {
}
},
mounted() {
this.rowDrop() //可拖拽行
this.columnDrop() //可拖拽列
},
methods: {
//行拖拽
rowDrop() {
const tbody = document.querySelector('.el-table__body-wrapper tbody')
const _this = this
Sortable.create(tbody, {
onEnd({ newIndex, oldIndex }) {
const currRow = _this.tableData.splice(oldIndex, 1)[0]
_this.tableData.splice(newIndex, 0, currRow)
}
})
},
//列拖拽
columnDrop() {
var _this = this;
const wrapperTr = document.querySelector('.el-table__header-wrapper tr')
this.sortable = Sortable.create(wrapperTr, {
draggable: ".allowDrag",//允许拖拽元素(el-table-column上设置class-name为允许拖拽)
animation: 180,
delay: 0,
//之前调用onEnd方法会出现表格DOM不更新以及表头对不上的情况所以更换为onUpdate方法
//参考资料 https://www.jianshu.com/p/fd6eb408d8bd
onUpdate:function(evt){
//修改items数据顺序
var newIndex = evt.newIndex;
var oldIndex = evt.oldIndex;
const newItem = wrapperTr.children[newIndex];
const oldItem = wrapperTr.children[oldIndex]; // 先删除移动的节点
wrapperTr.removeChild(newItem)
// 再插入移动的节点到原有节点,还原了移动的操作
if(newIndex > oldIndex) {
wrapperTr.insertBefore(newItem,oldItem)
} else {
wrapperTr.insertBefore(newItem,oldItem.nextSibling)
}
// 更新items数组(index-1是第一列有一个多选列不支持拖拽,否则会有排序错乱的问题)
var item = _this.tableHeaderTit.splice(oldIndex-1,1);
_this.tableHeaderTit.splice(newIndex-1,0,item[0]);
// 下一个tick就会走patch更新 //每次更新调取保存用户接口
_this.$emit('columnChange',_this.tableHeaderTit)
} })
}
}
}
</script>

参考:https://www.jianshu.com/p/362f880d0bfd

-----END

最新文章

  1. C#基础知识八之访问修饰符
  2. hibernate优化笔记(随时更新)
  3. nodejs安装及环境配置(windows系统)
  4. 2014北邮新生归来赛解题报告a-c
  5. nginx 配置 ThinkPHP Rewrite
  6. 54. Spiral Matrix
  7. Eclipse C/C++环境配置
  8. Dima and Salad(完全背包)
  9. poj 3013 Big Christmas Tree (dij+优先级队列优化 求最短)
  10. Webdriver初探
  11. Python-psutil模块
  12. c/c++ linux 进程间通信系列4,使用共享内存
  13. Oracle中的位图索引和函数索引
  14. C#中关闭子窗口而不释放子窗口对象的方法
  15. 【转】matlab学习(5) 读取excel文件
  16. iOS-静态库,动态库,framework浅析(三)
  17. MySQL-join的实现原理、优化及NLJ算法
  18. OpenGL中的旋转是可以叠加的?
  19. 20155303 2016-2017-2 《Java程序设计》第四周学习总结
  20. Google Map 形状显示

热门文章

  1. gitlab 配置汉化版
  2. uniapp组件监听onShow
  3. ES6-新增方法
  4. openwrt 配置 单网卡多IP
  5. mysql修改密码报错:Your password does not satisfy the current policy requirements
  6. 渗透测试工具sqlmap基础教程 【转】
  7. Rocky linux command-1
  8. vue-用户管理系统
  9. xd p4 WEB源码拓展
  10. Jquery EasyUI dataGrid 修改默认分页大小 不起效果