@94ai/nf-table 8.3.23 → 9.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -41,7 +41,7 @@
41
41
  :loading="columnDisplayBtnLoading"
42
42
  :disabled="columnDisplayBtnDisabled"
43
43
  >
44
- {{ columnDisplayBtnText }}
44
+ {{ localeText.columnDisplayBtnText }}
45
45
  </nf-button>
46
46
  </slot>
47
47
  <!-- 非虚拟 -->
@@ -108,7 +108,7 @@
108
108
  popper-class="actionTooltipRef"
109
109
  placement="top"
110
110
  effect="dark"
111
- :content="columnDisplayBtnText">
111
+ :content="localeText.columnDisplayBtnText">
112
112
  <i class="iconfont icon-liebiao_xianshiliemu operation-icon-column-check"
113
113
  @click="nfColumCheckDialogVisible = true"
114
114
  />
@@ -192,7 +192,7 @@
192
192
  </template>
193
193
  <slot name="append" slot="append"></slot>
194
194
  <slot name="empty" slot="empty">
195
- <nf-empty slot="empty" :image-size="emptyImageSize" :description="emptyText"></nf-empty>
195
+ <nf-empty slot="empty" :image-size="emptyImageSize" :description="localeText.emptyText"></nf-empty>
196
196
  </slot>
197
197
  </el-table>
198
198
  <!-- 虚拟 -->
@@ -268,7 +268,7 @@
268
268
  popper-class="actionTooltipRef"
269
269
  placement="top"
270
270
  effect="dark"
271
- :content="columnDisplayBtnText">
271
+ :content="localeText.columnDisplayBtnText">
272
272
  <i class="iconfont icon-liebiao_xianshiliemu operation-icon-column-check"
273
273
  @click="nfColumCheckDialogVisible = true"
274
274
  />
@@ -352,7 +352,7 @@
352
352
  </template>
353
353
  <slot name="append" slot="append"></slot>
354
354
  <slot name="empty" slot="empty">
355
- <nf-empty slot="empty" :image-size="emptyImageSize" :description="emptyText"></nf-empty>
355
+ <nf-empty slot="empty" :image-size="emptyImageSize" :description="localeText.emptyText"></nf-empty>
356
356
  </slot>
357
357
  </el-table>
358
358
  </virtual-scroll>
@@ -685,10 +685,10 @@
685
685
  </div>
686
686
  </div>
687
687
  <column-check-dialog
688
- :title="columnCheckTitle"
689
- :confirm-text="columnCheckConfirmButtonText"
690
- :cancel-text="columnCheckCancelButtonText"
691
- :basic-field-desc="columnCheckBasicFieldDesc"
688
+ :title="localeText.columnCheckTitle"
689
+ :confirm-text="localeText.columnCheckConfirmButtonText"
690
+ :cancel-text="localeText.columnCheckCancelButtonText"
691
+ :basic-field-desc="localeText.columnCheckBasicFieldDesc"
692
692
 
693
693
  :show.sync="nfColumCheckDialogVisible"
694
694
  :table-columns="processedColumns"
@@ -718,9 +718,9 @@
718
718
  <slot slot="tableColumnEditDialogFooter" name="footer"></slot>
719
719
  </column-check-dialog>
720
720
  <nf-drawer
721
- :title="columnCheckTitle"
722
- :cancel-button-text="columnCheckCancelButtonText"
723
- :confirm-button-text="columnCheckConfirmButtonText"
721
+ :title="localeText.columnCheckTitle"
722
+ :cancel-button-text="localeText.columnCheckCancelButtonText"
723
+ :confirm-button-text="localeText.columnCheckConfirmButtonText"
724
724
  v-bind="drawerProps"
725
725
  ref="columnCheckNfDrawer"
726
726
  size="680px"
@@ -737,14 +737,14 @@
737
737
  <slot slot="tableColumnEditDialogTitle" name="title" />
738
738
  <slot slot="tableColumnEditDialogFooter" name="footer" />
739
739
  <column-check-drawer
