简体   繁体   English

使用数据表或 HTML 表从 JSON 数组构建复杂的头表

[英]Building Complex header table from JSON Array with Datatable or HTML Table

I have a JSON array of Objects which is loaded by Ajax.我有一个由 Ajax 加载的 JSON 对象数组。 JSON is simple records from database I need to show it in Datatable or generate simple HTML table. JSON 是来自数据库的简单记录,我需要在 Datatable 中显示它或生成简单的 HTML 表。 Problem is that data is in simple row format but I need to display it in table with complex header and some row and column are also calculated from other columns eg.问题是数据采用简单的行格式,但我需要将其显示在带有复杂标题的表格中,并且某些行和列也是从其他列计算出来的,例如。 average salary.平均工资。 JSON data is also dynamic which is have data of 1 Month or more than one Month. JSON 数据也是动态的,具有 1 个月或超过 1 个月的数据。

Below is the sample JSON data下面是示例 JSON 数据

[
    {
        "str": "41",
        "div": "Industries Div",
        "cat": "OFFICER",
        "mon": "Feb-2020",
        "sal": "4949892"
    },
    {
        "str": "248",
        "div": "Head Office",
        "cat": "OFFICER",
        "mon": "Feb-2020",
        "sal": "35394485"
    },
    {
        "str": "739",
        "div": "Health Div",
        "cat": "OFFICER",
        "mon": "Feb-2020",
        "sal": "96792134"
    },
    {
        "str": "2700",
        "div": "Education Div",
        "cat": "OFFICER",
        "mon": "Feb-2020",
        "sal": "111993656"
    },
    {
        "str": "567",
        "div": "Head Office",
        "cat": "STAFF",
        "mon": "Feb-2020",
        "sal": "31487211"
    },
    {
        "str": "2298",
        "div": "Education Div",
        "cat": "STAFF",
        "mon": "Feb-2020",
        "sal": "66639233.6"
    },
    {
        "str": "3865",
        "div": "Health Div",
        "cat": "STAFF",
        "mon": "Feb-2020",
        "sal": "140546070"
    },
    {
        "str": "241",
        "div": "Head Office",
        "cat": "OFFICER",
        "mon": "Jan-2020",
        "sal": "33243002"
    },
    {
        "str": "723",
        "div": "Health Div",
        "cat": "OFFICER",
        "mon": "Jan-2020",
        "sal": "94086161"
    },
    {
        "str": "2682",
        "div": "Education Div",
        "cat": "OFFICER",
        "mon": "Jan-2020",
        "sal": "112367649"
    },
    {
        "str": "549",
        "div": "Head Office",
        "cat": "STAFF",
        "mon": "Jan-2020",
        "sal": "30342281"
    },
    {
        "str": "2269",
        "div": "Education Div",
        "cat": "STAFF",
        "mon": "Jan-2020",
        "sal": "66289864"
    },
    {
        "str": "3852",
        "div": "Health Div",
        "cat": "STAFF",
        "mon": "Jan-2020",
        "sal": "139230281"
    }
]

And this is the screen shot of excel table in which format I need to build table这是我需要建立表格的格式的excel表格的屏幕截图

在此处输入图片说明

I have tried to build data table but having problem with multiple dynamic headers.我试图建立数据表,但有多个动态标题的问题。 I have tried and transform data and merged record based on Month and department.我已经尝试过基于月份和部门转换数据和合并记录。 But it did not fulfill the requirements.但它没有满足要求。

Below is the screenshot of datatable I have build so far以下是我迄今为止构建的数据表的屏幕截图

在此处输入图片说明

Use rowspan and colspan on the HTML DOM to setup the complex headers accordingly before initializing the data table.在初始化数据表之前,在 HTML DOM 上使用rowspancolspan来相应地设置复杂的标题。

This is a working example of multiple headers: https://datatables.net/examples/basic_init/complex_header.html这是多个标题的工作示例: https : //datatables.net/examples/basic_init/complex_header.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM