|
| 1 | +/** |
| 2 | + * 单元格类型 - 自定义 |
| 3 | + * |
| 4 | + * 通过 TypeScript module augmentation 扩展 ColumnDefine, |
| 5 | + * 为内置 button 单元格增加一个自定义配置:highlightOnNegative。 |
| 6 | + * 注意: |
| 7 | + * - 当前运行时并不会使用 highlightOnNegative 配置。 |
| 8 | + * - Demo 的目的只是说明:用户可以安全地为 ColumnDefine 增加自定义列类型及其专有字段。 |
| 9 | + */ |
| 10 | + |
| 11 | +import * as VTable from '../../src'; |
| 12 | +import { bindDebugTool } from '../../src/scenegraph/debug-tool'; |
| 13 | + |
| 14 | +const ListTable = VTable.ListTable; |
| 15 | +const CONTAINER_ID = 'vTable'; |
| 16 | + |
| 17 | +/** |
| 18 | + * 这里演示库使用方如何通过模块扩展自定义列类型 |
| 19 | + * 注意:这个路径在真实使用场景下通常是 '@visactor/vtable/es/ts-types' |
| 20 | + */ |
| 21 | +declare module '../../src/ts-types/list-table/define' { |
| 22 | + /** |
| 23 | + * 为 ColumnBodyDefine 增加一个自定义按钮列配置 |
| 24 | + * - 在原有 button 能力基础上新增 highlightOnNegative |
| 25 | + */ |
| 26 | + interface CustomColumnBodyDefineMap { |
| 27 | + coloredButton: { |
| 28 | + cellType: 'button'; |
| 29 | + field?: string; |
| 30 | + text?: string; |
| 31 | + disable?: boolean; |
| 32 | + /** |
| 33 | + * 当单元格对应 value 为负数时是否高亮 |
| 34 | + * (运行时如何使用该配置由业务侧决定) |
| 35 | + */ |
| 36 | + highlightOnNegative?: boolean; |
| 37 | + style?: any; |
| 38 | + }; |
| 39 | + } |
| 40 | + |
| 41 | + /** |
| 42 | + * 给 ColumnDefine 增加一个完整的自定义列类型 |
| 43 | + * - 通常是 Header 配置 + Body 配置的组合 |
| 44 | + */ |
| 45 | + interface CustomColumnDefineMap { |
| 46 | + coloredButtonColumn: { |
| 47 | + // Header 相关字段(简化示例,实际可根据需要补充) |
| 48 | + title?: string; |
| 49 | + width?: number | 'auto'; |
| 50 | + |
| 51 | + // Body 相关字段 |
| 52 | + field?: string; |
| 53 | + cellType: 'button'; |
| 54 | + text?: string; |
| 55 | + disable?: boolean; |
| 56 | + highlightOnNegative?: boolean; |
| 57 | + style?: any; |
| 58 | + }; |
| 59 | + } |
| 60 | +} |
| 61 | + |
| 62 | +export function createTable() { |
| 63 | + const option: VTable.ListTableConstructorOptions = { |
| 64 | + container: document.getElementById(CONTAINER_ID), |
| 65 | + columns: [ |
| 66 | + { |
| 67 | + field: 'percent', |
| 68 | + title: 'percent', |
| 69 | + width: 120 |
| 70 | + }, |
| 71 | + { |
| 72 | + field: 'value', |
| 73 | + title: 'value', |
| 74 | + width: 120 |
| 75 | + }, |
| 76 | + // ⬇️ 这里就是一个“自定义类型扩展后的按钮列” |
| 77 | + { |
| 78 | + field: 'value', |
| 79 | + title: 'custom button', |
| 80 | + width: 'auto', |
| 81 | + cellType: 'button', |
| 82 | + disable: false, |
| 83 | + text: 'check', |
| 84 | + // 这个属性是通过 CustomColumnDefineMap 扩展进来的,当前运行时并不会使用该配置。Demo 的目的只是说明:用户可以安全地为 ColumnDefine 增加自定义列类型及其专有字段。 |
| 85 | + highlightOnNegative: true, |
| 86 | + style: { |
| 87 | + color: '#FFF' |
| 88 | + } |
| 89 | + } |
| 90 | + ], |
| 91 | + showFrozenIcon: true, |
| 92 | + widthMode: 'standard', |
| 93 | + defaultRowHeight: 80, |
| 94 | + heightMode: 'autoHeight' |
| 95 | + }; |
| 96 | + |
| 97 | + const instance = new ListTable(option); |
| 98 | + |
| 99 | + const records = [ |
| 100 | + { percent: '100%', value: 20 }, |
| 101 | + { percent: '80%', value: 18 }, |
| 102 | + { percent: '20%', value: 12 }, |
| 103 | + { percent: '0%', value: 10 }, |
| 104 | + { percent: '60%', value: 16 }, |
| 105 | + { percent: '40%', value: 14 }, |
| 106 | + { percent: '0%', value: -10 }, |
| 107 | + { percent: '0%', value: -10 } |
| 108 | + ]; |
| 109 | + |
| 110 | + // 设置表格数据 |
| 111 | + instance.setRecords(records); |
| 112 | + |
| 113 | + bindDebugTool(instance.scenegraph.stage as any, { |
| 114 | + // customGrapicKeys: ['role', '_updateTag'], |
| 115 | + }); |
| 116 | + |
| 117 | + instance.on(VTable.ListTable.EVENT_TYPE.BUTTON_CLICK, e => { |
| 118 | + console.log(VTable.ListTable.EVENT_TYPE.BUTTON_CLICK, e.col, e.row, e.event); |
| 119 | + }); |
| 120 | + |
| 121 | + // 只为了方便控制台调试用,不要拷贝到正式代码 |
| 122 | + (window as any).tableInstance = instance; |
| 123 | +} |
0 commit comments