740
- :search-placeholder="columnCheckSearchPlaceholder"
741
- :fixed-left-desc="columnCheckFixedLeftDesc"
742
- :clear-desc="columnCheckClearDesc"
743
- :no-fixed-desc="columnCheckNoFixedDesc"
744
- :basic-field-desc="columnCheckBasicFieldDesc"
745
- :select-empty-warning-tips="selectEmptyWarningTips"
746
- :get-checked-length-desc="columnCheckGetCheckedLengthDesc"
747
- :get-all-check-desc="columnCheckGetAllCheckDesc"
740
+ :search-placeholder="localeText.columnCheckSearchPlaceholder"
741
+ :fixed-left-desc="localeText.columnCheckFixedLeftDesc"
742
+ :clear-desc="localeText.columnCheckClearDesc"
743
+ :no-fixed-desc="localeText.columnCheckNoFixedDesc"
744
+ :basic-field-desc="localeText.columnCheckBasicFieldDesc"
745
+ :select-empty-warning-tips="localeText.selectEmptyWarningTips"
746
+ :get-checked-length-desc="localeText.columnCheckGetCheckedLengthDesc"
747
+ :get-all-check-desc="localeText.columnCheckGetAllCheckDesc"
748
748
  ref="columnCheckDrawer"
749
749
 
750
750
  :column-edit="columnEdit"
@@ -775,6 +775,7 @@
775
775
  nextTick,
776
776
  onMounted,
777
777
  onBeforeMount,
778
+ onBeforeUnmount,
778
779
  ref,
779
780
  watch,
780
781
  set,
@@ -792,7 +793,7 @@
792
793
  NfTableActionBtnAttributes
793
794
  } from './types'
794
795
  import { ITabItem } from '@94ai/nf-drawer/lib/types'
795
- import { CachedManager, resolveValue } from '@94ai/common-utils'
796
+ import { CachedManager, resolveValue, useLocale } from '@94ai/common-utils'
796
797
  import { useRoute } from 'vue-router/composables'
797
798
  import VirtualScroll, { VirtualColumn } from 'el-table-virtual-scroll'
798
799
 
@@ -983,18 +984,6 @@
983
984
  columnCheckBeforeClose: undefined,
984
985
  columnCheckCenter: false,
985
986
  columnCheckDestroyOnClose: false,
986
- selectEmptyWarningTips: '请选择至少一个选项',
987
- columnDisplayBtnText: '设置列表字段',
988
- columnCheckTitle: '设置显示字段',
989
- columnCheckCancelButtonText: '取消',
990
- columnCheckConfirmButtonText: '确定',
991
- columnCheckSearchPlaceholder: '搜索',
992
- columnCheckFixedLeftDesc: '固定在左侧',
993
- columnCheckClearDesc: '清除',
994
- columnCheckNoFixedDesc: '不固定',
995
- columnCheckBasicFieldDesc: '基础字段',
996
- columnCheckGetCheckedLengthDesc: (length) => (length) => '已选(' + length + ')',
997
- columnCheckGetAllCheckDesc: (length) => (length) => '全选(' + length + ')',
998
987
  drawerProps: () => ({}),
999
988
  virtualized: false,
1000
989
  virtualTableData: () => [],
@@ -1059,6 +1048,24 @@
1059
1048
  const cPageChangeSearchApi = ref(props.pageChangeSearchApi ?? props.setEasyConf)
1060
1049
  const cInitSearchApi = ref(props.initSearchApi ?? props.setEasyConf)
1061
1050
 
1051
+ // 文案:显式传入的 prop 优先,否则取内置语言包(跟随 vue-i18n 语言或 locale.use 指定的语言)
1052
+ const { t } = useLocale()
1053
+ const localeText = computed(() => ({
1054
+ emptyText: props.emptyText || t('nf.table.emptyText'),
1055
+ columnDisplayBtnText: props.columnDisplayBtnText || t('nf.table.columnDisplayBtnText'),
1056
+ columnCheckTitle: props.columnCheckTitle || t('nf.table.columnCheckTitle'),
1057
+ columnCheckCancelButtonText: props.columnCheckCancelButtonText || t('nf.table.cancel'),
1058
+ columnCheckConfirmButtonText: props.columnCheckConfirmButtonText || t('nf.table.confirm'),
1059
+ columnCheckSearchPlaceholder: props.columnCheckSearchPlaceholder || t('nf.table.searchPlaceholder'),
1060
+ columnCheckFixedLeftDesc: props.columnCheckFixedLeftDesc || t('nf.table.fixedLeft'),
1061
+ columnCheckClearDesc: props.columnCheckClearDesc || t('nf.table.clear'),
1062
+ columnCheckNoFixedDesc: props.columnCheckNoFixedDesc || t('nf.table.noFixed'),
1063
+ columnCheckBasicFieldDesc: props.columnCheckBasicFieldDesc || t('nf.table.basicField'),
1064
+ selectEmptyWarningTips: props.selectEmptyWarningTips || t('nf.table.selectEmptyWarning'),
1065
+ columnCheckGetCheckedLengthDesc: props.columnCheckGetCheckedLengthDesc || ((length) => t('nf.table.checkedLength', { length })),
1066
+ columnCheckGetAllCheckDesc: props.columnCheckGetAllCheckDesc || ((length) => t('nf.table.allCheck', { length }))
1067
+ }))
1068
+
1062
1069
  const cStorageKey = computed(() => {
1063
1070
  const key = props.storageKey || `nf-table-columns-cached-key-${routeName.value}`
1064
1071
  const userId = (window as any).userId
@@ -1073,48 +1080,48 @@
1073
1080
  })
