SListBasicLayout2.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  1. const n = `<template>
  2. <div class="s-list-basic-layout" ref="layoutRef">
  3. <a-spin :spinning="spinning">
  4. <div class="s-list-basic-layout-main">
  5. <div class="s-list-basic-content" v-if="!$slots.empty">
  6. <div class="filter-wrap" ref="filtersRef">
  7. <div class="filter-content" v-if="$slots.filter">
  8. <div class="filter">
  9. <slot name="filter" />
  10. </div>
  11. <a class="filter-more-btn" v-if="$slots.filterMore" @click="toggleFilter">
  12. {{ !isMoreFilter ? $l('更多筛选') : $l('收起筛选') }}
  13. <double-right-outlined
  14. style="font-size: 12px; transform: rotate(90deg)"
  15. v-if="!isMoreFilter"
  16. />
  17. <double-right-outlined style="font-size: 12px; transform: rotate(-90deg)" v-else />
  18. </a>
  19. <div class="reset-wrap" v-if="$slots.filter">
  20. <a-tooltip
  21. placement="top"
  22. :overlayStyle="{ maxWidth: '500px' }"
  23. :mouseEnterDelay="1.5"
  24. :mouseLeaveDelay="0.5"
  25. >
  26. <template #title>
  27. <span>{{ $l('条件重置') }}</span>
  28. </template>
  29. <a-button style="padding-bottom: 2px" @click="filterReset">
  30. <template #icon>
  31. <sync-outlined />
  32. </template>
  33. </a-button>
  34. </a-tooltip>
  35. </div>
  36. </div>
  37. <div class="filter-more-content" v-if="$slots.filterMore" v-show="isMoreFilter">
  38. <slot name="filterMore" />
  39. </div>
  40. <div class="desc-wrap" v-if="$slots.desc">
  41. <slot name="desc" />
  42. </div>
  43. </div>
  44. <div class="table-content">
  45. <div class="tabs-wrap" v-if="$slots.tabs" ref="tabsRef">
  46. <slot name="tabs" />
  47. </div>
  48. <div class="table-container">
  49. <s-table
  50. ref="table"
  51. size="middle"
  52. :scroll="{ y: tableScrollPx, x: '100%' }"
  53. :pageSize="pageSize"
  54. :rowKey="rowKey"
  55. :columns="renderColumns"
  56. :data="loadData"
  57. v-model:expanded-row-keys="expandedRowKeys"
  58. :rowSelection="
  59. showSelection || isSelection
  60. ? { selectedRowKeys: selectedRowKeys, onChange: onSelectChange }
  61. : null
  62. "
  63. :showPagination="showPagination"
  64. >
  65. <!-- 自定义单元格:template 渲染同名插槽、isClick 渲染可点击、action 渲染操作列 -->
  66. <template #bodyCell="scope">
  67. <template v-if="scope.column">
  68. <slot
  69. v-if="isTemplateColumn(scope.column.dataIndex)"
  70. :name="scope.column.dataIndex"
  71. :text="scope.text"
  72. :record="scope.record"
  73. :index="scope.index"
  74. :item="getColumn(scope.column.dataIndex)"
  75. />
  76. <a
  77. v-else-if="isClickColumn(scope.column.dataIndex)"
  78. @click="tableColCallback(scope.column.dataIndex, scope.record)"
  79. >{{ getCellText(scope.text) }}</a
  80. >
  81. <slot
  82. v-else-if="scope.column.dataIndex === 'action'"
  83. name="action"
  84. :record="scope.record"
  85. />
  86. </template>
  87. </template>
  88. </s-table>
  89. </div>
  90. </div>
  91. </div>
  92. <div class="s-list-basic-content" v-else>
  93. <slot name="empty" />
  94. </div>
  95. </div>
  96. <div class="other">
  97. <slot name="other" />
  98. </div>
  99. </a-spin>
  100. </div>
  101. </template>
  102. <script setup>
  103. import { nextTick, onActivated, onMounted, ref, watch } from 'vue'
  104. defineOptions({ name: 'SListBasicLayout' })
  105. const props = defineProps({
  106. // 列表 checkbox 列
  107. isSelection: {
  108. type: Boolean,
  109. default: false
  110. },
  111. // 是否显示分页
  112. showPagination: {
  113. type: Boolean,
  114. default: true
  115. },
  116. // 表格列配置
  117. columns: {
  118. type: Array,
  119. default: () => []
  120. },
  121. // 行主键字段名
  122. rowKey: {
  123. type: [Function, String],
  124. default: 'id'
  125. },
  126. // 取数函数,返回 { list, total }
  127. loadData: {
  128. type: Function,
  129. required: true
  130. },
  131. // 每页条数
  132. pSize: {
  133. type: [String, Number],
  134. default: 10
  135. },
  136. // 初始勾选的表格行 key
  137. selectedRowKeysArr: {
  138. type: Array,
  139. default: () => []
  140. }
  141. })
  142. const emit = defineEmits(['reset', 'tableColCallback'])
  143. // ---------- 模板 ref ----------
  144. const layoutRef = ref(null)
  145. const filtersRef = ref(null)
  146. const tabsRef = ref(null)
  147. const table = ref(null)
  148. // ---------- 状态 ----------
  149. const isMoreFilter = ref(false)
  150. // 表体滚动高度:仅作为 scroll.y 同步值(初值兜底),布局高度完全由 CSS flex 决定
  151. const tableScrollPx = ref(300)
  152. const spinning = ref(false)
  153. const pageSize = ref(10)
  154. const showSelection = ref(false)
  155. const selectedRowKeys = ref([])
  156. const expandedRowKeys = ref([])
  157. const renderColumns = ref([])
  158. // ---------- watch ----------
  159. watch(
  160. () => props.columns,
  161. () => initComponent()
  162. )
  163. watch(
  164. () => props.selectedRowKeysArr,
  165. (v) => {
  166. selectedRowKeys.value = v || []
  167. },
  168. { immediate: true }
  169. )
  170. // ---------- 方法 ----------
  171. function initTable() {
  172. pageSize.value = props.pSize
  173. renderColumns.value = props.columns.map((i) => {
  174. if (
  175. (i.template || i.isClick || (i.dataIndex === 'action' && !i.customRender)) &&
  176. !i.scopedSlots
  177. ) {
  178. i.scopedSlots = { customRender: i.dataIndex }
  179. }
  180. return i
  181. })
  182. nextTick(syncTableHeight)
  183. }
  184. function initComponent() {
  185. initTable()
  186. }
  187. function toggleFilter() {
  188. isMoreFilter.value = !isMoreFilter.value
  189. nextTick(syncTableHeight)
  190. }
  191. // 查询条件重置
  192. function filterReset() {
  193. emit('reset')
  194. }
  195. function reload() {
  196. table.value && table.value.refresh(true)
  197. }
  198. // 表格可点击列事件回调
  199. function tableColCallback(field, item) {
  200. emit('tableColCallback', field, item)
  201. }
  202. function onSelectChange(keys) {
  203. selectedRowKeys.value = keys
  204. }
  205. // 按 dataIndex 取列配置
  206. function getColumn(dataIndex) {
  207. return renderColumns.value.find((i) => i.dataIndex === dataIndex)
  208. }
  209. // 该列是否使用自定义插槽(列 dataIndex 即插槽名)
  210. function isTemplateColumn(dataIndex) {
  211. const col = getColumn(dataIndex)
  212. return !!(col && col.template)
  213. }
  214. // 该列是否为可点击列
  215. function isClickColumn(dataIndex) {
  216. const col = getColumn(dataIndex)
  217. return !!(col && col.isClick)
  218. }
  219. // 可点击列显示文本:对象取 name,其它原样
  220. function getCellText(text) {
  221. if (text === null || text === undefined) return ''
  222. if (typeof text === 'object') return text.name === undefined ? '' : text.name
  223. return text
  224. }
  225. // 布局高度全部交给 CSS flex,这里只把撑出来的真实表体高度同步给 scroll.y(antd 靠它启用固定表头)
  226. function syncTableHeight() {
  227. const el = table.value && table.value.$el
  228. const bodyEl = el && el.querySelector('.ant-table-body')
  229. const h = bodyEl && bodyEl.clientHeight
  230. if (h) tableScrollPx.value = Math.max(h - 8, 0)
  231. }
  232. // ---------- 生命周期 ----------
  233. onMounted(() => {
  234. initComponent()
  235. })
  236. // keep-alive 切回来时 DOM 重新可见,下一帧补同步一次
  237. onActivated(() => {
  238. nextTick(syncTableHeight)
  239. })
  240. defineExpose({
  241. reload,
  242. table,
  243. spinning,
  244. selectedRowKeys,
  245. filterReset,
  246. toggleFilter
  247. })
  248. <\/script>
  249. <style lang="less" scoped>
  250. // 高度实现:纯 CSS flex,零 JS 测量。
  251. // 根撑满父容器 → 主区吃剩余 → 内容区竖排 → 表格链条逐层贯通到表体。
  252. // 前提:父容器有确定高度(基座 BasicLayout 的 router-view 已带 height)。
  253. .s-list-basic-layout {
  254. height: 100%;
  255. min-height: 0;
  256. display: flex;
  257. flex-direction: column;
  258. overflow: hidden;
  259. }
  260. // 根节点外还包了一层 a-spin,需要把剩余高度透传下去
  261. .s-list-basic-layout > :deep(.ant-spin-nested-loading),
  262. .s-list-basic-layout > :deep(.ant-spin-container) {
  263. flex: 1 1 auto;
  264. min-height: 0;
  265. display: flex;
  266. flex-direction: column;
  267. }
  268. .s-list-basic-layout-main {
  269. flex: 1 1 auto;
  270. min-height: 0;
  271. display: flex;
  272. justify-content: flex-start;
  273. // stretch 让内容栏撑满主区高度;flex-start 会让它退回内容高度
  274. align-items: stretch;
  275. border-radius: 6px;
  276. background-color: var(--s-bg-color-container);
  277. overflow: hidden;
  278. }
  279. .s-list-basic-content {
  280. flex: 1 1 auto;
  281. min-width: 0;
  282. min-height: 0;
  283. padding: 24px;
  284. overflow: hidden;
  285. box-sizing: border-box;
  286. display: flex;
  287. flex-direction: column;
  288. }
  289. // 表格区吃掉内容区剩余高度;与 SListLayout 同一套撑满链路
  290. .table-content {
  291. flex: 1 1 auto;
  292. min-width: 0;
  293. min-height: 0;
  294. width: 100%;
  295. display: flex;
  296. flex-direction: column;
  297. }
  298. .table-container {
  299. flex: 1 1 auto;
  300. min-width: 0;
  301. min-height: 0;
  302. display: flex;
  303. flex-direction: column;
  304. overflow: hidden;
  305. // STable 自带的 .table-wrapper 不能漏,漏了整条链断掉、表体撑不开
  306. :deep(.table-wrapper),
  307. :deep(.ant-table-wrapper),
  308. :deep(.ant-spin-nested-loading),
  309. :deep(.ant-spin-container),
  310. :deep(.ant-table),
  311. :deep(.ant-table-container) {
  312. flex: 1 1 auto;
  313. min-height: 0;
  314. display: flex;
  315. flex-direction: column;
  316. }
  317. :deep(.ant-table-header) {
  318. flex: 0 0 auto;
  319. }
  320. :deep(.ant-table-body) {
  321. flex: 1 1 auto;
  322. min-height: 0;
  323. // antd 把 scroll.y 写成 inline max-height,会卡住 flex 撑出来的高度
  324. max-height: none !important;
  325. }
  326. :deep(.ant-table-pagination.ant-pagination) {
  327. flex: 0 0 auto;
  328. margin: 0;
  329. padding: 16px 0;
  330. border-top: 1px solid var(--s-border-color-split);
  331. box-sizing: border-box;
  332. }
  333. }
  334. .filter-wrap {
  335. flex: 0 0 auto;
  336. padding: 0 0 24px;
  337. box-sizing: border-box;
  338. .filter-content {
  339. height: 32px;
  340. display: flex;
  341. justify-content: flex-start;
  342. align-items: center;
  343. }
  344. }
  345. .filter-more-content {
  346. margin-top: 16px;
  347. }
  348. .desc-wrap {
  349. padding: 16px 0 0;
  350. box-sizing: border-box;
  351. }
  352. .reset-wrap {
  353. flex: 1;
  354. margin-left: 24px;
  355. text-align: right;
  356. }
  357. </style>
  358. `;
  359. export {
  360. n as default
  361. };