Header.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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. var rows = [];
  8. function fillRowCells(columns, colIndex) {
  9. var rowIndex = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 0;
  10. // Init rows
  11. rows[rowIndex] = rows[rowIndex] || [];
  12. var currentColIndex = colIndex;
  13. var colSpans = columns.filter(Boolean).map(function (column) {
  14. var cell = {
  15. key: column.key,
  16. class: classNames(column.className, column.class),
  17. // children: column.title,
  18. column: column,
  19. colStart: currentColIndex
  20. };
  21. var colSpan = 1;
  22. var subColumns = column.children;
  23. if (subColumns && subColumns.length > 0) {
  24. colSpan = fillRowCells(subColumns, currentColIndex, rowIndex + 1).reduce(function (total, count) {
  25. return total + count;
  26. }, 0);
  27. cell.hasSubColumns = true;
  28. }
  29. if ('colSpan' in column) {
  30. colSpan = column.colSpan;
  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. var rowCount = rows.length;
  47. var _loop = function _loop(rowIndex) {
  48. rows[rowIndex].forEach(function (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. for (var rowIndex = 0; rowIndex < rowCount; rowIndex += 1) {
  56. _loop(rowIndex);
  57. }
  58. return rows;
  59. }
  60. export default defineComponent({
  61. name: 'Header',
  62. inheritAttrs: false,
  63. props: ['columns', 'flattenColumns', 'stickyOffsets', 'customHeaderRow'],
  64. setup: function setup(props) {
  65. var tableContext = useInjectTable();
  66. var rows = computed(function () {
  67. return parseHeaderRows(props.columns);
  68. });
  69. return function () {
  70. var prefixCls = tableContext.prefixCls,
  71. getComponent = tableContext.getComponent;
  72. var stickyOffsets = props.stickyOffsets,
  73. flattenColumns = props.flattenColumns,
  74. customHeaderRow = props.customHeaderRow;
  75. var WrapperComponent = getComponent(['header', 'wrapper'], 'thead');
  76. var trComponent = getComponent(['header', 'row'], 'tr');
  77. var thComponent = getComponent(['header', 'cell'], 'th');
  78. return _createVNode(WrapperComponent, {
  79. "class": "".concat(prefixCls, "-thead")
  80. }, {
  81. default: function _default() {
  82. return [rows.value.map(function (row, rowIndex) {
  83. var rowNode = _createVNode(HeaderRow, {
  84. "key": rowIndex,
  85. "flattenColumns": flattenColumns,
  86. "cells": row,
  87. "stickyOffsets": stickyOffsets,
  88. "rowComponent": trComponent,
  89. "cellComponent": thComponent,
  90. "customHeaderRow": customHeaderRow,
  91. "index": rowIndex
  92. }, null);
  93. return rowNode;
  94. })];
  95. }
  96. });
  97. };
  98. }
  99. });