1074
1081
 
1075
1082
  const checkViewIdentityChange = async () => {
1076
- const userId = (window as any).userId
1077
- const viewField = (window as any).viewField
1078
- if (props.viewIdentity && userId && viewField) {
1079
- let viewIdentity = CachedManager.syncCachedGet(cStorageKeyViewIdentity.value)
1080
- if (!viewIdentity) {
1081
- viewIdentity = await CachedManager.asyncCachedGet(cStorageKeyViewIdentity.value)
1082
- }
1083
- if (!viewIdentity) {
1084
- await CachedManager.asyncCachedSet(cStorageKeyViewIdentity.value, viewField)
1085
- } else {
1086
- const newViewIdentity = viewField.find(item => item.viewIdentity === props.viewIdentity)
1087
- const oldViewIdentity = viewIdentity.find(item => item.viewIdentity === props.viewIdentity)
1088
- if ((newViewIdentity && !oldViewIdentity) || (!newViewIdentity && oldViewIdentity)) {
1089
- await clearColumnEditCached()
1090
- await clearDragCached()
1091
- } else if (newViewIdentity && oldViewIdentity) {
1092
- let change = false
1093
- newViewIdentity.fieldList.every(item => {
1094
- if (!oldViewIdentity.fieldList.includes(item)) {
1095
- change = true
1096
- return
1097
- }
1098
- return true
1099
- })
1100
- if (!change) {
1101
- oldViewIdentity.fieldList.every(item => {
1102
- if (!newViewIdentity.fieldList.includes(item)) {
1103
- change = true
1104
- return
1105
- }
1106
- return true
1107
- })
1108
- }
1109
- if (change) {
1110
- await clearColumnEditCached()
1111
- await clearDragCached()
1112
- oldViewIdentity.fieldList = newViewIdentity.fieldList
1113
- await CachedManager.asyncCachedSet(cStorageKeyViewIdentity.value, viewIdentity)
1114
- }
1115
- }
1116
- }
1117
- }
1083
+ // const userId = (window as any).userId
1084
+ // const viewField = (window as any).viewField
1085
+ // if (props.viewIdentity && userId && viewField) {
1086
+ // let viewIdentity = CachedManager.syncCachedGet(cStorageKeyViewIdentity.value)
1087
+ // if (!viewIdentity) {
1088
+ // viewIdentity = await CachedManager.asyncCachedGet(cStorageKeyViewIdentity.value)
1089
+ // }
1090
+ // if (!viewIdentity) {
1091
+ // await CachedManager.asyncCachedSet(cStorageKeyViewIdentity.value, viewField)
1092
+ // } else {
1093
+ // const newViewIdentity = viewField.find(item => item.viewIdentity === props.viewIdentity)
1094
+ // const oldViewIdentity = viewIdentity.find(item => item.viewIdentity === props.viewIdentity)
1095
+ // if ((newViewIdentity && !oldViewIdentity) || (!newViewIdentity && oldViewIdentity)) {
1096
+ // await clearColumnEditCached()
1097
+ // await clearDragCached()
1098
+ // } else if (newViewIdentity && oldViewIdentity) {
1099
+ // let change = false
1100
+ // newViewIdentity.fieldList.every(item => {
1101
+ // if (!oldViewIdentity.fieldList.includes(item)) {
1102
+ // change = true
1103
+ // return
1104
+ // }
1105
+ // return true
1106
+ // })
1107
+ // if (!change) {
1108
+ // oldViewIdentity.fieldList.every(item => {
1109
+ // if (!newViewIdentity.fieldList.includes(item)) {
1110
+ // change = true
1111
+ // return
1112
+ // }
1113
+ // return true
1114
+ // })
1115
+ // }
1116
+ // if (change) {
1117
+ // await clearColumnEditCached()
1118
+ // await clearDragCached()
1119
+ // oldViewIdentity.fieldList = newViewIdentity.fieldList
1120
+ // await CachedManager.asyncCachedSet(cStorageKeyViewIdentity.value, viewIdentity)
1121
+ // }
1122
+ // }
1123
+ // }
1124
+ // }
1118
1125
  }
