|
| 1 | +import * as VTable from '../../src'; |
| 2 | + |
| 3 | +const CONTAINER_ID = 'vTable'; |
| 4 | + |
| 5 | +const fields = [ |
| 6 | + ['orderDate', '订单日期', 91], |
| 7 | + ['customerName', '客户名称', 76], |
| 8 | + ['shipDate', '发货日期', 91], |
| 9 | + ['deliveryDate', '交付日期', 91], |
| 10 | + ['productDate', '产品日期', 91], |
| 11 | + ['category', '类别', 76], |
| 12 | + ['subCategory', '子类别', 76], |
| 13 | + ['region', '区域', 76], |
| 14 | + ['customerId', '客户编号', 110], |
| 15 | + ['birthDate', '出生日期', 100], |
| 16 | + ['renewDate', '续费日期', 100], |
| 17 | + ['rate', '折扣', 76], |
| 18 | + ['ratio', '占比', 76] |
| 19 | +] as const; |
| 20 | + |
| 21 | +const records = Array.from({ length: 80 }, (_, index) => { |
| 22 | + const day = `${(index % 28) + 1}`.padStart(2, '0'); |
| 23 | + const names = ['邢宁', '俞毅', '麦虢', '牛惠', '陶丽雪', '徐虹']; |
| 24 | + |
| 25 | + return { |
| 26 | + orderDate: `2016-01-${day}`, |
| 27 | + customerName: names[index % names.length], |
| 28 | + shipDate: `2016-02-${day}`, |
| 29 | + deliveryDate: `2017-05-${day}`, |
| 30 | + productDate: `2016-03-${day}`, |
| 31 | + category: ['家具', '技术', '办公用品'][index % 3], |
| 32 | + subCategory: ['桌子', '电话', '配件', '收纳具'][index % 4], |
| 33 | + region: index % 2 ? '分组1' : '中南', |
| 34 | + customerId: `${names[index % names.length]}-${10000 + index}`, |
| 35 | + birthDate: '1975-07-10', |
| 36 | + renewDate: '2026-02-28', |
| 37 | + rate: `${(index % 5) / 10}`, |
| 38 | + ratio: `${(index % 4) / 4}` |
| 39 | + }; |
| 40 | +}); |
| 41 | + |
| 42 | +export function createTable() { |
| 43 | + const container = document.getElementById(CONTAINER_ID)!; |
| 44 | + document.getElementById('issue5187Toolbar')?.remove(); |
| 45 | + |
| 46 | + container.style.width = '640px'; |
| 47 | + container.style.height = '520px'; |
| 48 | + |
| 49 | + const toolbar = document.createElement('div'); |
| 50 | + toolbar.id = 'issue5187Toolbar'; |
| 51 | + toolbar.style.cssText = 'height: 32px; display: flex; gap: 8px; align-items: center; font-size: 12px;'; |
| 52 | + toolbar.innerHTML = ` |
| 53 | + <button id="issue5187ScrollFrozen">scroll frozen cols</button> |
| 54 | + <button id="issue5187Wide">set container 1600px</button> |
| 55 | + <button id="issue5187Narrow">set container 1055px</button> |
| 56 | + <button id="issue5187Reproduce">reproduce 1600px -> 1055px</button> |
| 57 | + <span id="issue5187State"></span> |
| 58 | + `; |
| 59 | + container.before(toolbar); |
| 60 | + |
| 61 | + const columns: VTable.ColumnsDefine = fields.map(([field, title, width]) => ({ |
| 62 | + field, |
| 63 | + title, |
| 64 | + width, |
| 65 | + showSort: false |
| 66 | + })); |
| 67 | + |
| 68 | + const tableInstance = new VTable.ListTable({ |
| 69 | + container, |
| 70 | + columns, |
| 71 | + records, |
| 72 | + widthMode: 'standard', |
| 73 | + columnResizeMode: 'all', |
| 74 | + defaultRowHeight: 35.2, |
| 75 | + heightMode: 'autoHeight', |
| 76 | + defaultHeaderColWidth: 'auto', |
| 77 | + frozenColCount: 4, |
| 78 | + maxFrozenWidth: 100, |
| 79 | + unfreezeAllOnExceedsMaxWidth: false, |
| 80 | + scrollFrozenCols: true, |
| 81 | + theme: VTable.themes.DEFAULT.extends({ |
| 82 | + scrollStyle: { |
| 83 | + visible: 'focus', |
| 84 | + width: 7, |
| 85 | + hoverOn: true |
| 86 | + }, |
| 87 | + frozenColumnLine: { |
| 88 | + shadow: { |
| 89 | + width: 3, |
| 90 | + startColor: 'rgba(225, 228, 232, 0.6)', |
| 91 | + endColor: 'rgba(225, 228, 232, 0.6)' |
| 92 | + } |
| 93 | + } |
| 94 | + }) |
| 95 | + }); |
| 96 | + |
| 97 | + const stateNode = document.getElementById('issue5187State')!; |
| 98 | + const updateState = () => { |
| 99 | + stateNode.textContent = [ |
| 100 | + `container: ${container.style.width}`, |
| 101 | + `frozen width: ${tableInstance.getFrozenColsWidth()}`, |
| 102 | + `content: ${tableInstance.getFrozenColsContentWidth()}`, |
| 103 | + `offset: ${tableInstance.getFrozenColsOffset()}`, |
| 104 | + `scrollLeft: ${tableInstance.getFrozenColsScrollLeft()}` |
| 105 | + ].join(' | '); |
| 106 | + }; |
| 107 | + const resizeContainer = (width: number) => { |
| 108 | + container.style.width = `${width}px`; |
| 109 | + tableInstance.resize(); |
| 110 | + updateState(); |
| 111 | + }; |
| 112 | + |
| 113 | + document.getElementById('issue5187ScrollFrozen')?.addEventListener('click', () => { |
| 114 | + tableInstance.stateManager.setFrozenColsScrollLeft(tableInstance.getFrozenColsOffset()); |
| 115 | + updateState(); |
| 116 | + }); |
| 117 | + document.getElementById('issue5187Wide')?.addEventListener('click', () => { |
| 118 | + resizeContainer(1600); |
| 119 | + }); |
| 120 | + document.getElementById('issue5187Narrow')?.addEventListener('click', () => { |
| 121 | + resizeContainer(1055); |
| 122 | + }); |
| 123 | + document.getElementById('issue5187Reproduce')?.addEventListener('click', () => { |
| 124 | + resizeContainer(1600); |
| 125 | + tableInstance.stateManager.setFrozenColsScrollLeft(tableInstance.getFrozenColsOffset()); |
| 126 | + requestAnimationFrame(() => { |
| 127 | + resizeContainer(1055); |
| 128 | + }); |
| 129 | + }); |
| 130 | + |
| 131 | + tableInstance.on(VTable.ListTable.EVENT_TYPE.RESIZE_COLUMN_END, updateState); |
| 132 | + updateState(); |
| 133 | + |
| 134 | + const release = tableInstance.release.bind(tableInstance); |
| 135 | + tableInstance.release = () => { |
| 136 | + document.getElementById('issue5187Toolbar')?.remove(); |
| 137 | + release(); |
| 138 | + }; |
| 139 | + |
| 140 | + (window as any).tableInstance = tableInstance; |
| 141 | +} |
0 commit comments