Vlad Danila
Vlad Danila

Reputation: 1372

How to set Max Length on ag-grid cells

Is there any way to impose a maxLength text allowed in an ag-grid cell, similar to the one on a normal input element?

  <input maxlength="220"/>

No relative documentation was found. Also, no particular situations & more details are needed, in my opinion. Thank you!

Upvotes: 2

Views: 10790

Answers (2)

Mike Gledhill
Mike Gledhill

Reputation: 29161

agGrid's documentation really isn't clear on this, but it is possible.

agGrid MaxLength

In your column definitions, just add something like this:

this.columnDefs = [
    {
        field: 'Surname',
        minWidth: 100,
        editable: true,
        cellEditor: 'agLargeTextCellEditor',
        cellEditorParams: { maxLength: 200 }
    }

Upvotes: 5

un.spike
un.spike

Reputation: 5113

Yes, you can control the full flow of input data, but you have to create your own cellEditor for that.

So - it shouldn't be hard to make a simple input validation.

and to achieve your requirements you have to take care of one function within the component:

// Gets called once when editing is finished (eg if enter is pressed).
// If you return true, then the result of the edit will be ignored.
isCancelAfterEnd?(): boolean;

isCancelAfterEnd() {
    return !this.isValid(this.eInput.value);
}

isValid(value) {
    return value.length <= this.maxLength;
}

Demo

Upvotes: 3

Related Questions