| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227 |
- const n = `<template>
- <div class="s-table-toolbar" :id="tableId" ref="rootRef">
- <s-table
- ref="tableRef"
- :row-key="rowKey"
- :columns="columns"
- :data="data"
- :page-size="pageSize"
- :size="size"
- :scroll="innerScroll"
- v-bind="tableAttrs"
- >
- <template #bodyCell="scope">
- <template v-if="scope.column">
- <slot
- v-if="hasColumnSlot(scope.column.dataIndex)"
- :name="scope.column.dataIndex"
- :text="scope.text"
- :record="scope.record"
- :index="scope.index"
- :column="scope.column"
- />
- <template v-else>{{ scope.text }}</template>
- </template>
- </template>
- <!-- 其余插槽(headerCell / expandedRowRender / emptyText 等)原样透传给 s-table -->
- <template v-for="(slot, name) in getRestSlots()" #[name]="slotProps">
- <slot :name="name" v-bind="slotProps || {}" />
- </template>
- </s-table>
- </div>
- </template>
- <script setup>
- import { computed, nextTick, onBeforeUnmount, onMounted, ref, useAttrs, useSlots } from 'vue'
- defineOptions({ name: 'STableToolbar' })
- const props = defineProps({
- // 列配置,原样透传给 s-table
- columns: {
- type: Array,
- default: () => []
- },
- // 数据加载函数,原样透传给 s-table
- data: {
- type: Function,
- required: true
- },
- // 行主键字段名
- rowKey: {
- type: [String, Function],
- default: 'id'
- },
- // 每页条数
- pageSize: {
- type: Number,
- default: 10
- },
- // 表格尺寸:small / middle / large
- size: {
- type: String,
- default: 'middle'
- },
- // 横向滚动,默认按容器宽度自适应
- scrollX: {
- type: [String, Number],
- default: '100%'
- },
- // 表体最小高度:空间足够时严格按可用高度算,空间不足时才退到这个下限内
- tableHeight: {
- type: Number,
- default: 300
- }
- })
- const attrs = useAttrs()
- const slots = useSlots()
- const rootRef = ref(null)
- const tableRef = ref(null)
- const scrollY = ref(props.tableHeight)
- const tableId = window.$utils.uuid()
- // scroll 由组件自己算,父级透传的 scroll 丢弃,避免覆盖高度
- const tableAttrs = computed(() => {
- const { ...rest } = attrs
- return rest
- })
- const innerScroll = computed(() => ({ y: scrollY.value - 8, x: props.scrollX }))
- // 该列是否有同名插槽(与 SListLayout 一致:列 dataIndex 即插槽名)
- function hasColumnSlot(dataIndex) {
- return !!dataIndex && typeof slots[dataIndex] === 'function'
- }
- // 需要透传给 s-table 的插槽:扣除单元格分发(bodyCell)与所有列名插槽
- function getRestSlots() {
- const out = {}
- Object.keys(slots).forEach((name) => {
- if (name === 'bodyCell' || name === 'default' || hasColumnSlot(name)) return
- out[name] = slots[name]
- })
- return out
- }
- /**
- * 表体高度改由布局决定(见样式里贯通的 flex 链),不再做「内容区 − 表头 − 分页 − 间距」的算术。
- * 算术法有个死穴:首帧分页区还没渲染出来,只能吃一个兜底高度,量出来比实际少 9px,
- * 表格会整体顶出内容区;展开 filterMore 后触发重算才「碰巧」对。
- * 这里只把 flex 撑出来的真实高度同步给 scroll.y,供外部读取 / 兜底。
- */
- function resize() {
- const tableEl = tableRef.value && tableRef.value.$el
- const bodyEl = tableEl && tableEl.querySelector('.ant-table-body')
- const h = bodyEl && bodyEl.clientHeight
- if (h) scrollY.value = h
- }
- function refresh(deleteFlag = true) {
- const p = tableRef.value && tableRef.value.refresh(deleteFlag)
- // 刷新后布局可能变化,下一帧把真实表体高度同步一次
- Promise.resolve(p).then(() => nextTick(resize))
- return p
- }
- function reload() {
- return refresh(true)
- }
- let ro = null
- onMounted(() => {
- nextTick(resize)
- // 观察根节点:filter 折叠 / filterMore 展开 / tab 切换 / 窗口缩放 / 侧栏收起都会改变它
- if (window.ResizeObserver && rootRef.value) {
- ro = new ResizeObserver(() => resize())
- ro.observe(rootRef.value)
- }
- setTimeout(() => {
- const th = document.querySelector('.ant-table-cell.ant-table-cell-scrollbar')
- const thead = document.querySelector(\`#\${tableId.value} .ant-table-thead tr\`)
- if (th && thead) thead.removeChild(th)
- }, 100)
- })
- onBeforeUnmount(() => {
- if (ro) {
- ro.disconnect()
- ro = null
- }
- })
- defineExpose({ resize, refresh, reload, tableRef, scrollY })
- <\/script>
- <style lang="less" scoped>
- .s-table-toolbar {
- // flex 场景下由父级分配剩余高度;非 flex 场景用 height:100% 兜底。
- // overflow:hidden 是「不产生页面级滚动条」的关键:内容永远不往外顶。
- flex: 1 1 0%;
- min-height: 0;
- height: 100%;
- box-sizing: border-box;
- overflow: hidden;
- display: flex;
- flex-direction: column;
- // 分页与容器底之间留一口气:SListLayout 的表格区底部恒有空档(layoutHeight = 父高 − 76,
- // 工具条 60),本组件原来分页直接贴底,两种页面观感不一致。这里补 8px 对齐。
- padding-bottom: 8px;
- }
- // STable 自己包了一层 .table-wrapper,连同 antd 各层容器纵向 flex 贯通,
- // 把根节点的剩余高度一路交给表体 —— 表头、分页占多少都不用算,也不会算错。
- :deep(.table-wrapper),
- :deep(.ant-table-wrapper),
- :deep(.ant-spin-nested-loading),
- :deep(.ant-spin-container),
- :deep(.ant-table),
- :deep(.ant-table-container) {
- flex: 1 1 auto;
- min-height: 0;
- display: flex;
- flex-direction: column;
- }
- // 表头 / 合计行 / 分页固定占位,不参与剩余高度分配
- :deep(.ant-table-header),
- :deep(.ant-table-footer),
- :deep(.ant-table-pagination.ant-pagination) {
- flex: 0 0 auto;
- }
- // 表体吃掉剩余空间:数据不足一页也撑满,超出才滚动。
- // antd 把 scroll.y 写成了 inline max-height,会卡住 flex 撑出来的高度,必须覆盖。
- :deep(.ant-table-body) {
- flex: 1 1 auto;
- min-height: 0;
- max-height: none !important;
- }
- // 空数据:占位行撑满表体,提示语上下居中
- :deep(.ant-table-body:has(.ant-table-placeholder)) > table {
- height: 100%;
- }
- :deep(.ant-table-tbody > tr.ant-table-placeholder) {
- height: 100%;
- }
- :deep(.ant-table-tbody > tr.ant-table-placeholder > td) {
- vertical-align: middle;
- }
- // 分页上间距。antd 默认是 margin: 16px 0,上边距换成 padding-top(带上分隔线),
- // 下边距必须一起清掉,否则分页下方留 16px 空白,表格区就不算撑满。
- :deep(.ant-table-pagination.ant-pagination) {
- border-top: 1px solid var(--s-border-color-split);
- padding: 16px 0;
- box-sizing: border-box;
- margin: 0;
- }
- </style>
- `;
- export {
- n as default
- };
|