table固定表头 上下左右滑动

在这里插入图片描述

<style lang="scss">
.my-table {
  font-size: 16px;
  // width: 1000px;
  height: 200px;
  overflow: hidden;
  border: 1px solid #ccc;
  position: relative;
  margin-top: 50px;
  margin-left: 200px;
  text-align: center;
}
.first-table {
}
.top {
  overflow-x: auto;
  overflow-y: hidden;
}

.sec-table {
  overflow-y: auto;
  overflow-x: hidden;

  height: 200px;
  background: red;
}
</style>
<template>
  <div class="my-table" :style="{width:`${tableReallyWidth}px`}">
    <div class="top">
      <table cellspacing="0" cellpadding="0" border="0" class="first-table"
        :style="{width:`${tableWidth}px`}">
        <colgroup>
          <col v-for="(column, index) in columns" :key="index" :width="column.width">
        </colgroup>
        <thead>
          <tr>
            <th v-for="(column, index) in columns" :key="index">
              {{ column.title || '' }}
            </th>
          </tr>
        </thead>
      </table>
      <div class="sec-table" :style="{width:`${tableWidth}px`}">
        <table cellspacing="0" cellpadding="0" border="0">
          <colgroup>
            <col v-for="(column, index) in columns" :key="index" :width="column.width">
          </colgroup>
          <tbody>
            <tr v-for="(entry,index) in tableData" :key="index">
              <td v-for="(column, index2) in columns" :key="index2">
                <span v-if="entry[column.key]">{{entry[column.key]}}</span>
                <span v-else><button>编辑</button></span>
              </td>
            </tr>
          </tbody>
        </table>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: "table",
  data() {
    return {
      tableWidth: 0,
      tableReallyWidth: 700,
      columns: [
        {
          title: "姓名",
          key: "name",
          width: 100
        },
        {
          title: "年龄",
          key: "age",
          width: 100
        },
        {
          title: "省份",
          key: "province",
          width: 100
        },
        {
          title: "市区",
          key: "city",
          width: 200
        },
        {
          title: "地址",
          key: "address",
          width: 200
        },
        {
          title: "邮编",
          key: "zip",
          width: 100
        },
        {
          title: "操作",
          key: "action",
          fixed: "right",
          width: 100
        }
      ],
      tableData: []
    };
  },
  mounted() {
    this.$nextTick(() => {
      Array.from(new Array(20)).forEach(item =>
        this.tableData.push({
          province: "上海",
          city: "上海市",
          name: "张三",
          zip: "62352",
          age: 689,
          address: "江杨北路222号"
        })
      );

      this.columns.forEach(item => {
        this.tableWidth += +item.width;
      });
    });
  }
};
</script>


el-table 的固定列和滑动功能是通过设置 `fixed` 属性和 `scroll` 属性来实现的。 对于复杂表头,可以通过设置 `column-span` 属性来合并单元格,从而实现跨行或跨列的表头布局。同时,需要注意设置固定列时需要将对应的表头列也设置为固定。 以下是一个示例代码,展示了如何实现复杂表头的左右固定中间滑动功能: ```html <template> <el-table :data="tableData" style="width: 100%;" :column-key="columnKey" :height="tableHeight" :header-cell-style="{padding: '12px 0'}" :row-style="{height: '50px'}" :fixed="fixedColumns" :scroll="{ x: tableWidth, y: tableHeight }" > <el-table-column prop="name" label="姓名" width="150" fixed ></el-table-column> <el-table-column prop="age" label="年龄" width="150" fixed ></el-table-column> <el-table-column label="基本信息" :column-key="'basic'" :align="'center'" :width="basicWidth" :fixed="fixedColumns" > <el-table-column prop="gender" label="性别" :column-span="2" :align="'center'" ></el-table-column> <el-table-column prop="birthday" label="生日" :column-span="2" :align="'center'" ></el-table-column> </el-table-column> <el-table-column label="联系方式" :column-key="'contact'" :align="'center'" :width="contactWidth" :fixed="fixedColumns" > <el-table-column prop="phone" label="电话" :column-span="2" :align="'center'" ></el-table-column> <el-table-column prop="email" label="邮箱" ></el-table-column> </el-table-column> </el-table> </template> <script> export default { data() { return { tableData: [ { name: '张三', age: 18, gender: '男', birthday: '2000-01-01', phone: '12345678901', email: 'zhangsan@example.com' }, { name: '李四', age: 20, gender: '女', birthday: '1998-01-01', phone: '12345678902', email: 'lisi@example.com' } // ... ], columnKey: 'name', fixedColumns: 'left', // 左侧固定列 tableWidth: 700, // 表格总宽度 basicWidth: 300, // 基本信息列宽度 contactWidth: 400, // 联系方式列宽度 tableHeight: 400 // 表格高度 } } } </script> ``` 在上述代码中,我们设置了一个复杂表头,其中包含了四个表头列,使用 `column-span` 属性实现了跨行和跨列的合并布局。我们同时设置了两个左侧固定列,即姓名和年龄列,以及两个中间滑动的列,即基本信息和联系方式列。 为了实现固定列和滑动功能,我们在 `el-table-column` 组件中设置了 `fixed` 属性和 `scroll` 属性。其中,`fixed` 属性用于指定该列是否固定,可以设置为 `left`、`right` 或 `true`(表示左右都固定);`scroll` 属性用于指定表格的滚动区域大小,其中 `x` 属性表示横向滚动区域的宽度,`y` 属性表示纵向滚动区域的高度。 最后,我们需要设置表格的总宽度和高度,以及每个列的宽度。需要注意的是,在设置列宽度时,需要考虑到表头的合并布局。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值