1119
1126
 
1120
1127
  const cPageAttrs = reactive({
@@ -1315,13 +1322,57 @@
1315
1322
  return value
1316
1323
  }
1317
1324
  }
1318
- const fs = 14
1319
- const char = columnLabel.match(/[\u2E80-\u9FFF]/g)
1320
- const charLen = char ? char.length : 0
1321
- const num = columnLabel.match(/\d|\./g)
1322
- const numLen = num ? num.length : 0
1323
- const otherLen = columnLabel.length - charLen - numLen
1324
- return fs * (charLen * 1 + numLen * 0.7 + otherLen * 0.58) + 20 + (column.sortable ? 24 : 0)
1325
+ // 按实际字体测量表头、标签、操作按钮
1326
+ const label = columnLabel || ''
1327
+ const sortable = column.sortable || column.attr?.sortable
1328
+ // 表头为 `<span> {{ label }} </span>`:行首空格会折叠,尾随空格后面还有同行的图标时不折叠,要计入
1329
+ // 操作列内嵌「显示列」图标:nowrap,两侧空格都占宽,图标 14px + 内边距 4px×2 + 左间距 10px
1330
+ // 排序图标 24px
1331
+ const hasColumnCheckIcon = props.columnEdit && column.prop === props.operationPropKey && props.columnPosition === 'embed'
1332
+ const labelWidth = (hasColumnCheckIcon
1333
+ ? measureText(` ${label} `, '600 14px') + 33
1334
+ : measureText(sortable ? `${label} ` : label, '600 14px')) + CELL_PADDING + (sortable ? 24 : 0)
1335
+ // nf-tag:12px,左右内边距各 8px、边框各 1px、圆点 4px + 间距 4px;模板换行使文字前多一个空格
1336
+ const tags = [...getTagTexts(column), ...(column.fit?.tags || [])]
1337
+ const tagWidth = Math.max(0, ...tags.map(tag => measureText(` ${tag}`, '12px') + 26 + CELL_PADDING))
1338
+ // 操作按钮不按配置估算(按行隐藏的按钮无法预知,会估宽),由 fitOperationColumn 按渲染结果测得 __contentWidth;
1339
+ // 拖拽下限同样要包含它
1340
+ return Math.ceil(Math.max(labelWidth, tagWidth, column.__contentWidth || 0))
1341
+ }
1342
+
1343
+ const CELL_PADDING = 20
1344
+ let measureContext: CanvasRenderingContext2D | null = null
1345
+ let fontFamily = ''
1346
+ const measureText = (text, font: string) => {
1347
+ measureContext = measureContext || document.createElement('canvas').getContext('2d')
1348
+ fontFamily = fontFamily || getComputedStyle(document.body).fontFamily
1349
+ measureContext!.font = `${font} ${fontFamily}`
1350
+ return measureContext!.measureText(String(text)).width
1351
+ }
1352
+ const getTagTexts = (column) => {
1353
+ const tagMap = column.optionsTag || {}
1354
+ return Object.keys(tagMap).filter(key => tagMap[key]).map(key => column.optionsEnum?.[key])
1355
+ }
1356
+ const applyColumnWidth = (item, dragProp) => {
1357
+ if (!item.label || item.type) return
1358
+ const attr = item.attr
1359
+ const raw = item.__rawWidth || (item.__rawWidth = {})
1360
+ // 与上次写入值不同,说明是页面设定或运行时修改(如拖拽回写、按条件切换宽度),作为新的原值
1361
+ const applied = item.__appliedWidth || {}
1362
+ if (attr.width !== applied.width) raw.width = attr.width
1363
+ if (attr['min-width'] !== applied.minWidth) raw.minWidth = attr['min-width']
1364
+ const need = getMinWidth(item)
1365
+ set(attr, 'min-width', Math.max(parseFloat(raw.minWidth) || 0, need) + 'px')
1366
+ if (raw.width) {
1367
+ set(attr, 'width', Math.max(parseFloat(raw.width) || 0, need) + 'px')
1368
+ }
1369
+ if (props.dragCached) {
1370
+ const columnWidth = CachedManager.syncCachedGet(`${cStorageKey.value}$drag-column-width`) || {}
1371
+ if (columnWidth[dragProp]) { // 读取缓存
1372
+ set(attr, 'width', columnWidth[dragProp])
1373
+ }
1374
+ }
1375
+ item.__appliedWidth = { width: attr.width, minWidth: attr['min-width'] }
1325
1376
  }
