Header.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. import { createVNode as _createVNode } from "vue";
  2. import classNames from '../../_util/classNames';
  3. import { computed, defineComponent } from 'vue';
  4. import { useInjectTable } from '../context/TableContext';
  5. import HeaderRow from './HeaderRow';
  6. function parseHeaderRows(rootColumns) {
  7. const rows = [];
  8. function fillRowCells(columns, colIndex) {
  9. let rowIndex = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 0;
  10. // Init rows
  11. rows[rowIndex] = rows[rowIndex] || [];
  12. let currentColIndex = colIndex;
  13. const colSpans = columns.filter(Boolean).map(column => {
  14. const cell = {
  15. key: column.key,
  16. class: classNames(column.className, column.class),
  17. // children: column.title,
  18. column,
  19. colStart: currentColIndex
  20. };
  21. let colSpan = 1;
  22. const subColumns = column.children;
  23. if (subColumns && subColumns.length > 0) {
  24. colSpan = fillRowCells(subColumns, currentColIndex, rowIndex + 1).reduce((total, count) => total + count, 0);
  25. cell.hasSubColumns = true;
  26. }
  27. if ('colSpan' in column) {
  28. ({
  29. colSpan
  30. } = column);
  31. }
  32. if ('rowSpan' in column) {
  33. cell.rowSpan = column.rowSpan;
  34. }
  35. cell.colSpan = colSpan;
  36. cell.colEnd = cell.colStart + colSpan - 1;
  37. rows[rowIndex].push(cell);
  38. currentColIndex += colSpan;
  39. return colSpan;
  40. });
  41. return colSpans;
  42. }
  43. // Generate `rows` cell data
  44. fillRowCells(rootColumns, 0);
  45. // Handle `rowSpan`
  46. const rowCount = rows.length;
  47. for (let rowIndex = 0; rowIndex < rowCount; rowIndex += 1) {
  48. rows[rowIndex].forEach(cell => {
  49. if (!('rowSpan' in cell) && !cell.hasSubColumns) {
  50. // eslint-disable-next-line no-param-reassign
  51. cell.rowSpan = rowCount - rowIndex;
  52. }
  53. });
  54. }
  55. return rows;
  56. }
  57. export default defineComponent({
  58. name: 'TableHeader',
  59. inheritAttrs: false,
  60. props: ['columns', 'flattenColumns', 'stickyOffsets', 'customHeaderRow'],
  61. setup(props) {
  62. const tableContext = useInjectTable();
  63. const rows = computed(() => parseHeaderRows(props.columns));
  64. return () => {
  65. const {
  66. prefixCls,
  67. getComponent
  68. } = tableContext;
  69. const {
  70. stickyOffsets,
  71. flattenColumns,
  72. customHeaderRow
  73. } = props;
  74. const WrapperComponent = getComponent(['header', 'wrapper'], 'thead');
  75. const trComponent = getComponent(['header', 'row'], 'tr');
  76. const thComponent = getComponent(['header', 'cell'], 'th');
  77. return _createVNode(WrapperComponent, {
  78. "class": `${prefixCls}-thead`
  79. }, {
  80. default: () => [rows.value.map((row, rowIndex) => {
  81. const rowNode = _createVNode(HeaderRow, {
  82. "key": rowIndex,
  83. "flattenColumns": flattenColumns,
  84. "cells": row,
  85. "stickyOffsets": stickyOffsets,
  86. "rowComponent": trComponent,
  87. "cellComponent": thComponent,
  88. "customHeaderRow": customHeaderRow,
  89. "index": rowIndex
  90. }, null);
  91. return rowNode;
  92. })]
  93. });
  94. };
  95. }
  96. });