Search code examples
angular6ag-grid-angular

how to change the value of a cell when I choose a value in another


im currently using ag-grid in my aplication under angular 6 and my problem is: I have a row with 3 columns, | A | B | C |

A is a select with some names when I select a value in the row A, I want B and C (of the same row) change their values based on a date from A.

A = { id:number , aStart:Date, aEnd:Date }

I tried to use startEditingCell and other ways buts no one works.

Which is the right way to do that?

export interface Delivery {
    id: number;
    deliveryCode: string;
    startPeriodDate: Date;
    endPeriodDate: Date;
    description: string;
    inactive: boolean;
}

export class lineColumn {
    deliveryid: number
    startDate: Date
    endDate: Date
}
const columns = {
                headerName: 'Delivery',
                colId: 'delivery',
                field: 'id',
                editable: this.isGridEditableOnCondition,
                cellEditor: 'agRichSelectCellEditor',
                cellRenderer: this.deliveryFormatter.bind(this),
                cellEditorParams: (params) => {
                    return {
                        values: this.deliveries.map((delivery) => delivery.id),
                        formatValue: this.deliveryFormatter.bind(this),
                        displayPropertyName: 'deliveryCode',
                        valuePropertyName: 'deliveryCode',
                        displayFormat: 'deliveryCode',
                    };
                },
                onCellValueChanged: (params) => {
                    // when this value changes, the other 2 columns must change with the values of dates that delivery has (but can be still editables)
                    params.api.refreshCells();
                },
            },
            {
                headerName: 'Shipping Start Date',
                colId: 'shippingStartDate',
                field: startDate,
                editable: this.isGridEditable,
                valueFormatter: this.uiService.dateFormatter,
                cellEditor: 'atrDate',
            },
            {
                headerName: 'Shipping End Date',
                colId: 'shippingEndDate',
                field: endDate,
                editable: this.isGridEditable,
                valueFormatter: this.uiService.dateFormatter,
                cellEditor: 'atrDate',
            },

Solution

  • Finally:

     onCellValueChanged: (params) => {
    
                            const api: agGrid.GridApi = params.api;
                            const delivery = this.masterdata.deliveries.find((d: Delivery) => d.deliveryId === params.newValue);
                            api.getRowNode(params.node.rowIndex).setDataValue('shippingStartDate', delivery.startPeriodDate);
                            api.getRowNode(params.node.rowIndex).setDataValue('shippingEndDate', delivery.endPeriodDate);
    
                            api.refreshCells();
                        }
                    },