el-table自定义样式,表头固定,数据过多时滚动

最终效果:(此处没体现出来滚动,数据没那么多)

1.表头固定,设置表头样式,修改表格背景色

<div class="category-table">

   <el-table

          ref="tableRef"

          class="common-table"

          height="100%"

          :row-style="{ height: rowHeight + 'px' }"

          :header-row-style="{

            background: `url(${tableHeader}) center no-repeat !important`,

            backgroundSize: `100% 100% !important`,

          }"

          style="width: 100%; height: 100%; background-color: transparent"

        >

  </el-table>

</div>

动态设置行高,在获取到数据以后,记得加一个$nextTick(),不然会报错

// 在获取到表格数据后,判断数据长度大于0后调用
this.$nextTick(() => {
   this.initRowHeight();
});

// 设置行高
    initRowHeight() {
      let tableHeight =
        Math.round(this.$refs["tableRef"].$el.offsetHeight) -
        Math.round(this.$refs["tableRef"].$el.childNodes[1].offsetHeight);
      this.rowHeight = Math.floor(tableHeight / 10); // 返回小于等于最终结果的最大整数
      setTimeout(() => {
        if (this.$refs.tableRef) {
          this.$refs.tableRef.doLayout();
        }
      }, 1000);
    },

2.写在有scoped 的style标签内
/* 显示滚动条 */
.category-table ::v-deep .el-table--scrollable-x .el-table__body-wrapper {
  overflow-y: scroll;
}
/* 设置表格的滚动条宽度 */
.category-table >>> .el-table__body-wrapper::-webkit-scrollbar {
  width: 10px;
  height: 8px;
}
/*定义滚动条轨道 内阴影+圆角*/
.category-table >>> .el-table__body-wrapper::-webkit-scrollbar-track {
  border-radius: 8px;
  background-color: transparent;
}
/*定义滑块 内阴影+圆角*/
.category-table >>> .el-table__body-wrapper::-webkit-scrollbar-thumb {
  border-radius: 8px;
  box-shadow: inset 0 0 6px rgba(200, 209, 217, 0.3);
  background-color: rgba(76, 77, 77, 0.1);
}
 3.公共的scss样式文件内,没有公共样式文件的话可以放在没有scoped的style标签内,有的话要保证在main.js里引入了
