作者: 文章来源:
本文原地址:https://www.mimiwuqi.com/webqianduan/196646.html
目录文章目录最近在项目中有这样一个需求,基于 elementUI 的 table 组件如何实现拖动修改列宽度,并同步在多个表格中,这个功能其实大家也挺常见的,所以既然遇到了,就总结一下,方便日后再遇到时遗忘掉相关知识,也为新朋友提供参考价值。
我们的目的是:针对于不同的人群,显示不同的字段,不同的排序规则(index、fixed)。那好,我们先来分析一下实现这样的功能都需要做什么。
el-table-column 组件添加 width 属性即可。:width。border ,并且开启 resizable (默认开启)。
header-dragend(newWidth, oldWidth, column, event) 当拖动表头改变了列的宽度的时候会触发该事件<el-table-column> 的顺序来控制。经过调研 elementUI 发现可以近乎完成这项工作,那么我们就不考虑其他方案了。只把获取当前宽度配置这一个问题解决就好了。
思考一下,怎么改宽度?这不就 style="width: 10px" 行内样式嘛,这也太简单了。

那就简单了,我们直接获取即可。
重点是加 border
<el-table :data="tableData" border @header-dragend="headerdragend" style="width: 100%">
<el-table-column v-for="column in tableTitleList" :fixed="column.fixed" :prop="column.prop"
v-if="column.isShow"
:label="column.label" :width="column.width">
</el-table-column>
<el-table-column fixed="right" label="操作" width="100">
<template slot-scope="scope">
<el-button @click="handleClick(scope.row)" type="text" size="small">查看</el-button>
<el-button type="text" size="small">编辑</el-button>
</template>
</el-table-column>
</el-table>
通过 @header-dragend 拿到通知,然后去获取节点上的真实数据
headerdragend(newWidth, oldWidth, column, e) {
// 获取到触发节点,也就是你拖动的是哪一个
var el = e.target;
// 获取到当前 table 的 colgroup col 节点,用于后面获取宽度
// getParentNodes 是一个模仿 $(el).parents() 的方法
var colList = getParentNodes(el, 'table').querySelectorAll('colgroup col');
// 获取当前拖动的是第几个,方便后续检测 DOM 是否已更新
var currentColIndex = this.tableTitleList.findIndex(item=>item.label == column.label);
if(currentColIndex == -1){
return console.warn('找不到拖动列')
}
// 修改配置列表,把当前列设置为固定宽度
this.tableTitleList[currentColIndex].widthEnable = true;
// 起了一个定时任务去获取最终的宽度
clearInterval(headerDragendInterval)
headerDragendInterval = setInterval(()=>{
// 判断一下目标列的宽度是否为最终宽度
if(colList[currentColIndex].width == newWidth){
this.changeColumnWidth(colList);
clearInterval(headerDragendInterval)
}else if(colList[currentColIndex].width == oldWidth){
console.info('需要等待渲染')
}else{
console.warn('异常值');
this.changeColumnWidth(colList);
clearInterval(headerDragendInterval)
}
}, 10)
}
因为数组是有顺序的嘛,所以使用 v-for 来循环,然后改变数组中的顺序,就可以改变渲染出来的内容顺序。
<el-table :data="tableData" border @header-dragend="headerdragend" style="width: 100%">
<el-table-column v-for="column in tableTitleList" :fixed="column.fixed" :prop="column.prop"
v-if="column.isShow"
:label="column.label" :width="column.width">
</el-table-column>
<el-table-column fixed="right" label="操作" width="100">
<template slot-scope="scope">
<el-button @click="handleClick(scope.row)" type="text" size="small">查看</el-button>
<el-button type="text" size="small">编辑</el-button>
</template>
</el-table-column>
</el-table>
这块我直接使用 Sortable 来实现功能,正好也是群里一个人问的。在 onEnd 的时候把操作同步到数据源就 OK 了。
initSort(){
var el = document.querySelector('#sortWrap')
var that = this;
Sortable.create(el, {
delay: 100,
sort: !0,
forceFallback: !0,
scrollSensitivity: 100,
animation: 150,
ghostClass: "gift-ghost-item",
chosenClass: "gift-chosen-item",
onEnd: function(t) {
that.tableTitleList.splice(
t.newIndex,
0,
that.tableTitleList.splice(t.oldIndex, 1)[0]
)
}
})
},
完结,拿走不谢。
更多 建站教程 请访问 https://www.mimiwuqi.com/webqianduan/