table.mjs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  1. import { defineComponent, getCurrentInstance, provide, computed, onBeforeUnmount, resolveComponent, resolveDirective, openBlock, createElementBlock, normalizeClass, normalizeStyle, createElementVNode, renderSlot, withDirectives, createVNode, createCommentVNode, withCtx, createBlock, createTextVNode, toDisplayString, vShow } from 'vue';
  2. import { debounce } from 'lodash-unified';
  3. import { ElScrollbar } from '../../scrollbar/index.mjs';
  4. import { createStore } from './store/helper.mjs';
  5. import TableLayout from './table-layout.mjs';
  6. import TableHeader from './table-header/index.mjs';
  7. import TableBody from './table-body/index.mjs';
  8. import TableFooter from './table-footer/index.mjs';
  9. import useUtils from './table/utils-helper.mjs';
  10. import { convertToRows } from './table-header/utils-helper.mjs';
  11. import useStyle from './table/style-helper.mjs';
  12. import useKeyRender from './table/key-render-helper.mjs';
  13. import defaultProps from './table/defaults.mjs';
  14. import { TABLE_INJECTION_KEY } from './tokens.mjs';
  15. import { hColgroup } from './h-helper.mjs';
  16. import { useScrollbar } from './composables/use-scrollbar.mjs';
  17. import _export_sfc from '../../../_virtual/plugin-vue_export-helper.mjs';
  18. import Mousewheel from '../../../directives/mousewheel/index.mjs';
  19. import { useLocale } from '../../../hooks/use-locale/index.mjs';
  20. import { useNamespace } from '../../../hooks/use-namespace/index.mjs';
  21. let tableIdSeed = 1;
  22. const _sfc_main = defineComponent({
  23. name: "ElTable",
  24. directives: {
  25. Mousewheel
  26. },
  27. components: {
  28. TableHeader,
  29. TableBody,
  30. TableFooter,
  31. ElScrollbar,
  32. hColgroup
  33. },
  34. props: defaultProps,
  35. emits: [
  36. "select",
  37. "select-all",
  38. "selection-change",
  39. "cell-mouse-enter",
  40. "cell-mouse-leave",
  41. "cell-contextmenu",
  42. "cell-click",
  43. "cell-dblclick",
  44. "row-click",
  45. "row-contextmenu",
  46. "row-dblclick",
  47. "header-click",
  48. "header-contextmenu",
  49. "sort-change",
  50. "filter-change",
  51. "current-change",
  52. "header-dragend",
  53. "expand-change",
  54. "scroll"
  55. ],
  56. setup(props) {
  57. const { t } = useLocale();
  58. const ns = useNamespace("table");
  59. const table = getCurrentInstance();
  60. provide(TABLE_INJECTION_KEY, table);
  61. const store = createStore(table, props);
  62. table.store = store;
  63. const layout = new TableLayout({
  64. store: table.store,
  65. table,
  66. fit: props.fit,
  67. showHeader: props.showHeader
  68. });
  69. table.layout = layout;
  70. const isEmpty = computed(() => (store.states.data.value || []).length === 0);
  71. const {
  72. setCurrentRow,
  73. getSelectionRows,
  74. toggleRowSelection,
  75. clearSelection,
  76. clearFilter,
  77. toggleAllSelection,
  78. toggleRowExpansion,
  79. clearSort,
  80. sort,
  81. updateKeyChildren
  82. } = useUtils(store);
  83. const {
  84. isHidden,
  85. renderExpanded,
  86. setDragVisible,
  87. isGroup,
  88. handleMouseLeave,
  89. handleHeaderFooterMousewheel,
  90. tableSize,
  91. emptyBlockStyle,
  92. handleFixedMousewheel,
  93. resizeProxyVisible,
  94. bodyWidth,
  95. resizeState,
  96. doLayout,
  97. tableBodyStyles,
  98. tableLayout,
  99. scrollbarViewStyle,
  100. scrollbarStyle
  101. } = useStyle(props, layout, store, table);
  102. const { scrollBarRef, scrollTo, setScrollLeft, setScrollTop } = useScrollbar();
  103. const debouncedUpdateLayout = debounce(doLayout, 50);
  104. const tableId = `${ns.namespace.value}-table_${tableIdSeed++}`;
  105. table.tableId = tableId;
  106. table.state = {
  107. isGroup,
  108. resizeState,
  109. doLayout,
  110. debouncedUpdateLayout
  111. };
  112. const computedSumText = computed(() => {
  113. var _a;
  114. return (_a = props.sumText) != null ? _a : t("el.table.sumText");
  115. });
  116. const computedEmptyText = computed(() => {
  117. var _a;
  118. return (_a = props.emptyText) != null ? _a : t("el.table.emptyText");
  119. });
  120. const columns = computed(() => {
  121. return convertToRows(store.states.originColumns.value)[0];
  122. });
  123. useKeyRender(table);
  124. onBeforeUnmount(() => {
  125. debouncedUpdateLayout.cancel();
  126. });
  127. return {
  128. ns,
  129. layout,
  130. store,
  131. columns,
  132. handleHeaderFooterMousewheel,
  133. handleMouseLeave,
  134. tableId,
  135. tableSize,
  136. isHidden,
  137. isEmpty,
  138. renderExpanded,
  139. resizeProxyVisible,
  140. resizeState,
  141. isGroup,
  142. bodyWidth,
  143. tableBodyStyles,
  144. emptyBlockStyle,
  145. debouncedUpdateLayout,
  146. handleFixedMousewheel,
  147. setCurrentRow,
  148. getSelectionRows,
  149. toggleRowSelection,
  150. clearSelection,
  151. clearFilter,
  152. toggleAllSelection,
  153. toggleRowExpansion,
  154. clearSort,
  155. doLayout,
  156. sort,
  157. updateKeyChildren,
  158. t,
  159. setDragVisible,
  160. context: table,
  161. computedSumText,
  162. computedEmptyText,
  163. tableLayout,
  164. scrollbarViewStyle,
  165. scrollbarStyle,
  166. scrollBarRef,
  167. scrollTo,
  168. setScrollLeft,
  169. setScrollTop,
  170. allowDragLastColumn: props.allowDragLastColumn
  171. };
  172. }
  173. });
  174. function _sfc_render(_ctx, _cache, $props, $setup, $data, $options) {
  175. const _component_hColgroup = resolveComponent("hColgroup");
  176. const _component_table_header = resolveComponent("table-header");
  177. const _component_table_body = resolveComponent("table-body");
  178. const _component_table_footer = resolveComponent("table-footer");
  179. const _component_el_scrollbar = resolveComponent("el-scrollbar");
  180. const _directive_mousewheel = resolveDirective("mousewheel");
  181. return openBlock(), createElementBlock("div", {
  182. ref: "tableWrapper",
  183. class: normalizeClass([
  184. {
  185. [_ctx.ns.m("fit")]: _ctx.fit,
  186. [_ctx.ns.m("striped")]: _ctx.stripe,
  187. [_ctx.ns.m("border")]: _ctx.border || _ctx.isGroup,
  188. [_ctx.ns.m("hidden")]: _ctx.isHidden,
  189. [_ctx.ns.m("group")]: _ctx.isGroup,
  190. [_ctx.ns.m("fluid-height")]: _ctx.maxHeight,
  191. [_ctx.ns.m("scrollable-x")]: _ctx.layout.scrollX.value,
  192. [_ctx.ns.m("scrollable-y")]: _ctx.layout.scrollY.value,
  193. [_ctx.ns.m("enable-row-hover")]: !_ctx.store.states.isComplex.value,
  194. [_ctx.ns.m("enable-row-transition")]: (_ctx.store.states.data.value || []).length !== 0 && (_ctx.store.states.data.value || []).length < 100,
  195. "has-footer": _ctx.showSummary
  196. },
  197. _ctx.ns.m(_ctx.tableSize),
  198. _ctx.className,
  199. _ctx.ns.b(),
  200. _ctx.ns.m(`layout-${_ctx.tableLayout}`)
  201. ]),
  202. style: normalizeStyle(_ctx.style),
  203. "data-prefix": _ctx.ns.namespace.value,
  204. onMouseleave: _ctx.handleMouseLeave
  205. }, [
  206. createElementVNode("div", {
  207. class: normalizeClass(_ctx.ns.e("inner-wrapper"))
  208. }, [
  209. createElementVNode("div", {
  210. ref: "hiddenColumns",
  211. class: "hidden-columns"
  212. }, [
  213. renderSlot(_ctx.$slots, "default")
  214. ], 512),
  215. _ctx.showHeader && _ctx.tableLayout === "fixed" ? withDirectives((openBlock(), createElementBlock("div", {
  216. key: 0,
  217. ref: "headerWrapper",
  218. class: normalizeClass(_ctx.ns.e("header-wrapper"))
  219. }, [
  220. createElementVNode("table", {
  221. ref: "tableHeader",
  222. class: normalizeClass(_ctx.ns.e("header")),
  223. style: normalizeStyle(_ctx.tableBodyStyles),
  224. border: "0",
  225. cellpadding: "0",
  226. cellspacing: "0"
  227. }, [
  228. createVNode(_component_hColgroup, {
  229. columns: _ctx.store.states.columns.value,
  230. "table-layout": _ctx.tableLayout
  231. }, null, 8, ["columns", "table-layout"]),
  232. createVNode(_component_table_header, {
  233. ref: "tableHeaderRef",
  234. border: _ctx.border,
  235. "default-sort": _ctx.defaultSort,
  236. store: _ctx.store,
  237. "append-filter-panel-to": _ctx.appendFilterPanelTo,
  238. "allow-drag-last-column": _ctx.allowDragLastColumn,
  239. onSetDragVisible: _ctx.setDragVisible
  240. }, null, 8, ["border", "default-sort", "store", "append-filter-panel-to", "allow-drag-last-column", "onSetDragVisible"])
  241. ], 6)
  242. ], 2)), [
  243. [_directive_mousewheel, _ctx.handleHeaderFooterMousewheel]
  244. ]) : createCommentVNode("v-if", true),
  245. createElementVNode("div", {
  246. ref: "bodyWrapper",
  247. class: normalizeClass(_ctx.ns.e("body-wrapper"))
  248. }, [
  249. createVNode(_component_el_scrollbar, {
  250. ref: "scrollBarRef",
  251. "view-style": _ctx.scrollbarViewStyle,
  252. "wrap-style": _ctx.scrollbarStyle,
  253. always: _ctx.scrollbarAlwaysOn,
  254. tabindex: _ctx.scrollbarTabindex,
  255. onScroll: ($event) => _ctx.$emit("scroll", $event)
  256. }, {
  257. default: withCtx(() => [
  258. createElementVNode("table", {
  259. ref: "tableBody",
  260. class: normalizeClass(_ctx.ns.e("body")),
  261. cellspacing: "0",
  262. cellpadding: "0",
  263. border: "0",
  264. style: normalizeStyle({
  265. width: _ctx.bodyWidth,
  266. tableLayout: _ctx.tableLayout
  267. })
  268. }, [
  269. createVNode(_component_hColgroup, {
  270. columns: _ctx.store.states.columns.value,
  271. "table-layout": _ctx.tableLayout
  272. }, null, 8, ["columns", "table-layout"]),
  273. _ctx.showHeader && _ctx.tableLayout === "auto" ? (openBlock(), createBlock(_component_table_header, {
  274. key: 0,
  275. ref: "tableHeaderRef",
  276. class: normalizeClass(_ctx.ns.e("body-header")),
  277. border: _ctx.border,
  278. "default-sort": _ctx.defaultSort,
  279. store: _ctx.store,
  280. "append-filter-panel-to": _ctx.appendFilterPanelTo,
  281. onSetDragVisible: _ctx.setDragVisible
  282. }, null, 8, ["class", "border", "default-sort", "store", "append-filter-panel-to", "onSetDragVisible"])) : createCommentVNode("v-if", true),
  283. createVNode(_component_table_body, {
  284. context: _ctx.context,
  285. highlight: _ctx.highlightCurrentRow,
  286. "row-class-name": _ctx.rowClassName,
  287. "tooltip-effect": _ctx.tooltipEffect,
  288. "tooltip-options": _ctx.tooltipOptions,
  289. "row-style": _ctx.rowStyle,
  290. store: _ctx.store,
  291. stripe: _ctx.stripe
  292. }, null, 8, ["context", "highlight", "row-class-name", "tooltip-effect", "tooltip-options", "row-style", "store", "stripe"]),
  293. _ctx.showSummary && _ctx.tableLayout === "auto" ? (openBlock(), createBlock(_component_table_footer, {
  294. key: 1,
  295. class: normalizeClass(_ctx.ns.e("body-footer")),
  296. border: _ctx.border,
  297. "default-sort": _ctx.defaultSort,
  298. store: _ctx.store,
  299. "sum-text": _ctx.computedSumText,
  300. "summary-method": _ctx.summaryMethod
  301. }, null, 8, ["class", "border", "default-sort", "store", "sum-text", "summary-method"])) : createCommentVNode("v-if", true)
  302. ], 6),
  303. _ctx.isEmpty ? (openBlock(), createElementBlock("div", {
  304. key: 0,
  305. ref: "emptyBlock",
  306. style: normalizeStyle(_ctx.emptyBlockStyle),
  307. class: normalizeClass(_ctx.ns.e("empty-block"))
  308. }, [
  309. createElementVNode("span", {
  310. class: normalizeClass(_ctx.ns.e("empty-text"))
  311. }, [
  312. renderSlot(_ctx.$slots, "empty", {}, () => [
  313. createTextVNode(toDisplayString(_ctx.computedEmptyText), 1)
  314. ])
  315. ], 2)
  316. ], 6)) : createCommentVNode("v-if", true),
  317. _ctx.$slots.append ? (openBlock(), createElementBlock("div", {
  318. key: 1,
  319. ref: "appendWrapper",
  320. class: normalizeClass(_ctx.ns.e("append-wrapper"))
  321. }, [
  322. renderSlot(_ctx.$slots, "append")
  323. ], 2)) : createCommentVNode("v-if", true)
  324. ]),
  325. _: 3
  326. }, 8, ["view-style", "wrap-style", "always", "tabindex", "onScroll"])
  327. ], 2),
  328. _ctx.showSummary && _ctx.tableLayout === "fixed" ? withDirectives((openBlock(), createElementBlock("div", {
  329. key: 1,
  330. ref: "footerWrapper",
  331. class: normalizeClass(_ctx.ns.e("footer-wrapper"))
  332. }, [
  333. createElementVNode("table", {
  334. class: normalizeClass(_ctx.ns.e("footer")),
  335. cellspacing: "0",
  336. cellpadding: "0",
  337. border: "0",
  338. style: normalizeStyle(_ctx.tableBodyStyles)
  339. }, [
  340. createVNode(_component_hColgroup, {
  341. columns: _ctx.store.states.columns.value,
  342. "table-layout": _ctx.tableLayout
  343. }, null, 8, ["columns", "table-layout"]),
  344. createVNode(_component_table_footer, {
  345. border: _ctx.border,
  346. "default-sort": _ctx.defaultSort,
  347. store: _ctx.store,
  348. "sum-text": _ctx.computedSumText,
  349. "summary-method": _ctx.summaryMethod
  350. }, null, 8, ["border", "default-sort", "store", "sum-text", "summary-method"])
  351. ], 6)
  352. ], 2)), [
  353. [vShow, !_ctx.isEmpty],
  354. [_directive_mousewheel, _ctx.handleHeaderFooterMousewheel]
  355. ]) : createCommentVNode("v-if", true),
  356. _ctx.border || _ctx.isGroup ? (openBlock(), createElementBlock("div", {
  357. key: 2,
  358. class: normalizeClass(_ctx.ns.e("border-left-patch"))
  359. }, null, 2)) : createCommentVNode("v-if", true)
  360. ], 2),
  361. withDirectives(createElementVNode("div", {
  362. ref: "resizeProxy",
  363. class: normalizeClass(_ctx.ns.e("column-resize-proxy"))
  364. }, null, 2), [
  365. [vShow, _ctx.resizeProxyVisible]
  366. ])
  367. ], 46, ["data-prefix", "onMouseleave"]);
  368. }
  369. var Table = /* @__PURE__ */ _export_sfc(_sfc_main, [["render", _sfc_render], ["__file", "table.vue"]]);
  370. export { Table as default };
  371. //# sourceMappingURL=table.mjs.map