Vue中實(shí)現(xiàn)table的首行首列固定

移動端需要表格展示數(shù)據(jù)時,需要滑動表格實(shí)現(xiàn)展示更多數(shù)據(jù)的目的,固定表格的首行和首列可在查看數(shù)據(jù)時更加直觀。

效果圖
效果圖
思路
分解圖

首先將表格分成左右兩部分,左邊第一列在上下滑動是header部分需要固定;右邊第一行在左右滑動時firstRowheader部分也需要是固定的。可將這幾個劃分區(qū)域分別用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>
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容