如何在 ag-grid 单元格字段值中显示枚举值?

How to display enum value in ag-grid cell field value?

我正在使用 ag-grid 最新版本 (^20)。我尝试了单元格渲染但不确定如何显示字符串而不是枚举。

在sample.component.html代码中

<ag-grid-angular 
    #agGrid style="width: 100%; height: 350px;" 
    class="ag-theme-balham"
    [gridOptions]="gridOptions"
    [columnDefs]="columnDefs"
    [showToolPanel]="showToolPanel"
    [defaultColDef]="defaultColDef"
    [rowData]="rowData">
</ag-grid-angular>

在sample.component.ts代码中

 export const PROJECT_LIST_COLUMNS = [
        { headerName: 'Code', field: 'code', width: 120 },
        { headerName: 'Name', field: 'name', width: 200 },
        { headerName: 'Customer', field: 'customerName', width: 200 },
        { headerName: 'Manager', field: 'manager', width: 150 },
        { headerName: 'Project Group', field: 'projectGroup', width: 150 },

        {
            headerName: 'End Date',
            field: 'endDate',
            width: 130,
        },
        {
            headerName: 'Status', //what I want is from the below project status file I want the string 
            field: 'status', 
            width: 150,
            cellRenderer: data => {
                return data.value ? data.value : 'not found';
            },
        },
    ];

这是我的项目-status.ts文件

export enum ProjectStatus {
    Queue = -3,
    Hold= -2,
    Proposal = -1,
    NS= 0,
    WIP= 1,
    Completed = 2,
    Posted= 3,
    Closed = 4,
}

不确定如何实现。请帮助

使用ProjectStatus[data.value]获取枚举值的文本。

    {
        headerName: 'Status',
        field: 'status', 
        width: 150,
        cellRenderer: data => {
            return (data.value !== null && data.value !== undefined)
                    ? ProjectStatus[data.value] : 'not found';
        },
    }

注意:确保将您的条件更新为 (data.value !== null && data.value !== undefined),否则 ProjectStatus.NS 将显示 not found

参考: How does one get the names of TypeScript enum entries?

详细参考:TypeScript Handbook - Enums

Reverse mappings

In addition to creating an object with property names for members, numeric enums members also get a reverse mapping from enum values to enum names. For example, in this example:

enum Enum { A }  
let a = Enum.A;
let nameOfA = Enum[a]; // "A"