Angular 如何在ag网格中的数字(仅限数字)单元格上退格?
使用AG Grid慷慨提供的示例[我目前正在尝试使用数字编辑器 此示例来自ag网格官方网站[ 我注意到的一件事是,即使在AG Grid提供的示例中,退格也不起作用 我是AG Grid的新手,非常感谢您的帮助 这是我正在使用的numeric-editor.ts:Angular 如何在ag网格中的数字(仅限数字)单元格上退格?,angular,typescript,input,ag-grid,Angular,Typescript,Input,Ag Grid,使用AG Grid慷慨提供的示例[我目前正在尝试使用数字编辑器 此示例来自ag网格官方网站[ 我注意到的一件事是,即使在AG Grid提供的示例中,退格也不起作用 我是AG Grid的新手,非常感谢您的帮助 这是我正在使用的numeric-editor.ts: import {AfterViewInit, Component, ViewChild, ViewContainerRef} from "@angular/core"; import {ICellEditorAngularComp}
import {AfterViewInit, Component, ViewChild, ViewContainerRef} from
"@angular/core";
import {ICellEditorAngularComp} from "ag-grid-angular";
@Component({
selector: 'numeric-cell',
template: `<input #input (keydown)="onKeyDown($event)"
[(ngModel)]="value" style="width: 100%">`
})
export class NumericEditor implements ICellEditorAngularComp,
AfterViewInit {
private params: any;
public value: number;
private cancelBeforeStart: boolean = false;
@ViewChild('input', {read: ViewContainerRef}) public input;
agInit(params: any): void {
this.params = params;
this.value = this.params.value;
// only start edit if key pressed is a number, not a letter
this.cancelBeforeStart = params.charPress &&
('1234567890'.indexOf(params.charPress) < 0);
}
getValue(): any {
return this.value;
}
isCancelBeforeStart(): boolean {
return this.cancelBeforeStart;
}
// will reject the number if it greater than 1,000,000
// not very practical, but demonstrates the method.
isCancelAfterEnd(): boolean {
return this.value > 1000000;
};
onKeyDown(event): void {
if (!this.isKeyPressedNumeric(event)) {
if (event.preventDefault) event.preventDefault();
}
}
// dont use afterGuiAttached for post gui events - hook into
ngAfterViewInit instead for this
ngAfterViewInit() {
window.setTimeout(() => {
this.input.element.nativeElement.focus();
})
}
private getCharCodeFromEvent(event): any {
event = event || window.event;
return (typeof event.which == "undefined") ? event.keyCode :
event.which;
}
private isCharNumeric(charStr): boolean {
return !!/\d/.test(charStr);
}
private isKeyPressedNumeric(event): boolean {
const charCode = this.getCharCodeFromEvent(event);
const charStr = event.key ? event.key :
String.fromCharCode(charCode);
return this.isCharNumeric(charStr);
}
}
向ischarnumeric函数添加backspace成功了!您可能需要将比较更改为| | charStr=='\b';
private isCharNumeric(charStr): boolean {
return !!/\d/.test(charStr) || charStr === 'Backspace';
}