动态更改headerName ag-grid Angular 2?

时间:2017-09-20 07:29:56

标签: typescript ag-grid

我有这段代码:

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

和这个功能:

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

如您所见,FY mod将在请求后更改。如何在列中更改它?我再次尝试调用网格渲染功能,( this.gridOptions.api.refreshView(); ),但这对我没有帮助。

3 个答案:

答案 0 :(得分:5)

您可以在GridReady事件中将数据绑定到网格后动态分配标题名称。

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

//列def

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

params_1是列键而不是列名。

答案 1 :(得分:3)

我可以通过获取当前列def并更改标题名称来实现此目的

this.baseGrid.gridOptions.api.getColumnDef("clan").headerName = "WOW";

然后调用refreshHeader

this.baseGrid.gridOptions.api.refreshHeader();

或许首先尝试刷新,然后看看你是否需要直接访问columnDf ...希望这会有所帮助。

答案 2 :(得分:0)

不幸的是,您似乎无法使用相同的技术刷新自动列的标题文本。