Как создать файл JSON для дальнейшего преобразования в таблицу (html)? - PullRequest
0 голосов
/ 11 июля 2019

В PostgreSQL у меня есть таблица с такими данными. Как вы можете заметить, здесь хранится информация об иерархических отношениях между отделами. Например, Google является родителем Google Play, Google Analytics является родителем Google Finance.

| ORGANIZATION_ID | ORGANIZATION_NAME | ORGANIZATION_RANG | PARENT_ORGANIZATION_ID | PRODUCT_PERCENT |
|-----------------|-------------------|-------------------|------------------------|-----------------|
| 1               | Google            | 1                 |                        | 50              |
| 2               | Google Play       | 2                 | 1                      | 30              |
| 3               | Google News       | 2                 | 1                      | 25              |
| 4               | Google Analytics  | 2                 | 1                      | 77              |
| 5               | Google Finance    | 3                 | 4                      | 88              |
| 6               | Apple             | 1                 |                        | 35              |
| 7               | Apple Pay         | 2                 | 6                      | 73              |

В моем приложении Go я делаю SQL-запрос к этой таблице. Я хочу сгенерировать такое JSON, которое затем могу отправить клиентскому приложению:

{
    "tag": "table",
    "class": null,
    "rowspan": null,
    "colspan": null,
    "content": null
    "body": [
        {
            "tag": "tr",
            "class": null,
            "rowspan": null,
            "colspan": null,
            "content": null,
            "body": [
                {
                    "tag": "td",
                    "class": null,
                    "rowspan": "5",
                    "colspan": null,
                    "content": "Google",
                    "body": null
                },
                {
                    "tag": "td",
                    "class": null,
                    "rowspan": null,
                    "colspan": "2",
                    "content": "Google",
                    "body": null
                },
                {
                    "tag": "td",
                    "class": null,
                    "rowspan": null,
                    "colspan": null,
                    "content": "50",
                    "body": null
                }
            ]
        },
        {
            "tag": "tr",
            "class": null,
            "rowspan": null,
            "colspan": null,
            "content": null,
            "body": [
                {
                    "tag": "td",
                    "class": null,
                    "rowspan": null,
                    "colspan": "2",
                    "content": "Google Play",
                    "body": null
                },
                {
                    "tag": "td",
                    "class": null,
                    "rowspan": null,
                    "colspan": null,
                    "content": "30",
                    "body": null
                }
            ]
        },
        {
            "tag": "tr",
            "class": null,
            "rowspan": null,
            "colspan": null,
            "content": null,
            "body": [
                {
                    "tag": "td",
                    "class": null,
                    "rowspan": null,
                    "colspan": "2",
                    "content": "Google News",
                    "body": null
                },
                {
                    "tag": "td",
                    "class": null,
                    "rowspan": null,
                    "colspan": null,
                    "content": "25",
                    "body": null
                }
            ]
        },
        {
            "tag": "tr",
            "class": null,
            "rowspan": null,
            "colspan": null,
            "content": null,
            "body": [
                {
                    "tag": "td",
                    "class": null,
                    "rowspan": "2",
                    "colspan": null,
                    "content": "Google Analytics",
                    "body": null
                },
                {
                    "tag": "td",
                    "class": null,
                    "rowspan": null,
                    "colspan": null,
                    "content": "Google Analytics",
                    "body": null
                }
                {
                    "tag": "td",
                    "class": null,
                    "rowspan": null,
                    "colspan": null,
                    "content": "77",
                    "body": null
                }
            ]
        },
        {
            "tag": "tr",
            "class": null,
            "rowspan": null,
            "colspan": null,
            "content": null,
            "body": [
                {
                    "tag": "td",
                    "class": null,
                    "rowspan": null,
                    "colspan": null,
                    "content": "Google Finance",
                    "body": null
                },
                {
                    "tag": "td",
                    "class": null,
                    "rowspan": null,
                    "colspan": null,
                    "content": "88",
                    "body": null
                }
            ]
        },
        {
            "tag": "tr",
            "class": null,
            "rowspan": null,
            "colspan": null,
            "content": null,
            "body": [
                {
                    "tag": "td",
                    "class": null,
                    "rowspan": "2",
                    "colspan": null,
                    "content": "Apple",
                    "body": null
                },
                {
                    "tag": "td",
                    "class": null,
                    "rowspan": null,
                    "colspan": "2",
                    "content": "Apple",
                    "body": null
                },
                {
                    "tag": "td",
                    "class": null,
                    "rowspan": null,
                    "colspan": null,
                    "content": "35",
                    "body": null
                }
            ]
        },
        {
            "tag": "tr",
            "class": null,
            "rowspan": null,
            "colspan": null,
            "content": null,
            "body": [
                {
                    "tag": "td",
                    "class": null,
                    "rowspan": null,
                    "colspan": "2",
                    "content": "Apple Pay",
                    "body": null
                },
                {
                    "tag": "td",
                    "class": null,
                    "rowspan": null,
                    "colspan": null,
                    "content": "75",
                    "body": null
                }
            ]
        },
    ]
}

ВОПРОС : Может ли кто-нибудь помочь с логикой формирования HTML-тегов и значений rowspan и colspan ?! Я очень запутался и мне нужен совет.

P.S. С помощью этого файла JSON я могу создать такую ​​HTML-таблицу в клиентском приложении: enter image description here

HTML-разметка таблицы выглядит так:

<table>
    <tr>
        <td rowspan="5">Google</td>
        <td colspan="2">Google</td>
        <td>50</td>
    </tr>
    <tr>
        <td colspan="2">Google Play</td>
        <td>30</td>
    </tr>
    <tr>
        <td colspan="2">Google News</td>
        <td>25</td>
    </tr>
    <tr>
        <td rowspan="2">Google Analytics</td>
        <td>Google Analytics</td>
        <td>77</td>
    </tr>
    <tr>
        <td>Google Finance</td>
        <td>88</td>
    </tr>
    <tr>
        <td rowspan="2">Apple</td>
        <td colspan="2">Apple</td>
        <td>35</td>
    </tr>
    <tr>
        <td colspan="2">Apple Pay</td>
        <td>73</td>
    </tr>
</table>
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...