Using the example that AG-Grid has generously provided [https://next.plnkr.co/edit/Gbwc2rBQKJRFbLxS?preview] I'm currently trying to use the numeric editor.
This example is from the ag-grid official site [https://www.ag-grid.com/javascript-grid-cell-editor/#example-cell-editing-using-angular-components]
One thing I've noticed is even on the example provided by AG-Grid, the backspace does not work.
I'm fairly new to AG-Grid and would appreciate any help!
This is the numeric-editor.ts that I am using:
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);
}
}