1326
1377
 
1327
1378
  const tableColumns = ref<NfTableColumnAttributes[]>([])
@@ -1330,17 +1381,7 @@
1330
1381
  props.columns._skip = true
1331
1382
  const columns = processedColumns.value!.map((item, index) => {
1332
1383
  const attr = (item.attr || {}) as ElTableColumnAttributes
1333
- // 区分中文、数字、其他字符
1334
- if (!attr['min-width'] && item.label) {
1335
- attr['min-width'] = String(getMinWidth(item)) + 'px'
1336
- }
1337
- if (props.dragCached) {
1338
- const prop = item.prop || item.attr?.prop
1339
- let columnWidth = CachedManager.syncCachedGet(`${cStorageKey.value}$drag-column-width`) || {}
1340
- if (columnWidth[prop]) { // 读取缓存
1341
- attr.width = columnWidth[prop]
1342
- }
1343
- }
1384
+ const dragProp = item.prop || item.attr?.prop
1344
1385
  item.attr = {
1345
1386
  ...item.attr,
1346
1387
  ...attr
@@ -1360,6 +1401,12 @@
1360
1401
  item.optionsEnum[o.value] = o.label
1361
1402
  item.optionsTag[o.value] = o.tagType
1362
1403
  })
1404
+ // 异步回调时 _skip 已复位,补算宽度若改变了值会触发 columns 深度监听,导致整表重新同步、重建
1405
+ props.columns._skip = true
1406
+ applyColumnWidth(item, dragProp)
1407
+ nextTick(() => {
1408
+ props.columns._skip = false
1409
+ })
1363
1410
  })
1364
1411
  } else if (item.options) {
1365
1412
  set(item, 'optionsEnum', {})
@@ -1368,6 +1415,7 @@
1368
1415
  item.optionsTag[o.value] = o.tagType
1369
1416
  })
1370
1417
  }
1418
+ applyColumnWidth(item, dragProp)
1371
1419
  return item
1372
1420
  })
1373
1421
  if (readCached && props.columnEdit) {
@@ -1383,6 +1431,7 @@
1383
1431
  if (typeof columnState === 'string') {
1384
1432
  columnState = JSON.parse(columnState)
1385
1433
  }
1434
+ let maxIndex = 0
1386
1435
  for (const key in columnState as NfTableColumnAttributes) {
1387
1436
  const state = columnState![key]
1388
1437
  columns.every(i => {
@@ -1390,11 +1439,26 @@
1390
1439
  i.initHidden = state.initHidden
1391
1440
  i.attr ? i.attr['fixed'] = state.fixed : i.attr = { fixed: state.fixed }
1392
1441
  i._index = state.index
1442
+ if (maxIndex < state.index) {
1443
+ maxIndex = state.index
1444
+ }
1393
1445
  return false
1394
1446
  }
1395
1447
  return true
1396
1448
  })
1397
1449
  }
1450
+ const columnStateKeys = Object.keys(columnState)
1451
+ columns.forEach(item => {
1452
+ if (!columnStateKeys.includes(item.prop)) {
1453
+ maxIndex++
1454
+ item._index = maxIndex
1455
+ }
1456
+ })
1457
+ const optItem = columns.find(itemOpt => itemOpt.prop === props.operationPropKey)
1458
+ if (optItem) {
1459
+ maxIndex++
1460
+ optItem._index = maxIndex++
1461
+ }
1398
1462
  }
1399
1463
  } catch (e) {
1400
1464
  console.log(e)
@@ -1570,9 +1634,121 @@
1570
1634
  }
1571
1635
  }
1572
1636
 
