美文网首页
Vue中实现table的首行首列固定

Vue中实现table的首行首列固定

作者: 雩风二十八夜 | 来源:发表于2019-11-26 18:07 被阅读0次

移动端需要表格展示数据时,需要滑动表格实现展示更多数据的目的,固定表格的首行和首列可在查看数据时更加直观。

效果图
效果图
思路
分解图

首先将表格分成左右两部分,左边第一列在上下滑动是header部分需要固定;右边第一行在左右滑动时firstRowheader部分也需要是固定的。可将这几个划分区域分别用table填充,滑动tableBody时保持firstRowfirstCol的同步滑动即可。

<template>
  <div class="content-table">
    <div class="left-div">
      <div class="left-div1">
        <table>
          <tr>
            <th>{{ header }}</th>
          </tr>
        </table>
      </div>
      <div
        ref="firstColLayer"
        class="left-div2"
      >
        <table class="left-table2">
          <tr
            v-for="(col, index) in firstCol"
            :key="index"
          >
            <td>
              <p>{{ col }}</p>
            </td>
          </tr>
        </table>
      </div>
    </div>
    <div class="right-div">
      <div
        ref="firstRowLayer"
        class="right-div1"
      >
        <table class="right-table1">
          <tr>
            <th
              v-for="(row, index) in firstRow"
              :key="index"
            >
              {{ row }}
            </th>
          </tr>
        </table>
      </div>
      <div
        ref="tableContainer"
        class="right-div2"
        @scroll="tableScroll($event)"
      >
        <table class="right-table2">
          <tr
            v-for="(body,index) in tableBody"
            :key="index"
          >
            <td>{{ body.a }}</td>
            <td>{{ body.b }}</td>
            <td>{{ body.c }}</td>
            <td>{{ body.d }}</td>
          </tr>
        </table>
      </div>
    </div>
  </div>
</template>
<script>
export default {
    name: "TableComponent",
    props: {
        header: {
            type: String,
            default() {
                return "";
            },
        },
        firstCol: {
            type: Array,
            default() {
                return [];
            },
        },
        firstRow: {
            type: Array,
            default() {
                return [];
            },
        },
        tableBody: {
            type: Array,
            default() {
                return [];
            },
        },
    },
    methods: {        
        tableScroll() {
            const $target = this.$refs.tableContainer;
            // 首行固定
            this.$refs.firstRowLayer.scrollLeft = $target.scrollLeft;
            // 首列固定
            this.$refs.firstColLayer.scrollTop = $target.scrollTop;
        },
    },
}
</script>
<style scoped>
.content-table {
  padding-left: 15px;
  box-sizing: border-box;
  overflow-x: hidden;
}
table {
  border-collapse: collapse;
  margin: 0 auto;
  width: 100%;
  border-spacing: 0;
  font-size: 13px;
}
th {
  height: 40px;
  width: 100px;
  line-height: 40px;
  text-align: left;
  border-left: 1px solid #999;
  background: #d9d9d9;
}
td {
  word-break: break-all;
  word-wrap: break-word;
  width: 100px;
  height: 30px;
  line-height: 30px;
  text-indent: 20px;
  border-left: 1px solid #999;
}
tr {
  border-top: 1px solid #999;
}
.left-div {
  width: 80px;
  float: left;
}
.left-div1 {
  width: 100%;
}
.left-div2 {
  width: 100%;
  height: 300px;
  overflow: hidden;
}
.left-table2 {
  margin-bottom: 4px;
}
.right-div {
  float: left;
  width: 260px;
  margin-left: -1px;
}
.right-div1 {
  width: 100%;
  overflow: hidden;
}
.right-div2 {
  width: 100%;
  height: 300px;
  overflow: auto;
}
.right-table1, .right-table2 {
  width: 600px;
}
.right-table2 {
  overflow: hidden;
}
table tr:nth-child(odd) {
  background: #fff;
}
table tr:nth-child(even) {
  background: #d9d9d9;
}
</style>

相关文章