This commit is contained in:
2026-08-12 17:45:49 +08:00
parent 5b441626e9
commit 2a889a7d91
25 changed files with 154 additions and 29 deletions
+23 -2
View File
@@ -8,8 +8,8 @@ import { UI_PREFERENCE_SCOPE, preferenceStorageKey } from '@/shared/utils/uiLayo
const TableStub = defineComponent({ template: '<div><slot /></div>', methods: { doLayout() {} } })
const ColumnStub = defineComponent({
inheritAttrs: false,
props: { label: String },
template: '<div class="column-stub" :data-label="label"><slot /></div>',
props: { label: String, prop: String },
template: '<div class="column-stub" :data-label="label" :data-prop="prop"><slot /></div>',
})
const ButtonStub = defineComponent({ template: '<button type="button"><slot /></button>' })
const PopoverStub = defineComponent({ template: '<div><slot name="reference" /><slot /></div>' })
@@ -69,6 +69,27 @@ describe('DataTable pagination helpers', () => {
describe('DataTable column preferences', () => {
beforeEach(() => localStorage.clear())
it('uses key as the data property for ordinary columns only', () => {
const wrapper = shallowMount(DataTable, {
props: {
data: [{ customerName: '客户A', explicitName: '显式字段' }],
columns: [
{ key: 'actions', label: '操作', slotName: 'actions' },
{ key: 'customerName', label: '客户' },
{ key: 'name', prop: 'explicitName', label: '显式字段' },
{ key: 'selection', type: 'selection', label: '选择' },
],
},
global: {
stubs: { ElTable: TableStub, ElTableColumn: ColumnStub, ElButton: ButtonStub, ElPopover: PopoverStub },
directives: { loading: {} },
},
})
const columns = wrapper.findAll('.column-stub')
expect(columns.map((column) => column.attributes('data-prop'))).toEqual([undefined, 'customerName', 'explicitName', undefined])
})
it('restores column order and visibility while locking the actions column', async () => {
localStorage.setItem(preferenceStorageKey('table', 'user-1', 'orders-table'), JSON.stringify({
version: 1,
+13 -1
View File
@@ -232,7 +232,19 @@ watch([page, pageCount], ([value, count]) => {
}, { immediate: true })
function columnAttrs({ key, slotName, headerSlotName, hideable, movable, defaultVisible, ...attrs }: DataTableColumn) {
return attrs
const hasDataProperty = attrs.prop !== undefined || attrs.property !== undefined
const isSpecialColumn = slotName || ['selection', 'expand', 'index'].includes(String(attrs.type || ''))
if (hasDataProperty || isSpecialColumn || key === undefined || key === null || key === '') return attrs
const label = String(attrs.label || '')
return {
...attrs,
prop: String(key),
formatter: attrs.formatter || ((_row: unknown, _column: unknown, value: unknown) => {
if (value === null || value === undefined || value === '') return '—'
if (typeof value === 'number' && /CNY|金额|应收|应付|已收|已付|未收|待收|待付|开账/.test(label)) return value.toFixed(2)
return String(value)
}),
}
}
function columnRenderKey(column: DataTableColumn, index: number) {