本帖最后由 Richard.Huang 于 2024-5-27 16:24 编辑
产品:SpreadJS
版本:V17.0.4
designer自定义celltype当单元格为空和没有任何改变时也会触发editCell
,希望自定义celltype和默认保持一致,当值没有变化时不触发editcell,下面是自定义cellType代码
- import * as GC from '@grapecity/spread-sheets'
- /**
- * 自定义单元格类型
- */
- type cellType = 'number' | 'integer' | 'identifier' | 'string'
- class CustomCellType extends GC.Spread.Sheets.CellTypes.Base {
- type: cellType
- precision: number
- constructor(type: cellType = 'string', precision: number = 2) {
- super()
- this.typeName = 'CustomCellType'
- this.type = type
- this.precision = precision
- }
- paint(ctx: CanvasRenderingContext2D, value: any, x: number, y: number, w: number, h: number, style: GC.Spread.Sheets.Style, context?: any): void {
- GC.Spread.Sheets.CellTypes.Base.prototype.paint.apply(this, [ctx, value, x, y, w, h, style, context])
- }
- createEditorElement(context?: any): HTMLInputElement {
- const input = document.createElement('input')
- input.setAttribute('data-col', context.col)
- input.setAttribute('data-row', context.row)
- return input
- }
- activateEditor(editorContext: HTMLInputElement, cellStyle: GC.Spread.Sheets.Style, cellRect: GC.Spread.Sheets.Rect, context?: any): void {
- if (editorContext) {
- GC.Spread.Sheets.CellTypes.Base.prototype.activateEditor.apply(this, [editorContext, cellStyle, cellRect, context])
- editorContext.style.position = 'absolute'
- const format = () => {
- const value = editorContext.value
- if (this.type == 'number') editorContext.value = value.replace(new RegExp(`^(-?\\d*\\.?\\d{0,${this.precision}}).*`), '$1')
- if (this.type == 'integer') editorContext.value = value.replace(/\D/g, '')
- if (this.type == 'identifier') editorContext.value = value.replace(/[^A-Za-z0-9/g, '')
- }
- editorContext.addEventListener('input', format)
- editorContext.addEventListener('change', format)
- }
- }
- deactivateEditor(editorContext: HTMLInputElement, context?: any): void {
- GC.Spread.Sheets.CellTypes.Base.prototype.deactivateEditor.apply(this, [editorContext, context])
- }
- getEditorValue(editorContext: HTMLInputElement, context?: any): string {
- return editorContext.value
- }
- setEditorValue(editorContext: HTMLInputElement, value: any, context?: any): void {
- if (value) editorContext.value = value
- }
- updateEditor(editorContext: HTMLInputElement, cellStyle: GC.Spread.Sheets.Style, cellRect: GC.Spread.Sheets.Rect, context?: any): GC.Spread.Sheets.Rect | any {
- if (editorContext) {
- editorContext.style.width = '1000px'
- editorContext.style.height = '1000px'
- }
- }
- }
- export default CustomCellType
复制代码
麻烦帮忙排查问题,不要再建议监听其他事件了,就editCell |