StickyTable.vue 3.93 KB
<template>
  <div class="sticky-table-container" :style="{ height: height }">
    <div class="sticky-table-wrapper" :style="{ height: data.length > 5 ? '100%' : '' }">
      <table class="sticky-table">
        <thead>
          <tr>
            <th v-for="(col, index) in columns" :key="index" :class="{ 'fixed-col': index === 0 && fixedFirstColumn }" :style="getHeaderStyle(index)">
              {{ col.label }}
            </th>
          </tr>
        </thead>
        <tbody>
          <tr v-for="(row, rowIndex) in data" :key="rowIndex">
            <td v-for="(col, colIndex) in columns" :key="colIndex" :class="{ 'fixed-col': colIndex === 0 && fixedFirstColumn }" :style="getCellStyle(colIndex)">
              <div class="cell-content">
                <slot :name="col.prop" :row="row" :index="rowIndex">
                  {{ col.formatter ? col.formatter(row) : (row[col.prop] || '-') }}
                </slot>
              </div>
            </td>
          </tr>
        </tbody>
      </table>
    </div>
  </div>
</template>

<script>
export default {
  name: 'StickyTable',
  props: {
    // 列定义 [{ label: '名称', prop: 'name', width: '100px' }]
    columns: {
      type: Array,
      required: true
    },
    // 数据源
    data: {
      type: Array,
      default: () => []
    },
    // 表格高度
    height: {
      type: String,
      default: 'auto'
    },
    // 是否固定第一列
    fixedFirstColumn: {
      type: Boolean,
      default: true
    }
  },
  methods: {
    getHeaderStyle(index) {
      const style = {};
      if (this.columns[index].width) {
        style.minWidth = this.columns[index].width
        style.maxWidth = this.columns[index].width
      }
      return style
    },
    getCellStyle(index) {
      const style = {}
      // 如果有其他样式逻辑可以在这里添加
      return style
    }
  }
}
</script>

<style lang="scss" scoped>
.sticky-table-container {
  overflow: hidden;
  position: relative;
  width: 100%;
  background: #fff;
}

.sticky-table-wrapper {
  overflow: auto;
  // height: 100%;

  .sticky-table {
    border-collapse: separate; // sticky需要separate或collapse配合特定处理,separate较安全
    border-spacing: 0;
    min-width: 100%;

    th,
    td {
      box-sizing: border-box;
      padding: 0 15px; // 调整为水平内边距
      height: 15px;
      background: #fff;
      text-align: left;
      color: #333333;
      font-size: 12px;
    }

    th {
      line-height: 15px; // 表头单行垂直居中
      white-space: nowrap;
      overflow: hidden;
      text-overflow: ellipsis;
    }

    td {
      vertical-align: middle; // 内容垂直居中
    }

    .cell-content {
      display: -webkit-box;
      -webkit-box-orient: vertical;
      -webkit-line-clamp: 2;
      overflow: hidden;
      text-overflow: ellipsis;
      word-break: break-all;
      line-height: 1.4;
      white-space: normal;
    }

    // 最后一列去掉右边框
    // th:last-child,
    // td:last-child {
    //   border-right: none;
    // }
    th:first-child,
    td:first-child {
      padding: 4px 8px 4px 0;
    }

    th {
      position: sticky;
      top: 0;
      z-index: 10;
      color: #999999;
      font-size: 12px;
      padding-top: 0;
    }

    // 固定第一列样式
    .fixed-col {
      position: sticky;
      left: 0;
      z-index: 5;
      background: #fff; // 必须指定背景色,否则透明
      // border-right: 1px solid #ebedf0;
      box-shadow: 2px 0 0 0 #adadad1a;

      // 添加阴影效果增加层次感
      &::after {
        content: '';
        position: absolute;
        top: 0;
        right: -4px;
        bottom: 0;
        width: 4px;
        // background: linear-gradient(90deg, rgba(0, 0, 0, 0.05), transparent      );
        pointer-events: none;
      }
    }

    // 表头和第一列交叉的单元格
    th.fixed-col {
      padding: 4px 8px 4px 0;
      z-index: 15; // 最高层级
      background: #fff;
    }
  }
}
</style>