当前位置:   article > 正文

element ui el-table表格宽度根据元素宽度自适应_elementui table 宽度自适应

elementui table 宽度自适应

需求要将元素全部展示,用户自己拖动滚动条来查看元素。

  1. 第一步设置表格内的元素不会自动隐藏,本人用的SCSS,使用样式穿透达到这一效果
    1. .table::v-deep .el-table .cell {
    2. white-space: nowrap;
    3. text-overflow: unset;
    4. }
  2. 初始html,将fit设置为false,使伸缩窗口大小不会改变table
    1. <el-table :data="tableData" ref="table" :border="true" v-loading="loading" height="580px" :fit="false">
    2. <el-table-column prop="month" label="台账月份"></el-table-column>
    3. <el-table-column prop="date" label="日期"></el-table-column>
    4. <el-table-column prop="corporateName" label="公司名"></el-table-column>
    5. <el-table-column prop="store" label="门店"></el-table-column>
    6. <el-table-column prop="projectType" label="项目类型" width="80"></el-table-column>
    7. <el-table-column prop="contractCode" label="合同编号"></el-table-column>
    8. <el-table-column prop="address" label="地址"></el-table-column>
    9. <el-table-column prop="amountReceivable" label="应收金额"></el-table-column>
    10. <el-table-column prop="paidInAmount" label="实收金额"></el-table-column>
    11. <el-table-column prop="isInvoicing" label="是否开票"></el-table-column>
    12. <el-table-column prop="client" label="委托人"></el-table-column>
    13. </el-table>
  3. js代码如下,模拟调用接口

    1. created() {
    2. this.getData()
    3. },
    4. methods: {
    5. getData() {
    6. setTimeout(() => {
    7. this.tableData.push({
    8. month: '2022-05', date: '2022-05-18', corporateName: '长沙眷屋房地产中介有限责任公司AAA', store: '新环境藏珑店A店', projectType: '售', contractCode: 'C546123457812',
    9. address: '藏珑湖上国际花园5栋1单元1805A', amountReceivable: 180000, paidInAmount: 0, isInvoicing: false, client: ['委托人1', '委托人2', '委托人3']
    10. }, {
    11. month: '2022-05', date: '2022-05-18', corporateName: '长沙眷屋房地产中介有限责任公司', store: '新环境藏珑店A店BBB', projectType: '售', contractCode: 'C546123457812',
    12. address: '藏珑湖上国际花园5栋1单元1805ABBB', amountReceivable: 180000, paidInAmount: 0, isInvoicing: false, client: ['委托人1', '委托人2', '委托人3']
    13. }, {
    14. month: '2022-05', date: '2022-05-18', corporateName: '长沙眷屋房地产中介有限责任公司曹碾卡萨丁开发局', store: '新环境藏珑店A店BBB玄酒瓠脯vjkzcbjk', projectType: '售', contractCode: 'C546123457812',
    15. address: '藏珑湖上国际花园5栋1单元1805ABBB', amountReceivable: 180000, paidInAmount: 0, isInvoicing: false, client: ['委托人1', '委托人2', '委托人3']
    16. })
    17. this.rendering()
    18. }, 2000)
    19. },
    20. },
  4. 核心渲染this.rendering()

    1. rendering() {
    2. this.$nextTick(() => {
    3. let header = this.$refs.table.$el.querySelector('.el-table__header-wrapper')
    4. let headerCol = header.querySelectorAll('col')
    5. let body = this.$refs.table.$el.querySelector('.el-table__body-wrapper')
    6. let bodyCol = body.querySelectorAll('col')
    7. let tr = body.querySelectorAll('tr')
    8. let maxLength = []
    9. tr.forEach((nodeList, i) => {
    10. let cell = nodeList.querySelectorAll('.cell')
    11. if (i >= 1) {
    12. let oldCell = tr[i - 1].querySelectorAll('.cell')
    13. cell.forEach((node, index) => {
    14. maxLength[index] = oldCell[index].scrollWidth > node.scrollWidth ? oldCell[index].scrollWidth : node.scrollWidth
    15. })
    16. } else {
    17. cell.forEach((node, index) => {
    18. maxLength.push(node.scrollWidth)
    19. })
    20. }
    21. });
    22. maxLength.forEach((element, index) => {
    23. headerCol[index].width = element + ''
    24. bodyCol[index].width = element + ''
    25. });
    26. this.loading = false
    27. })
    28. }

  5. 到这一步已经完成,如果没有效果需要再加上一些css代码

    1. .table::v-deep .el-table,
    2. .table::v-deep .el-table__footer-wrapper,
    3. .table::v-deep .el-table__header-wrapper,
    4. .table::v-deep .el-table__body-wrapper {
    5. overflow: unset;
    6. }
    7. .table::v-deep .el-table {
    8. overflow-x: auto;
    9. }

声明:本文内容由网友自发贡献,转载请注明出处:【wpsshop】
推荐阅读