STableToolbar2.js 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. const n = `<template>
  2. <div class="s-table-toolbar" :id="tableId" ref="rootRef">
  3. <s-table
  4. ref="tableRef"
  5. :row-key="rowKey"
  6. :columns="columns"
  7. :data="data"
  8. :page-size="pageSize"
  9. :size="size"
  10. :scroll="innerScroll"
  11. v-bind="tableAttrs"
  12. >
  13. <template #bodyCell="scope">
  14. <template v-if="scope.column">
  15. <slot
  16. v-if="hasColumnSlot(scope.column.dataIndex)"
  17. :name="scope.column.dataIndex"
  18. :text="scope.text"
  19. :record="scope.record"
  20. :index="scope.index"
  21. :column="scope.column"
  22. />
  23. <template v-else>{{ scope.text }}</template>
  24. </template>
  25. </template>
  26. <!-- 其余插槽(headerCell / expandedRowRender / emptyText 等)原样透传给 s-table -->
  27. <template v-for="(slot, name) in getRestSlots()" #[name]="slotProps">
  28. <slot :name="name" v-bind="slotProps || {}" />
  29. </template>
  30. </s-table>
  31. </div>
  32. </template>
  33. <script setup>
  34. import { computed, nextTick, onBeforeUnmount, onMounted, ref, useAttrs, useSlots } from 'vue'
  35. defineOptions({ name: 'STableToolbar' })
  36. const props = defineProps({
  37. // 列配置,原样透传给 s-table
  38. columns: {
  39. type: Array,
  40. default: () => []
  41. },
  42. // 数据加载函数,原样透传给 s-table
  43. data: {
  44. type: Function,
  45. required: true
  46. },
  47. // 行主键字段名
  48. rowKey: {
  49. type: [String, Function],
  50. default: 'id'
  51. },
  52. // 每页条数
  53. pageSize: {
  54. type: Number,
  55. default: 10
  56. },
  57. // 表格尺寸:small / middle / large
  58. size: {
  59. type: String,
  60. default: 'middle'
  61. },
  62. // 横向滚动,默认按容器宽度自适应
  63. scrollX: {
  64. type: [String, Number],
  65. default: '100%'
  66. },
  67. // 表体最小高度:空间足够时严格按可用高度算,空间不足时才退到这个下限内
  68. tableHeight: {
  69. type: Number,
  70. default: 300
  71. }
  72. })
  73. const attrs = useAttrs()
  74. const slots = useSlots()
  75. const rootRef = ref(null)
  76. const tableRef = ref(null)
  77. const scrollY = ref(props.tableHeight)
  78. const tableId = window.$utils.uuid()
  79. // scroll 由组件自己算,父级透传的 scroll 丢弃,避免覆盖高度
  80. const tableAttrs = computed(() => {
  81. const { ...rest } = attrs
  82. return rest
  83. })
  84. const innerScroll = computed(() => ({ y: scrollY.value - 8, x: props.scrollX }))
  85. // 该列是否有同名插槽(与 SListLayout 一致:列 dataIndex 即插槽名)
  86. function hasColumnSlot(dataIndex) {
  87. return !!dataIndex && typeof slots[dataIndex] === 'function'
  88. }
  89. // 需要透传给 s-table 的插槽:扣除单元格分发(bodyCell)与所有列名插槽
  90. function getRestSlots() {
  91. const out = {}
  92. Object.keys(slots).forEach((name) => {
  93. if (name === 'bodyCell' || name === 'default' || hasColumnSlot(name)) return
  94. out[name] = slots[name]
  95. })
  96. return out
  97. }
  98. /**
  99. * 表体高度改由布局决定(见样式里贯通的 flex 链),不再做「内容区 − 表头 − 分页 − 间距」的算术。
  100. * 算术法有个死穴:首帧分页区还没渲染出来,只能吃一个兜底高度,量出来比实际少 9px,
  101. * 表格会整体顶出内容区;展开 filterMore 后触发重算才「碰巧」对。
  102. * 这里只把 flex 撑出来的真实高度同步给 scroll.y,供外部读取 / 兜底。
  103. */
  104. function resize() {
  105. const tableEl = tableRef.value && tableRef.value.$el
  106. const bodyEl = tableEl && tableEl.querySelector('.ant-table-body')
  107. const h = bodyEl && bodyEl.clientHeight
  108. if (h) scrollY.value = h
  109. }
  110. function refresh(deleteFlag = true) {
  111. const p = tableRef.value && tableRef.value.refresh(deleteFlag)
  112. // 刷新后布局可能变化,下一帧把真实表体高度同步一次
  113. Promise.resolve(p).then(() => nextTick(resize))
  114. return p
  115. }
  116. function reload() {
  117. return refresh(true)
  118. }
  119. let ro = null
  120. onMounted(() => {
  121. nextTick(resize)
  122. // 观察根节点:filter 折叠 / filterMore 展开 / tab 切换 / 窗口缩放 / 侧栏收起都会改变它
  123. if (window.ResizeObserver && rootRef.value) {
  124. ro = new ResizeObserver(() => resize())
  125. ro.observe(rootRef.value)
  126. }
  127. setTimeout(() => {
  128. const th = document.querySelector('.ant-table-cell.ant-table-cell-scrollbar')
  129. const thead = document.querySelector(\`#\${tableId.value} .ant-table-thead tr\`)
  130. if (th && thead) thead.removeChild(th)
  131. }, 100)
  132. })
  133. onBeforeUnmount(() => {
  134. if (ro) {
  135. ro.disconnect()
  136. ro = null
  137. }
  138. })
  139. defineExpose({ resize, refresh, reload, tableRef, scrollY })
  140. <\/script>
  141. <style lang="less" scoped>
  142. .s-table-toolbar {
  143. // flex 场景下由父级分配剩余高度;非 flex 场景用 height:100% 兜底。
  144. // overflow:hidden 是「不产生页面级滚动条」的关键:内容永远不往外顶。
  145. flex: 1 1 0%;
  146. min-height: 0;
  147. height: 100%;
  148. box-sizing: border-box;
  149. overflow: hidden;
  150. display: flex;
  151. flex-direction: column;
  152. // 分页与容器底之间留一口气:SListLayout 的表格区底部恒有空档(layoutHeight = 父高 − 76,
  153. // 工具条 60),本组件原来分页直接贴底,两种页面观感不一致。这里补 8px 对齐。
  154. padding-bottom: 8px;
  155. }
  156. // STable 自己包了一层 .table-wrapper,连同 antd 各层容器纵向 flex 贯通,
  157. // 把根节点的剩余高度一路交给表体 —— 表头、分页占多少都不用算,也不会算错。
  158. :deep(.table-wrapper),
  159. :deep(.ant-table-wrapper),
  160. :deep(.ant-spin-nested-loading),
  161. :deep(.ant-spin-container),
  162. :deep(.ant-table),
  163. :deep(.ant-table-container) {
  164. flex: 1 1 auto;
  165. min-height: 0;
  166. display: flex;
  167. flex-direction: column;
  168. }
  169. // 表头 / 合计行 / 分页固定占位,不参与剩余高度分配
  170. :deep(.ant-table-header),
  171. :deep(.ant-table-footer),
  172. :deep(.ant-table-pagination.ant-pagination) {
  173. flex: 0 0 auto;
  174. }
  175. // 表体吃掉剩余空间:数据不足一页也撑满,超出才滚动。
  176. // antd 把 scroll.y 写成了 inline max-height,会卡住 flex 撑出来的高度,必须覆盖。
  177. :deep(.ant-table-body) {
  178. flex: 1 1 auto;
  179. min-height: 0;
  180. max-height: none !important;
  181. }
  182. // 空数据:占位行撑满表体,提示语上下居中
  183. :deep(.ant-table-body:has(.ant-table-placeholder)) > table {
  184. height: 100%;
  185. }
  186. :deep(.ant-table-tbody > tr.ant-table-placeholder) {
  187. height: 100%;
  188. }
  189. :deep(.ant-table-tbody > tr.ant-table-placeholder > td) {
  190. vertical-align: middle;
  191. }
  192. // 分页上间距。antd 默认是 margin: 16px 0,上边距换成 padding-top(带上分隔线),
  193. // 下边距必须一起清掉,否则分页下方留 16px 空白,表格区就不算撑满。
  194. :deep(.ant-table-pagination.ant-pagination) {
  195. border-top: 1px solid var(--s-border-color-split);
  196. padding: 16px 0;
  197. box-sizing: border-box;
  198. margin: 0;
  199. }
  200. </style>
  201. `;
  202. export {
  203. n as default
  204. };