同时改变两个表格的宽 element ui

文章首发地址:https://juejin.cn/post/6994599997563994142

需求是两个表格,改变其中一个表格列的宽,另一个表格也随之相应改变同等宽度。本文是用的vue+element ui框架。 文中使用header-dragend方法,官网有介绍:当拖动表头改变了列的宽度的时候会触发该事件

具体代码如下:

  <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
          }]
        }
      }

效果图如下:

image.png

image.png

posted @ 2021-08-27 14:40  vaelcy  阅读(307)  评论(0编辑  收藏  举报