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

金歌  金牌会员 | 2024-8-27 05:15:36 | 显示全部楼层 | 阅读模式
打印 上一主题 下一主题

主题 578|帖子 578|积分 1734

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


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(),不然会报错
  1. // 在获取到表格数据后,判断数据长度大于0后调用
  2. this.$nextTick(() => {
  3.    this.initRowHeight();
  4. });
  5. // 设置行高
  6.     initRowHeight() {
  7.       let tableHeight =
  8.         Math.round(this.$refs["tableRef"].$el.offsetHeight) -
  9.         Math.round(this.$refs["tableRef"].$el.childNodes[1].offsetHeight);
  10.       this.rowHeight = Math.floor(tableHeight / 10); // 返回小于等于最终结果的最大整数
  11.       setTimeout(() => {
  12.         if (this.$refs.tableRef) {
  13.           this.$refs.tableRef.doLayout();
  14.         }
  15.       }, 1000);
  16.     },
复制代码

2.写在有scoped 的style标签内

  1. /* 显示滚动条 */
  2. .category-table ::v-deep .el-table--scrollable-x .el-table__body-wrapper {
  3.   overflow-y: scroll;
  4. }
  5. /* 设置表格的滚动条宽度 */
  6. .category-table >>> .el-table__body-wrapper::-webkit-scrollbar {
  7.   width: 10px;
  8.   height: 8px;
  9. }
  10. /*定义滚动条轨道 内阴影+圆角*/
  11. .category-table >>> .el-table__body-wrapper::-webkit-scrollbar-track {
  12.   border-radius: 8px;
  13.   background-color: transparent;
  14. }
  15. /*定义滑块 内阴影+圆角*/
  16. .category-table >>> .el-table__body-wrapper::-webkit-scrollbar-thumb {
  17.   border-radius: 8px;
  18.   box-shadow: inset 0 0 6px rgba(200, 209, 217, 0.3);
  19.   background-color: rgba(76, 77, 77, 0.1);
  20. }
复制代码
 3.公共的scss样式文件内,没有公共样式文件的话可以放在没有scoped的style标签内,有的话要包管在main.js里引入了

  1. /* el-table表格组件样式 */
  2. .common-table {
  3.     /* 表格加载中的背景 */
  4.     .el-loading-mask {
  5.         background-color: transparent;
  6.     }
  7.     /** 设置表格暂无数据样式 */
  8.     .el-table__empty-block {
  9.         background-color: transparent;
  10.         color: #a8bfd5;
  11.         letter-spacing: 2px;
  12.     }
  13.     /** 修改表头多选样式 */
  14.     .el-checkbox__inner {
  15.         background-color: transparent;
  16.     }
  17.     .el-checkbox__inner:hover {
  18.         border: 1px solid #6d90ae;
  19.     }
  20.     .el-checkbox__input.is-checked .el-checkbox__inner {
  21.         background-color: #1173be;
  22.     }
  23.     /* 设置表头样式 */
  24.     &.el-table .el-table__header-wrapper th {
  25.         color: #85b4e6;
  26.         font-weight: normal;
  27.         font-size: var(--font-size-base);
  28.         letter-spacing: 2px;
  29.         background-color: transparent !important;
  30.         background: url("/images/imagine/table-header.png") center no-repeat;
  31.         background-size: 100% 100%;
  32.         border-bottom: 1px solid #0b4f85;
  33.         box-sizing: border-box;
  34.     }
  35.     /** 设置表格的行背景色 */
  36.     .el-table__row {
  37.         background: url("/images/imagine/table-row1.png") center no-repeat;
  38.         background-size: 100% 100%;
  39.     }
  40.     /** 去掉每一行的底边border */
  41.     &.el-table td.el-table__cell {
  42.         color: #aec4da;
  43.         border-bottom: 1px solid #0b4f85;
  44.         font-size: var(--font-size-base);
  45.         letter-spacing: 2px;
  46.     }
  47.     /* 修改表格上侧和左侧的border */
  48.     &.el-table--border,
  49.     &.el-table--group,
  50.     /* 修改表格右侧和底侧的border */
  51.     &.el-table--border:after,
  52.     &.el-table--group:after,
  53.     &.el-table:before {
  54.         border-color: transparent;
  55.     }
  56.     /* 删除表格右侧的border */
  57.     &.el-table--border::after {
  58.         width: 0;
  59.     }
  60.     /** 设置表格左侧第一列的边 */
  61.     &.el-table td.el-table__cell:first-child {
  62.         border-left: 1px solid #0b4f85;
  63.     }
  64.     /** 去掉表格的底边border */
  65.     &.el-table::before {
  66.         height: 0;
  67.     }
  68.     /** 去掉表格头部的border */
  69.     &.el-table--border {
  70.         border: none;
  71.     }
  72.     /** 表格内部每一列右侧border */
  73.     &.el-table--border .el-table__cell {
  74.         border-right: 1px solid #0b4f85;
  75.     }
  76.     /** 表格行的鼠标滑过样式 */
  77.     &.el-table tbody tr:hover>td {
  78.         background: url("/images/imagine/table-row2.png") center no-repeat !important;
  79.         background-size: 100% 100% !important;
  80.     }
  81.     /* 去掉表格滚动条那一列的border */
  82.     &.el-table--border th.el-table__cell.gutter:last-of-type {
  83.         border-bottom-width: 0;
  84.     }
  85. }
复制代码


免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

金歌

金牌会员
这个人很懒什么都没写!

标签云

快速回复 返回顶部 返回列表