<el-table> 表头的字数多于5时,表头单元格的字符串的行、列数按近似黄金分割比例 (√5 - 1)/2 排列

以下是基于Element UI的<el-table>组件的实现方式:

  1. 在<el-table-column>中添加自定义的表头模板,并给表头模板绑定一个ref。
html 复制代码
<el-table-column prop="name">
  <template slot="header">
    <div ref="tableHeader" class="custom-header">
      {{ $column.label }}
    </div>
  </template>
</el-table-column>
  1. 在 mounted 钩子中获取表头单元格的行、列数,并根据黄金分割比例进行排列。
js 复制代码
mounted() {
  this.$nextTick(() => {
    const tableHeader = this.$refs.tableHeader;
    const headerWidth = tableHeader.offsetWidth;
    const headerHeight = tableHeader.offsetHeight;

    // 黄金分割比例
    const ratio = (Math.sqrt(5) - 1) / 2;

    // 计算行数和列数
    const rowCount = Math.ceil(1 / ratio);
    const colCount = Math.ceil(ratio);

    // 计算每个单元格的宽度和高度
    const cellWidth = headerWidth / colCount;
    const cellHeight = headerHeight / rowCount;

    // 将单元格位置设置为绝对定位
    tableHeader.style.position = 'relative';

    // 遍历每个单元格并设置它的位置
    let row = 0;
    let col = 0;
    Array.from(tableHeader.childNodes).forEach((cell) => {
      cell.style.position = 'absolute';
      cell.style.left = `${col * cellWidth}px`;
      cell.style.top = `${row * cellHeight}px`;
      col++;
      if (col === colCount) {
        col = 0;
        row++;
      }
    });
  });
}

完整的代码示例可以参考以下代码片段:

html 复制代码
<template>
  <el-table :data="tableData">
    <el-table-column prop="name">
      <template slot="header">
        <div ref="tableHeader" class="custom-header">
          {{ $column.label }}
        </div>
      </template>
    </el-table-column>
    <el-table-column prop="age" label="Age"></el-table-column>
    <el-table-column prop="gender" label="Gender"></el-table-column>
  </el-table>
</template>

<script>
export default {
  data() {
    return {
      tableData: [
        { name: 'John Doe', age: 30, gender: 'Male' },
        { name: 'Jane Doe', age: 25, gender: 'Female' },
        { name: 'Bob Smith', age: 40, gender: 'Male' },
      ],
    };
  },
  mounted() {
    this.$nextTick(() => {
      const tableHeader = this.$refs.tableHeader;
      const headerWidth = tableHeader.offsetWidth;
      const headerHeight = tableHeader.offsetHeight;

      // 黄金分割比例
      const ratio = (Math.sqrt(5) - 1) / 2;

      // 计算行数和列数
      const rowCount = Math.ceil(1 / ratio);
      const colCount = Math.ceil(ratio);

      // 计算每个单元格的宽度和高度
      const cellWidth = headerWidth / colCount;
      const cellHeight = headerHeight / rowCount;

      // 将单元格位置设置为绝对定位
      tableHeader.style.position = 'relative';

      // 遍历每个单元格并设置它的位置
      let row = 0;
      let col = 0;
      Array.from(tableHeader.childNodes).forEach((cell) => {
        cell.style.position = 'absolute';
        cell.style.left = `${col * cellWidth}px`;
        cell.style.top = `${row * cellHeight}px`;
        col++;
        if (col === colCount) {
          col = 0;
          row++;
        }
      });
    });
  },
};
</script>

<style scoped>
.custom-header {
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: bold;
  font-size: 18px;
}
</style>
相关推荐
子非鱼9211 分钟前
【JavaScript】LeetCode:41-45
开发语言·javascript·leetcode·链表·二叉树
小胖伦的夕阳粉34 分钟前
js 获取树节点上某节点的最底层叶子节点数据
开发语言·javascript·ecmascript
@听风吟1 小时前
力扣之182.查找重复的电子邮箱
大数据·javascript·数据库·sql·leetcode
喝旺仔la1 小时前
axios相关知识点
elementui
码力码力我爱你1 小时前
QT + WebAssembly + Vue环境搭建
vue.js·vue·wasm·webassembly·emscripten
唐家小妹1 小时前
【flex-grow】计算 flex弹性盒子的子元素的宽度大小
前端·javascript·css·html
洛千陨1 小时前
element-plus弹窗内分页表格保留勾选项
前端·javascript·vue.js
小小19922 小时前
elementui 单元格添加样式的两种方法
前端·javascript·elementui
完球了2 小时前
【Day02-JS+Vue+Ajax】
javascript·vue.js·笔记·学习·ajax
前端没钱2 小时前
若依Nodejs后台、实现90%以上接口,附体验地址、源码、拓展特色功能
前端·javascript·vue.js·node.js