Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dynamic change headerName ag-grid Angular 2?

I have this code:

public fy: string = "17";
let months = ['July', 'August', 'September', 'October', 'November', 'December', 'January', 'February', 'March', 'April', 'May', 'June',];

        function headerCellRendererFunc(year, params) {
            const eHeader = document.createElement('span');
            eHeader.innerText = `${params.colDef.headerName} ${year}`;
            return eHeader;
        }

        const januaryIndex = months.indexOf('January');
        months.map((month, index, arr) => {
            const year = januaryIndex === 0 || index > januaryIndex ? this.fy : this.fy + 1;

            return <ColDef>{
                headerName: month,
                headerCellRenderer: headerCellRendererFunc.bind(null, year),
                field: `data.${month}`,
                editable: isEditable,
                valueGetter: cellMonthValueGetter(month, index),
                cellFormatter: cellMonthValueFormatter,
                newValueHandler: cellMonthNewValueHandler(month),
                width: 100,
            };
        }).forEach((colDef) => colDefs.push(colDef));

and this function:

refresh(): void {
    let jobId = +this.route.snapshot.parent.params['id'];
    this.jobService.getJob(jobId).then(result => {
        this.job = result.Object;
        this.fy = result.Object.FinancialYearName;
        this.jobService.getTimePhasing(jobId).then(result => {
            this.setModel(result.Object);
        });
    });
}

As you can see, the FY mod will change after the request. How do I change it in columns? I tried again to call the grid rendering function, (this.gridOptions.api.refreshView ();) but that did not help me.

like image 737
Valitskiy Dmitriy Avatar asked Dec 03 '22 21:12

Valitskiy Dmitriy


1 Answers

You can assign header names dynamically after binding data into grid in on GridReady event.

this.gridOptionsDetail.api.getColumnDef("params_1").headerName = "Files Count (" + fileCount + )";
this.gridOptionsDetail.api.refreshHeader();

//Column def

this.columnDefs = [
                        {
                headerName: "Test Columns",
                field: "test1",
                cellStyle: { 'text-align': 'left' },
                suppressSorting: true,
                minWidth: 250,
                ColId:"params_1"
            }];

params_1 is column key not column name.

like image 192
Muni Chittem Avatar answered Dec 09 '22 15:12

Muni Chittem