@jxs

Как отобразить данные в Json в иерархической форме?

У меня JSON отображается в таком виде:

[
  {
    "id": "197",
    "user_id": 471,
    "position": "CEO",
    "child_id_users": null
  },
  {
    "id": "338",
    "user_id": 342,
    "position": "Deputy. director",
    "child_id_users": "471"
  },
  {
    "id": "411",
    "user_id": 347,
    "position": "Chief Accountant",
    "child_id_users": "342"
  },
  {
    "id": "543",
    "user_id": 1003,
    "position": "personal driver №1",
    "child_id_users": "471"
  },
  {
    "id": "521",
    "user_id": 867,
    "position": "personal driver №2",
    "child_id_users": "342"
  },
...
]


Я хочу чтобы JSON отображался в иерархическом стиле, для того чтобы на клиенте можно было создать «организационную схему». То есть, он должен выглядеть примерно так:

[
  {
    "id": "197",
    "user_id": 471,
    "position": "CEO",
    "child_id_users": null,
     "children": [{
           {
             "id": "338",
             "user_id": 342,
             "position": "Deputy. director",
             "child_id_users": "471",
             "children": [{       
                           "id": "411",
                           "user_id": 347,
                           "position": "Chief Accountant",
                           "child_id_users": "342"
                          },
                          {
                           "id": "521",
                           "user_id": 867,
                           "position": "personal driver №2",
                           "child_id_users": "342"
                          }]
            },
            {
              "id": "543",
              "user_id": 1003,
              "position": "personal driver №1",
              "child_id_users": "471"
             }

      }]
  }
]


Возможно ли реализовать что-то подобное? Вот мой «index» из контроллера на данный момент:

def index
  @org_charts = OrgChart.all
  render json: JSON.pretty_generate(@org_charts.as_json)
end
  • Вопрос задан
  • 87 просмотров
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы