21a638379608e6743762edcb3b94ff6c55a46df113962389316ae889e97038da55600b76e515d224a7b8251af6a5a5051d2f8ccf71558422328e3cab00315f 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. import { defineComponent, getCurrentInstance, inject, ref, reactive, watch, onBeforeUnmount, onMounted, nextTick, h } from 'vue';
  2. import { ElCheckbox } from '../../../checkbox/index.mjs';
  3. import FilterPanel from '../filter-panel.mjs';
  4. import useLayoutObserver from '../layout-observer.mjs';
  5. import { TABLE_INJECTION_KEY } from '../tokens.mjs';
  6. import useEvent from './event-helper.mjs';
  7. import useStyle from './style.helper.mjs';
  8. import useUtils from './utils-helper.mjs';
  9. import { useNamespace } from '../../../../hooks/use-namespace/index.mjs';
  10. var TableHeader = defineComponent({
  11. name: "ElTableHeader",
  12. components: {
  13. ElCheckbox
  14. },
  15. props: {
  16. fixed: {
  17. type: String,
  18. default: ""
  19. },
  20. store: {
  21. required: true,
  22. type: Object
  23. },
  24. border: Boolean,
  25. defaultSort: {
  26. type: Object,
  27. default: () => {
  28. return {
  29. prop: "",
  30. order: ""
  31. };
  32. }
  33. },
  34. appendFilterPanelTo: {
  35. type: String
  36. },
  37. allowDragLastColumn: {
  38. type: Boolean
  39. }
  40. },
  41. setup(props, { emit }) {
  42. const instance = getCurrentInstance();
  43. const parent = inject(TABLE_INJECTION_KEY);
  44. const ns = useNamespace("table");
  45. const filterPanels = ref({});
  46. const { onColumnsChange, onScrollableChange } = useLayoutObserver(parent);
  47. const isTableLayoutAuto = (parent == null ? void 0 : parent.props.tableLayout) === "auto";
  48. const saveIndexSelection = reactive(/* @__PURE__ */ new Map());
  49. const theadRef = ref();
  50. let delayId;
  51. const updateFixedColumnStyle = () => {
  52. delayId = setTimeout(() => {
  53. if (saveIndexSelection.size > 0) {
  54. saveIndexSelection.forEach((column, key) => {
  55. const el = theadRef.value.querySelector(`.${key.replace(/\s/g, ".")}`);
  56. if (el) {
  57. const width = el.getBoundingClientRect().width;
  58. column.width = width || column.width;
  59. }
  60. });
  61. saveIndexSelection.clear();
  62. }
  63. });
  64. };
  65. watch(saveIndexSelection, updateFixedColumnStyle);
  66. onBeforeUnmount(() => {
  67. if (delayId) {
  68. clearTimeout(delayId);
  69. delayId = void 0;
  70. }
  71. });
  72. onMounted(async () => {
  73. await nextTick();
  74. await nextTick();
  75. const { prop, order } = props.defaultSort;
  76. parent == null ? void 0 : parent.store.commit("sort", { prop, order, init: true });
  77. updateFixedColumnStyle();
  78. });
  79. const {
  80. handleHeaderClick,
  81. handleHeaderContextMenu,
  82. handleMouseDown,
  83. handleMouseMove,
  84. handleMouseOut,
  85. handleSortClick,
  86. handleFilterClick
  87. } = useEvent(props, emit);
  88. const {
  89. getHeaderRowStyle,
  90. getHeaderRowClass,
  91. getHeaderCellStyle,
  92. getHeaderCellClass
  93. } = useStyle(props);
  94. const { isGroup, toggleAllSelection, columnRows } = useUtils(props);
  95. instance.state = {
  96. onColumnsChange,
  97. onScrollableChange
  98. };
  99. instance.filterPanels = filterPanels;
  100. return {
  101. ns,
  102. filterPanels,
  103. onColumnsChange,
  104. onScrollableChange,
  105. columnRows,
  106. getHeaderRowClass,
  107. getHeaderRowStyle,
  108. getHeaderCellClass,
  109. getHeaderCellStyle,
  110. handleHeaderClick,
  111. handleHeaderContextMenu,
  112. handleMouseDown,
  113. handleMouseMove,
  114. handleMouseOut,
  115. handleSortClick,
  116. handleFilterClick,
  117. isGroup,
  118. toggleAllSelection,
  119. saveIndexSelection,
  120. isTableLayoutAuto,
  121. theadRef,
  122. updateFixedColumnStyle
  123. };
  124. },
  125. render() {
  126. const {
  127. ns,
  128. isGroup,
  129. columnRows,
  130. getHeaderCellStyle,
  131. getHeaderCellClass,
  132. getHeaderRowClass,
  133. getHeaderRowStyle,
  134. handleHeaderClick,
  135. handleHeaderContextMenu,
  136. handleMouseDown,
  137. handleMouseMove,
  138. handleSortClick,
  139. handleMouseOut,
  140. store,
  141. $parent,
  142. saveIndexSelection,
  143. isTableLayoutAuto
  144. } = this;
  145. let rowSpan = 1;
  146. return h("thead", {
  147. ref: "theadRef",
  148. class: { [ns.is("group")]: isGroup }
  149. }, columnRows.map((subColumns, rowIndex) => h("tr", {
  150. class: getHeaderRowClass(rowIndex),
  151. key: rowIndex,
  152. style: getHeaderRowStyle(rowIndex)
  153. }, subColumns.map((column, cellIndex) => {
  154. if (column.rowSpan > rowSpan) {
  155. rowSpan = column.rowSpan;
  156. }
  157. const _class = getHeaderCellClass(rowIndex, cellIndex, subColumns, column);
  158. if (isTableLayoutAuto && column.fixed) {
  159. saveIndexSelection.set(_class, column);
  160. }
  161. return h("th", {
  162. class: _class,
  163. colspan: column.colSpan,
  164. key: `${column.id}-thead`,
  165. rowspan: column.rowSpan,
  166. style: getHeaderCellStyle(rowIndex, cellIndex, subColumns, column),
  167. onClick: ($event) => {
  168. var _a;
  169. if ((_a = $event.currentTarget) == null ? void 0 : _a.classList.contains("noclick")) {
  170. return;
  171. }
  172. handleHeaderClick($event, column);
  173. },
  174. onContextmenu: ($event) => handleHeaderContextMenu($event, column),
  175. onMousedown: ($event) => handleMouseDown($event, column),
  176. onMousemove: ($event) => handleMouseMove($event, column),
  177. onMouseout: handleMouseOut
  178. }, [
  179. h("div", {
  180. class: [
  181. "cell",
  182. column.filteredValue && column.filteredValue.length > 0 ? "highlight" : ""
  183. ]
  184. }, [
  185. column.renderHeader ? column.renderHeader({
  186. column,
  187. $index: cellIndex,
  188. store,
  189. _self: $parent
  190. }) : column.label,
  191. column.sortable && h("span", {
  192. onClick: ($event) => handleSortClick($event, column),
  193. class: "caret-wrapper"
  194. }, [
  195. h("i", {
  196. onClick: ($event) => handleSortClick($event, column, "ascending"),
  197. class: "sort-caret ascending"
  198. }),
  199. h("i", {
  200. onClick: ($event) => handleSortClick($event, column, "descending"),
  201. class: "sort-caret descending"
  202. })
  203. ]),
  204. column.filterable && h(FilterPanel, {
  205. store,
  206. placement: column.filterPlacement || "bottom-start",
  207. appendTo: $parent == null ? void 0 : $parent.appendFilterPanelTo,
  208. column,
  209. upDataColumn: (key, value) => {
  210. column[key] = value;
  211. }
  212. }, {
  213. "filter-icon": () => column.renderFilterIcon ? column.renderFilterIcon({
  214. filterOpened: column.filterOpened
  215. }) : null
  216. })
  217. ])
  218. ]);
  219. }))));
  220. }
  221. });
  222. export { TableHeader as default };
  223. //# sourceMappingURL=index.mjs.map