JSON array of arrays as datasource with column names in the first array

JSON array of arrays as datasource with column names in the first array

neloydasneloydas Posts: 2Questions: 1Answers: 0
edited May 2017 in Free community support

I have a json like this:

var dataSet = 
[
    [
      "datetime",
      "Service name",
      " End time",
      "details",
      "status"
    ],
    [
      "2017-05-30T16:00:00.000Z",
      "TV Service",
      "2017-05-30T17:00:00.000Z",
      [],
      "ready"
]

The json is coming from querying an api from another project.

I want to use this as datasource in the datatable. Also I want to rearrange the column names in this order while omitting the details. I am using columns.title to generate the column names from a config file.

                columns: [
                    { title: "Service" }, --- this one is Service Name
                    { title: "Scheduled start time" }, --- this one is datetime
                    { title: "Scheduled end time" }, 
                    { title: "Status" }
                ]

So far I have written it like this:

table.DataTable({
                columns: [
                    { title: "Service" },
                    { title: "Scheduled start time" },
                    { title: "Scheduled end time" }, 
                    { title: "Status" }
                ],
                    data: dataSet
                });

How to rearrange the order of the columns and also omit the first array which includes the column names so that it does not show up as data row in the table?

Answers

  • neloydasneloydas Posts: 2Questions: 1Answers: 0

    Found a way to rearrange the columns like this:

                    myTable = table.DataTable({
                        columns: defaultParams.columns,
                        "aaData": dataSet,
                        "aoColumnDefs": [{
                            "aTargets": [0],
                            "mData": [1]
                        },
                        {
                            "aTargets": [1],
                            "mData": [0]
                        },
                        {
                            "aTargets": [2],
                            "mData": [2]
                        },
                        {
                            "aTargets": [3],
                            "mData": [4]
                        },
                        {
                            "aTargets": [4],
                            "mData": [3]
                        }]
                    });
    

    However the column names are still coming up in the table as a row. Is there any way to remove first array from the datasSet? Or should I just remove it manually and then push the data into DataTables?

This discussion has been closed.