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


    以下是基于Element UI的组件的实现方式:

    1. 中添加自定义的表头模板,并给表头模板绑定一个ref。
    1. <el-table-column prop="name">
    2. <template slot="header">
    3. <div ref="tableHeader" class="custom-header">
    4. {{ $column.label }}
    5. div>
    6. template>
    7. el-table-column>

    1. 在 mounted 钩子中获取表头单元格的行、列数,并根据黄金分割比例进行排列。
    1. mounted() {
    2. this.$nextTick(() => {
    3. const tableHeader = this.$refs.tableHeader;
    4. const headerWidth = tableHeader.offsetWidth;
    5. const headerHeight = tableHeader.offsetHeight;
    6. // 黄金分割比例
    7. const ratio = (Math.sqrt(5) - 1) / 2;
    8. // 计算行数和列数
    9. const rowCount = Math.ceil(1 / ratio);
    10. const colCount = Math.ceil(ratio);
    11. // 计算每个单元格的宽度和高度
    12. const cellWidth = headerWidth / colCount;
    13. const cellHeight = headerHeight / rowCount;
    14. // 将单元格位置设置为绝对定位
    15. tableHeader.style.position = 'relative';
    16. // 遍历每个单元格并设置它的位置
    17. let row = 0;
    18. let col = 0;
    19. Array.from(tableHeader.childNodes).forEach((cell) => {
    20. cell.style.position = 'absolute';
    21. cell.style.left = `${col * cellWidth}px`;
    22. cell.style.top = `${row * cellHeight}px`;
    23. col++;
    24. if (col === colCount) {
    25. col = 0;
    26. row++;
    27. }
    28. });
    29. });
    30. }

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

    1. <template>
    2. <el-table :data="tableData">
    3. <el-table-column prop="name">
    4. <template slot="header">
    5. <div ref="tableHeader" class="custom-header">
    6. {{ $column.label }}
    7. div>
    8. template>
    9. el-table-column>
    10. <el-table-column prop="age" label="Age">el-table-column>
    11. <el-table-column prop="gender" label="Gender">el-table-column>
    12. el-table>
    13. template>
    14. <script>
    15. export default {
    16. data() {
    17. return {
    18. tableData: [
    19. { name: 'John Doe', age: 30, gender: 'Male' },
    20. { name: 'Jane Doe', age: 25, gender: 'Female' },
    21. { name: 'Bob Smith', age: 40, gender: 'Male' },
    22. ],
    23. };
    24. },
    25. mounted() {
    26. this.$nextTick(() => {
    27. const tableHeader = this.$refs.tableHeader;
    28. const headerWidth = tableHeader.offsetWidth;
    29. const headerHeight = tableHeader.offsetHeight;
    30. // 黄金分割比例
    31. const ratio = (Math.sqrt(5) - 1) / 2;
    32. // 计算行数和列数
    33. const rowCount = Math.ceil(1 / ratio);
    34. const colCount = Math.ceil(ratio);
    35. // 计算每个单元格的宽度和高度
    36. const cellWidth = headerWidth / colCount;
    37. const cellHeight = headerHeight / rowCount;
    38. // 将单元格位置设置为绝对定位
    39. tableHeader.style.position = 'relative';
    40. // 遍历每个单元格并设置它的位置
    41. let row = 0;
    42. let col = 0;
    43. Array.from(tableHeader.childNodes).forEach((cell) => {
    44. cell.style.position = 'absolute';
    45. cell.style.left = `${col * cellWidth}px`;
    46. cell.style.top = `${row * cellHeight}px`;
    47. col++;
    48. if (col === colCount) {
    49. col = 0;
    50. row++;
    51. }
    52. });
    53. });
    54. },
    55. };
    56. script>
    57. <style scoped>
    58. .custom-header {
    59. display: flex;
    60. align-items: center;
    61. justify-content: center;
    62. font-weight: bold;
    63. font-size: 18px;
    64. }
    65. style>

  • 相关阅读:
    PMP知识的应用思考
    容器云多集群环境下如何实践 DevOps
    如何在 Linux 中检查我的网卡速度?
    JS——数组相关知识点汇总
    使用FastAPI部署Ultralytics YOLOv5模型
    腾讯云双11服务器优惠活动价格表预热!
    Java老码农心得:卷了这么多年,您真的卷会了吗?
    python绘制蕨菜叶分形
    深入探索地理空间查询:如何优雅地在MySQL、PostgreSQL及Redis中实现精准的地理数据存储与检索技巧
    世界杯小组赛频繁爆冷?这或许是强队的谋略 一分钟带你了解2022卡塔尔世界杯赛制
  • 原文地址:https://blog.csdn.net/u012632105/article/details/132967553