【问题标题】:How to display enum value in ag-grid cell field value?如何在 ag-grid 单元格字段值中显示枚举值?
【发布时间】:2019-08-19 22:41:20
【问题描述】:

我正在使用 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';
            },
        },
    ];

这是我的 project-status.ts 文件

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

不知道如何实现这一点。请帮忙

【问题讨论】:

  • 您想根据cellValue 更改headerName
  • 对不起。我已经修改了我的问题以使其更有意义。我正在寻找单元格值

标签: typescript enums ag-grid ag-grid-ng2 ag-grid-angular


【解决方案1】:

使用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 &amp;&amp; data.value !== undefined),否则ProjectStatus.NS 将显示not found

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

详细参考:TypeScript Handbook - Enums

反向映射

除了创建具有属性名称的对象 对于成员,数字枚举成员也从 枚举值到枚举名称。例如,在这个例子中:

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

【讨论】:

  • 非常感谢帕里托什,也感谢打字稿链接。它有效。
猜你喜欢
  • 2020-08-20
  • 2022-12-15
  • 1970-01-01
  • 2017-09-14
  • 2020-10-11
  • 1970-01-01
  • 1970-01-01
  • 2018-12-25
  • 2022-12-03
相关资源
最近更新 更多