Ag-grid不显示嵌套的json

时间:2017-09-10 07:01:51

标签: cell ag-grid

我有json。我想把它渲染成ag-grid。所有字段都正常工作,但我无法呈现possible_positions个项目。我做错了什么?

[
  {
    "id": 1,
    "surname": "Иванов",
    "stop_sheet_status_id": 1,
    "status_treatment": {
      "id": 1,
      "display_name": "active",
      "created_at": "2017-09-08T06:50:19.043Z",
      "updated_at": "2017-09-08T06:50:19.043Z"
    },
    "nationality": {
      "id": 1,
      "display_name": "nationality",
      "created_at": "2017-09-08T06:50:16.441Z",
      "updated_at": "2017-09-08T06:50:16.441Z"
    },
    "possible_positions": [
      {
        "id": 1,
        "display_name": "programmer",
        "created_at": "2017-09-08T06:50:17.883Z",
        "updated_at": "2017-09-08T06:50:17.883Z"
      },
      {
        "id": 2,
        "display_name": "engineer",
        "created_at": "2017-09-08T06:50:17.964Z",
        "updated_at": "2017-09-08T06:50:17.964Z"
      },
      {
        "id": 3,
        "display_name": "worker",
        "created_at": "2017-09-08T06:50:18.053Z",
        "updated_at": "2017-09-08T06:50:18.053Z"
      }
    ]
  }
]

example.js

 {
       headerName: "treatment date", field: "treatment_date",
       width: 100, pinned: true
    },
    {
      headerName: "nat display ", field: "nationality.display_name",
      width: 180, pinned: true
    },
    {
      headerName: "position", field: 
      "possible_positions[0].display_name",
      width: 180, pinned: true
    }...

1 个答案:

答案 0 :(得分:1)

您需要为该列定义value getter。获取值的默认方法假定数据仅在嵌套对象中,而不在数组中。这就是nationality.display_name适用于nat display列的原因。

这是你定义valueGetter的方法

{
  headerName: "position", 
  valueGetter: params => params.data.possible_positions[0].display_name,
  width: 180, pinned: true
}

实际上,在查看源代码时, 可能会 能够逃脱这一点:

{
  headerName: "position", field: 
  "possible_positions.0.display_name",
  width: 180, pinned: true
}

由于源在.上拆分提供的字段名称,因此使用括号表示法递归地挖掘对象。