/* el-table表格组件样式 */
.common-table {

    /* 表格加载中的背景 */
    .el-loading-mask {
        background-color: transparent;
    }

    /** 设置表格暂无数据样式 */
    .el-table__empty-block {
        background-color: transparent;
        color: #a8bfd5;
        letter-spacing: 2px;
    }

    /** 修改表头多选样式 */
    .el-checkbox__inner {
        background-color: transparent;
    }

    .el-checkbox__inner:hover {
        border: 1px solid #6d90ae;
    }

    .el-checkbox__input.is-checked .el-checkbox__inner {
        background-color: #1173be;
    }

    /* 设置表头样式 */
    &.el-table .el-table__header-wrapper th {
        color: #85b4e6;
        font-weight: normal;
        font-size: var(--font-size-base);
        letter-spacing: 2px;
        background-color: transparent !important;
        background: url("/images/imagine/table-header.png") center no-repeat;
        background-size: 100% 100%;
        border-bottom: 1px solid #0b4f85;
        box-sizing: border-box;
    }

    /** 设置表格的行背景色 */
    .el-table__row {
        background: url("/images/imagine/table-row1.png") center no-repeat;
        background-size: 100% 100%;
    }

    /** 去掉每一行的底边border */
    &.el-table td.el-table__cell {
        color: #aec4da;
        border-bottom: 1px solid #0b4f85;
        font-size: var(--font-size-base);
        letter-spacing: 2px;
    }

    /* 修改表格上侧和左侧的border */
    &.el-table--border,
    &.el-table--group,
    /* 修改表格右侧和底侧的border */
    &.el-table--border:after,
    &.el-table--group:after,
    &.el-table:before {
        border-color: transparent;
    }

    /* 删除表格右侧的border */
    &.el-table--border::after {
        width: 0;
    }

    /** 设置表格左侧第一列的边 */
    &.el-table td.el-table__cell:first-child {
        border-left: 1px solid #0b4f85;
    }

    /** 去掉表格的底边border */
    &.el-table::before {
        height: 0;
    }

    /** 去掉表格头部的border */
    &.el-table--border {
        border: none;
    }

    /** 表格内部每一列右侧border */
    &.el-table--border .el-table__cell {
        border-right: 1px solid #0b4f85;
    }

    /** 表格行的鼠标滑过样式 */
    &.el-table tbody tr:hover>td {
        background: url("/images/imagine/table-row2.png") center no-repeat !important;
        background-size: 100% 100% !important;
    }

    /* 去掉表格滚动条那一列的border */
    &.el-table--border th.el-table__cell.gutter:last-of-type {
        border-bottom-width: 0;
    }
}

  • 7
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要让 `el-table` 的横向滚动固定在屏幕底部,你可以在表格外部嵌套一个容器,用来固定表头和横向滚动条的位置,然后将表格放置在容器内部,并设置它的高度、宽度和 `overflow` 样式,用来实现纵向滚动。同时,你还需要通过 JavaScript 代码来动态计算容器的高度和横向滚动条的位置,以确保横向滚动条始终出现在屏幕底部。下面是一个示例代码: ```html <template> <div class="table-container"> <el-table ref="table" :data="tableData" style="width: 100%;" :row-class-name="tableRowClassName" > <!-- 表格列 --> </el-table> </div> </template> <script> export default { data() { return { tableData: [], // 表格数据 }; }, mounted() { this.$nextTick(() => { this.updateTableContainerHeight(); // 计算容器高度 window.addEventListener('resize', this.updateTableContainerHeight); // 监听窗口大小变化 const tableEl = this.$refs.table.$el.querySelector('.el-table__body-wrapper'); tableEl.addEventListener('scroll', this.updateTableContainerHeight); // 监听表格滚动事件 }); }, beforeDestroy() { window.removeEventListener('resize', this.updateTableContainerHeight); // 移除事件监听器 const tableEl = this.$refs.table.$el.querySelector('.el-table__body-wrapper'); tableEl.removeEventListener('scroll', this.updateTableContainerHeight); }, methods: { tableRowClassName({ row, rowIndex }) { // 自定义表格行样式 }, updateTableContainerHeight() { const tableContainerEl = this.$el.querySelector('.table-container'); const tableEl = this.$refs.table.$el.querySelector('.el-table__body-wrapper'); const offsetTop = tableContainerEl.getBoundingClientRect().top; const windowHeight = window.innerHeight; const tableHeight = tableEl.clientHeight; const scrollHeight = tableEl.scrollHeight; const bottom = windowHeight - offsetTop - tableHeight; if (scrollHeight > tableHeight) { tableContainerEl.style.height = `calc(100vh - ${offsetTop}px - ${bottom}px)`; tableEl.style.overflowX = 'scroll'; tableEl.style.overflowY = 'hidden'; tableEl.style.maxHeight = `calc(100vh - ${offsetTop}px - ${bottom}px - 17px)`; } else { tableContainerEl.style.height = ''; tableEl.style.overflowX = ''; tableEl.style.overflowY = ''; tableEl.style.maxHeight = ''; } }, }, }; </script> <style> .table-container { position: relative; overflow: hidden; } </style> ``` 在这个示例代码中,我们首先在表格外部嵌套了一个容器,并设置它的 `position` 样式为 `relative` 和 `overflow` 样式为 `hidden`,用来固定表头和横向滚动条的位置。然后,我们将表格放置在容器内部,并设置它的宽度为 `100%`,用来自适应容器的宽度。接着,我们在 `mounted` 钩子函数中计算容器的高度和横向滚动条的位置,并监听窗口大小变化和表格滚动事件,以动态更新容器的高度和横向滚动条的位置。最后,在 `beforeDestroy` 钩子函数中移除事件监听器,以避免内存泄漏。 需要注意的是,这个示例代码假设表格的纵向滚动条高度为 17px,如果你的表格纵向滚动条高度不同,你需要相应地调整代码中的计算公式。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值