1637
+ // 操作列按渲染结果撑宽:slot、权限按钮、按行变化的按钮都只能从 DOM 得知
1638
+ const fitOperationColumn = () => {
1639
+ const table = elTableRef.value
1640
+ const config = tableColumns.value.find(item => item.prop === props.operationPropKey)
1641
+ if (!table || !config || !config.attr) return
1642
+ const column = (table.store.states.columns || []).find(item => item.property === props.operationPropKey)
1643
+ if (!column) return
1644
+ // 固定列会渲染两份,优先测用户看到的固定列那份(nf-action 的按钮折叠只在可见的那份生效)
1645
+ const fixedWrapper = column.fixed === 'left' || column.fixed === true ? '.el-table__fixed' : column.fixed === 'right' ? '.el-table__fixed-right' : ''
1646
+ let cells: HTMLElement[] = fixedWrapper ? [...table.$el.querySelectorAll(`${fixedWrapper} .el-table__fixed-body-wrapper td.${column.id} .cell`)] : []
1647
+ if (!cells.length) cells = [...table.$el.querySelectorAll(`.el-table__body-wrapper td.${column.id} .cell`)]
1648
+ if (!cells.length) return
1649
+ // 批量写样式、批量读、批量还原:只触发一次布局,且在同一帧内完成,不会闪动
1650
+ const origins = cells.map(cell => [cell.style.whiteSpace, cell.style.width])
1651
+ cells.forEach(cell => {
1652
+ cell.style.whiteSpace = 'nowrap'
1653
+ cell.style.width = 'max-content'
1654
+ })
1655
+ // 容器可能是 width: 100%(如 nf-action),宽度随列变化,不能直接读单元格宽度;
1656
+ // 改为取按内容定宽的元素最右边缘,加单元格右内边距:按钮、链接、图标直接取其盒子,
1657
+ // 其余末端元素中,行内元素取盒子,块级元素取文字范围
1658
+ const contentTags = ['BUTTON', 'A', 'I', 'SVG', 'IMG']
1659
+ const range = document.createRange()
1660
+ const need = Math.max(...cells.map(cell => {
1661
+ const rect = cell.getBoundingClientRect()
1662
+ let right = rect.left
1663
+ cell.querySelectorAll('*').forEach(el => {
1664
+ const tag = el.tagName.toUpperCase()
1665
+ if (!contentTags.includes(tag) && el.children.length) return
1666
+ let box = el.getBoundingClientRect()
1667
+ if (!box.width) return
1668
+ if (!contentTags.includes(tag) && !getComputedStyle(el).display.startsWith('inline')) {
1669
+ range.selectNodeContents(el)
1670
+ box = range.getBoundingClientRect()
1671
+ }
1672
+ right = Math.max(right, box.right)
1673
+ })
1674
+ return Math.ceil(right - rect.left + parseFloat(getComputedStyle(cell).paddingRight)) + 1
1675
+ }))
1676
+ cells.forEach((cell, i) => {
1677
+ cell.style.whiteSpace = origins[i][0]
1678
+ cell.style.width = origins[i][1]
1679
+ })
1680
+ const current = parseFloat(config.attr.width as string) || column.realWidth || column.width || 0
1681
+ if (need > current) {
1682
+ // 记在列上,列重新同步时仍生效;_skip 避免深度监听 columns 触发重新同步
1683
+ config.__contentWidth = need
1684
+ props.columns._skip = true
1685
+ set(config.attr, 'width', need + 'px')
1686
+ config.__appliedWidth = { ...config.__appliedWidth, width: config.attr.width }
1687
+ nextTick(() => {
1688
+ props.columns._skip = false
1689
+ doLayout()
1690
+ })
1691
+ }
1692
+ }
1693
+
1694
+ // 行高兜底:固定列是整表副本,副本中单元格渲染不一致(如组件按可见性决定内容)时,
1695
+ // 主体与固定列行高会错位。监听可见单元格内容尺寸,变化后把同一行各副本统一为最大高度
1696
+ let rowHeightObserver: ResizeObserver | null = null
1697
+ let rowHeightFrame = 0
1698
+ const syncRowHeights = () => {
1699
+ rowHeightFrame = 0
1700
+ const tableEl = elTableRef.value?.$el
1701
+ if (!tableEl) return
1702
+ const bodies = [tableEl.querySelector('.el-table__body-wrapper tbody'), ...tableEl.querySelectorAll('.el-table__fixed-body-wrapper tbody')].filter(Boolean)
1703
+ if (bodies.length < 2) return
1704
+ const groups: HTMLElement[][] = bodies.map(body => [...body.children].filter(tr => tr.tagName === 'TR'))
1705
+ // 行数不一致(如展开行只在主体渲染)时无法按下标对应,放弃同步
1706
+ if (groups.some(rows => rows.length !== groups[0].length)) return
1707
+ // 先恢复为自然高度(只还原自己设过的),批量读取后再批量写入:只触发一次布局
1708
+ groups.forEach(rows => rows.forEach(tr => {
1709
+ if (tr.dataset.nfRowHeight) {
1710
+ tr.style.height = tr.dataset.nfOriginHeight || ''
1711
+ delete tr.dataset.nfRowHeight
1712
+ }
1713
+ }))
1714
+ const heights = groups.map(rows => rows.map(tr => tr.getBoundingClientRect().height))
1715
+ groups[0].forEach((_, i) => {
1716
+ const rowHeights = heights.map(list => list[i])
1717
+ const max = Math.max(...rowHeights)
1718
+ if (max - Math.min(...rowHeights) < 0.5) return
1719
+ groups.forEach(rows => {
1720
+ const tr = rows[i]
1721
+ tr.dataset.nfOriginHeight = tr.style.height
1722
+ tr.dataset.nfRowHeight = '1'
1723
+ tr.style.height = max + 'px'
1724
+ })
1725
+ })
1726
+ }
1727
+ const observeRowHeights = () => {
1728
+ rowHeightObserver?.disconnect()
1729
+ rowHeightObserver = null
1730
+ const tableEl = elTableRef.value?.$el
1731
+ if (!tableEl || !window.ResizeObserver || !tableEl.querySelector('.el-table__fixed-body-wrapper')) return
1732
+ rowHeightObserver = new ResizeObserver(() => {
1733
+ rowHeightFrame = rowHeightFrame || requestAnimationFrame(syncRowHeights)
1734
+ })
1735
+ // 监听可见单元格的内容盒:tr 被设了高度后,内容变矮也能感知到
1736
+ tableEl.querySelectorAll('.el-table__body-wrapper td:not(.is-hidden) > .cell, .el-table__fixed-body-wrapper td:not(.is-hidden) > .cell').forEach(cell => rowHeightObserver!.observe(cell))
1737
+ }
1738
+ onBeforeUnmount(() => {
1739
+ rowHeightObserver?.disconnect()
1740
+ rowHeightFrame && cancelAnimationFrame(rowHeightFrame)
1741
+ })
1742
+ watch([() => props.data, elTableRefKey], () => {
1743
+ nextTick(() => requestAnimationFrame(() => {
1744
+ fitOperationColumn()
1745
+ observeRowHeights()
1746
+ }))
1747
+ })
1748
+
1573
1749
  const headerDragend = async (newWidth, oldWidth, column, event) => {
1574
1750
  await nextTick()
1575
- const minWidth = getMinWidth(column)
1751
+ const minWidth = getMinWidth(tableColumns.value.find(item => item.prop === column.property) || column)
1576
1752
  if (newWidth < minWidth) {
1577
1753
  column.width = minWidth
1578
1754
  }
@@ -1664,10 +1840,10 @@
1664
1840
  const createIndicatorLine = (rect?: DOMRect) => {
1665
1841
  const line = document.createElement('div')
1666
1842
  line.id = 'nf-fixed-right-indicator-line'
1667
-
1843
+
1668
1844
  // 获取表格位置信息(入参优先,避免多次触发回流)
1669
1845
  const tableRect = rect || tableEl.getBoundingClientRect()
1670
-
1846
+
1671
1847
  line.style.cssText = `
1672
1848
  position: fixed;
1673
1849
  top: ${tableRect.top}px;
@@ -1718,15 +1894,15 @@
1718
1894
  document.removeEventListener('pointerup', onPointerUp)
1719
1895
  const dragMask = document.getElementById('nf-fixed-right-drag-mask')
1720
1896
  dragMask && dragMask.remove()
1721
-
1897
+
1722
1898
  // 移除指示线
1723
1899
  if (indicatorLine) {
1724
1900
  indicatorLine.remove()
1725
1901
  indicatorLine = null
1726
1902
  }
1727
-
1903
+
1728
1904
  try { handle.releasePointerCapture && handle.releasePointerCapture(e.pointerId) } catch {}
1729
-
1905
+
1730
1906
  // 计算最终宽度并更新
1731
1907
  try {
1732
1908
  // 复用缓存的 rect,避免额外回流
@@ -1739,14 +1915,14 @@
1739
1915
  const rawWidth = tableRect.right - clampX
1740
1916
  const minWidth = getMinWidth(opCol)
1741
1917
  const finalWidthNum = Math.max(rawWidth, minWidth)
1742
-
1918
+
1743
1919
  // 缓存宽度
1744
1920
  if (props.dragCached && columnProp) {
1745
1921
  const columnCachedData = await CachedManager.asyncCachedGet(`${cStorageKey.value}$drag-column-width`) || {}
1746
1922
  columnCachedData[columnProp] = finalWidthNum
1747
1923
  await CachedManager.asyncCachedSet(`${cStorageKey.value}$drag-column-width`, columnCachedData)
1748
1924
  }
1749
-
1925
+
1750
1926
  // 通知外部(自定义固定列拖拽事件)
1751
1927
  const oldWidth = th.getBoundingClientRect().width
1752
1928
  emit('fixedColumnDragEnd', { width: finalWidthNum, oldWidth, prop: columnProp, column: opCol, nativeEvent: e })
@@ -1757,10 +1933,10 @@
1757
1933
 
1758
1934
  const onPointerDown = (e: PointerEvent) => {
1759
1935
  e.preventDefault()
1760
-
1936
+
1761
1937
  // 创建指示线
1762
1938
  indicatorLine = createIndicatorLine()
1763
-
1939
+
1764
1940
  // 添加全屏遮罩捕获指针,避免失焦
1765
1941
  const mask = document.createElement('div')
1766
1942
  mask.id = 'nf-fixed-right-drag-mask'
package/package/types.ts CHANGED
@@ -289,6 +289,11 @@ export interface NfTableColumnAttributes {
289
289
  operationSpan?: number
290
290
  /** 操作栏按钮数组 */
291
291
  operationList?: object[]
292
+ /**
293
+ * 列宽自适应补充:slot 渲染的 nf-tag 文案无法从配置得知时,声明其文案参与最小宽度计算
294
+ * (操作列按渲染结果自动撑宽,无需声明)
295
+ */
296
+ fit?: { tags?: string[] }
292
297
  attr?: ElTableColumnAttributes
293
298
  }
294
299
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@94ai/nf-table",
3
- "version": "8.3.23",
3
+ "version": "9.0.0",
4
4
  "description": "> TODO: description",
5
5
  "keywords": [],
6
6
  "author": "zoujiahe <zoujiahe@94ai.com>",
@@ -11,16 +11,16 @@
11
11
  },
12
12
  "repository": {
13
13
  "type": "git",
14
- "url": "http://94ai.gitlab.com/zoujiahe/common-ui.git"
14
+ "url": "http://gitlab.94ai.pro/zoujiahe/common-ui.git"
15
15
  },
16
16
  "dependencies": {
17
- "@94ai/common-utils": "^8.3.23",
18
- "@94ai/nf-button": "^8.3.23",
19
- "@94ai/nf-drawer": "^8.3.23",
20
- "@94ai/nf-message": "^8.3.23",
21
- "@94ai/nf-search-form": "^8.3.23",
22
- "@94ai/nf-tag": "^8.3.23",
23
- "@94ai/nf-theme-chalk": "^8.3.23",
17
+ "@94ai/common-utils": "^9.0.0",
18
+ "@94ai/nf-button": "^9.0.0",
19
+ "@94ai/nf-drawer": "^9.0.0",
20
+ "@94ai/nf-message": "^9.0.0",
21
+ "@94ai/nf-search-form": "^9.0.0",
22
+ "@94ai/nf-tag": "^9.0.0",
23
+ "@94ai/nf-theme-chalk": "^9.0.0",
24
24
  "idb-keyval": "^6.2.1",
25
25
  "vue-demi": "^0.14.5",
26
26
  "vue-router": "3.6.4",
@@ -39,5 +39,5 @@
39
39
  "types": "lib/index.d.ts",
40
40
  "main": "lib/nf-table.cjs.js",
41
41
  "module": "lib/nf-table.esm-bundler.js",
42
- "gitHead": "74e871c7972dc4576480bee02b1dc61a3eafc6ba"
42
+ "gitHead": "afe3016f0e42dfb7fedf6cbbe400ec08bf4b8662"
43
43
  }