Tips for deleting on a numeric cell within ag-grid?

While exploring the functionality of AG-Grid with the example provided at this link [, I am currently experimenting with the numeric editor feature.

I found this example on the official AG-Grid website [https://www.ag-grid.com/javascript-grid-cell-editor/#example-cell-editing-using-angular-components]

An observation I've made is that even in the example provided by AG-Grid, the backspace functionality seems to be not working as expected.

Being relatively new to AG-Grid, any guidance or assistance on this matter would be greatly appreciated!

This snippet shows the code for the numeric-editor.ts file that I am currently utilizing:

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);
}
}

Answer №1

function isCharNumeric(inputChar): boolean {
    return !!/\d/.test(inputChar) || inputChar === 'Backspace';
}

Including the 'Backspace' condition in the isCharNumeric function resolved the issue!

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Accessing the menu

There are two main headings in my menu: Administration and Market When I click on the Administration heading, submenus for Portfolio and Corporate Action are displayed The issue I am facing is that if I then try to open the Market section, the Administra ...

Leveraging ag-grid within a React application

Within my application, there exists a structure with four distinct tabs. Each tab is intended to display a unique dataset, yet at this time, only two datasets have been defined. Upon executing the code provided below, an error is produced indicating an i ...

Issue encountered after updating to Spartacus 3.0 from 2.0: Unable to access the 'findStores' property due to a TypeError

After upgrading to Spartacus 3.0 from 2.0, everything seems to be working fine except for this particular error that keeps popping up. I followed the steps provided by SAP team on the documentation site to add the storefinder module. Error in spartacus-co ...

The application's functionality does not support the return type of `express-session

this.app.use( session({ // session configuration }) ); Encountering an error when passing the session configuration as shown above: Argument of type 'RequestHandler<ParamsDictionary, any, any, any>' is not assignabl ...

Exploring the Ways to Determine Array Type in Typescript Generics

I'm working with a method that looks like this: public select(fieldName: keyof TType) In this scenario, TType can potentially be an array type. If fieldName is called with a type of User[], I want to access the properties of User instead of the defa ...

Navigate to the identical component using Angular

There is a situation where a user visits /details;id=1. In the DetailsComponent, there is a table displaying similar objects with a button that redirects to /details;id=x, where x represents the id of the object. After clicking on the button, the URL para ...

Dynamic controller in TypeScript with React Hook Form

Is there a way to ensure that the fieldName is always inside the control? Passing a field name that doesn't exist in the form causes issues for me, so I'm looking for a solution. Can anyone help? import { Control, Controller } from 'react-ho ...

Assign a specific HTML class to serve as the container within an Angular directive

Is there a way to dynamically add and set an HTML class using an Angular directive with a parameter? Let's consider a scenario where we have a div with an existing class but no directive: <div class="myClass"></div> Now, if we w ...

What is the process of creating the /dist folder in an unreleased version of an npm package?

Currently working on implementing a pull request for this module: https://github.com/echoulen/react-pull-to-refresh ... It seems that the published module generates the /dist folder in the package.json prepublish npm script. I have my local version of the ...

Setting a maximum value for a text box input in Angular for enhanced bot functionality

Currently, I am working with Angular 7 and I find myself trying to incorporate the min and max properties into a textbox. However, I seem to be encountering some difficulties in achieving this. <div> <input type='text' [(ngModel)]= ...

Guide to receiving input in Python with spaces between values

Looking to collect the input in one line from the user like so: Abc 0 1 0 What's the correct way to achieve this? I've attempted: map(int,input().split()) input() and then split input().split(" ") Unfortunately, I'm encountering ...

How to transfer files between Dropbox and AWS S3 using Angular 5

Currently, I'm utilizing the Dropbox file picker to download a file. Once a file is selected using the Dropbox picker, I obtain the download link. I'm wondering if it's possible to save it as a bytestream in the browser and then upload it t ...

What is the method for determining the width of a Mat-Table once it has been displayed?

When utilizing Angular Material Mat-Table in conjunction with Angular 8, I am passing the dataSource dynamically. The number of rows and columns varies each time. Is there a method to calculate the width of the table once it is rendered on the screen? &l ...

A guide on how to identify the return type of a callback function in TypeScript

Looking at this function I've created function computedLastOf<T>(cb: () => T[]) : Readonly<Ref<T | undefined>> { return computed(() => { const collection = cb(); return collection[collection.length - 1]; }); } Thi ...

The state of dynamically created Angular components is not being preserved

My current task involves dynamically creating multiple components to be placed in a table. The code successfully achieves this objective, but the state seems to be getting jumbled up at the level of the dynamically generated components. When a component is ...

Why does "excess property checking" seem pleased when I utilize a key from set A or set B, even though "keyof (A|B)" is consistently "never"?

I am diving deeper into Typescript types and encountering some puzzling behavior: interface Person { name: string; } interface Lifespan { birth: number; death?: number; } let k: keyof (Person | Lifespan); //k is never let test1: Person | Life ...

I'm experiencing an issue with my website where it appears broken when I build it, but functions properly when I use npm run dev in Next

For my project, I have utilized NextJs, Tailwind, React, and Typescript. It is all set and ready to be hosted. After using "output: export" in next.config.js and running npm run build, the process was successful. However, when viewing my website locally, I ...

Utilizing PrimeNG menu items to bind commands to a base class function

I'm struggling to connect a parent class function with my Angular 2 PrimeNG menu options. HTML <p-menu #menu popup="popup" [model]="exportItems"></p-menu> <button type="button" class="fa fa-download" title="Export As" (click)="menu.to ...

How can I obtain the model values for all cars in the primary object?

const vehicles={ vehicle1:{ brand:"Suzuki", model:565, price:1200 }, vehicle2:{ brand:"Hyundai", model:567, price:1300 }, vehicle3:{ brand:"Toyota", model ...

Required attributes not found for data type in TypeScript

When the following code snippet is executed: @Mutation remove_bought_products(productsToBeRemoved: Array<I.Product>) { const tmpProductsInVendingMachine: Array<I.Product> = Object.values(this.productsInVendingMachine); const reducedPro ...