这是我参与8月更文挑战的第9天,活动详情查看: 8月更文挑战html
学习过程当中将笔记跟你们分享,但愿对你们也有所帮助,共同成长进步💪~
若是你们喜欢,能够点赞或留言💕~~~~,谢谢你们⭐️⭐️⭐️~~~vue
需求是两个表格,改变其中一个表格列的宽,另外一个表格也随之相应改变同等宽度。本文是用的vue+element ui框架。 文中使用header-dragend方法,官网有介绍:当拖动表头改变了列的宽度的时候会触发该事件 具体代码以下:web
<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" :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>
<hr>
<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" :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>
复制代码
methods: {
handleClick(row) {
console.log(row);
},
headerdragend() {
top.console.log('headerdragend', arguments)
var el = arguments[3].target;
var clist = el.parentNode.parentNode.parentNode.parentNode.children[0].children;
setTimeout(() => {
this.tableTitleList.forEach((v, idx) => {
v.width = (clist[idx] && clist[idx].width) || 'auto'
})
}, 100)
},
}
data() {
return {
tableTitleList: [
{
fixed: true,
prop: "date",
label: "日期",
width: "150"
},
{
prop: "name",
label: "姓名",
width: "120",
},
{
prop: "province",
label: "省份",
width: "120",
},
{
prop: "city",
label: "市区",
width: "120",
},
{
prop: "address",
label: "地址",
width: "300",
},
{
prop: "zip",
label: "邮编",
width: "120",
},
],
tableData: [{
date: '2016-05-02',
name: '王小虎',
province: '上海',
city: '普陀区',
address: '上海市普陀区金沙江路 1518 弄',
zip: 200333
}, {
date: '2016-05-04',
name: '王小虎',
province: '上海',
city: '普陀区',
address: '上海市普陀区金沙江路 1517 弄',
zip: 200333
}, {
date: '2016-05-01',
name: '王小虎',
province: '上海',
city: '普陀区',
address: '上海市普陀区金沙江路 1519 弄',
zip: 200333
}, {
date: '2016-05-03',
name: '王小虎',
province: '上海',
city: '普陀区',
address: '上海市普陀区金沙江路 1516 弄',
zip: 200333
}]
}
}
复制代码
效果图以下:markdown
若是你们有更好的方法能够写在评论里供你们参考~~~~ 欢迎阅读~~~⭐️⭐️⭐️框架