AG-Grid: Cannot update field in Grid as it is read only

空扰寡人 提交于 2019-12-11 17:14:49

问题


I am trying to update a field in ag-grid and it keeps coming back with ERROR TypeError: Cannot assign to read only property 'quantity' of object '[object Object]'.

It allows me to edit the cell but when i try to update it comes up with this error.

I am using an observable from an ngrx store to populate the rowData as below:

this.order$.subscribe(order => {
            this.order = { ...order, products: [...order.products] };
        });

I have checked the above and I can update this object by saying this.orders.products = [] which says to me it is no longer immutable.

 <mi-ag-grid [data]="order.products" [columnDefs]="columnDefs" [suppressClickEdit]="false"></mi-ag-grid>

columnDefs = [
        {
            headerName: 'Code',
            field: 'code',
            width: 150
        },
        {
            headerName: 'Name',
            field: 'name',
            width: 200,
            editable: true
        },
        {
            headerName: 'Quantity',
            field: 'quantity',
            width: 150,
            editable: true
        }
    ];

I next tried suggestion from @GreyBeardedGeek and tried to set value with a valueSetter as per below, but still getting same error.

,
    {
        headerName: 'Quantity',
        width: 150,
        editable: true,
        field: 'quantity',
        valueSetter: quantityValueSetter
    },


function quantityValueSetter(params): any {
console.log(params);
params.data.quantity = params.newValue;

}


回答1:


The object that you are getting from the store and using in the grid is immutable.

By default, when you make a column in the grid editable, it will attempt to update the backing object directly, and this is the problem that you are seeing - the grid attempts to mutate an immutable object.

The solution is to add a "valueSetter" property to the grid column's definition. The value of this property should be a function that will receive the new value, and then use that new value to update the store.

When you have this property set, ag-grid will not try to update the object directly.



来源:https://stackoverflow.com/questions/53572072/ag-grid-cannot-update-field-in-grid-as-it-is-read-only

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!