different data structures object tree of json

different data structures object tree of json

ycusoyycusoy Posts: 27Questions: 5Answers: 0

I enter the data from the url but it can't be processed by the DataTable. then I found an example from https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/keys.

json examples as follows:

{
            "data": {
                "AS": [
                    ["Boston", "15", "15", "center", "80.2%", "north"],
                    ["DC", "6", "6", "left", "0.12%", "south"]
                ],
                "England": [
                    ["Burn", "15", "15", "right", "80.2%", "west"],
                    ["Listle", "6", "6", "center", "0.12%", "east"]
                ],

            }
        }

assuming the data displayed is parallel like this.

City|State|A|B|Pos|%|Wind

AS|Boson|15|15|center|80.2%|north
AS|DC|6|6|left|...
England|Burn|15...
England|Listle...

i try this method but no function.

$(document).ready(function() {
    var table = $('#example').DataTable({
        "ajax": {
            "url": "link data from url",
            "dataSrc": "data"
        },
        "columns": [{
            "data": function(row, type, val, meta) {
                return Object.keys(row)[0]
            },
        }, {
            "data": function(row, type, val, meta) {
                return row[Object.keys(row)[0]]
            },
        }, {
            "data": function(row, type, val, meta) {
                return row[Object.keys(row)[0]]
            },
        }],
    });
});

This question has an accepted answers - jump to answer

Answers

  • kthorngrenkthorngren Posts: 20,300Questions: 26Answers: 4,769

    The first problem is that Datatables expects the row data to be in an array where each array element is a row. See the example you post here.

    The second problem is there isn't a way for Datatables to see this as two rows:

    "AS": [
                        ["Boston", "15", "15", "center", "80.2%", "north"],
                        ["DC", "6", "6", "left", "0.12%", "south"]
                    ],
    

    This is the structure that Datatables supports:

          {
                "data": [
                    ["AS","Boston", "15", "15", "center", "80.2%", "north"],
                    ["AS", "DC", "6", "6", "left", "0.12%", "south"],
                    ["England", "Burn", "15", "15", "right", "80.2%", "west"],
                    ["England", "Listle", "6", "6", "center", "0.12%", "east"]
                  ]
            }
    

    Here is an example:
    http://live.datatables.net/pixonona/1/edit

    You would either need to change your server script to retrieve the data in this format or add code to your server script to modify the structure or use '-option ajax.dataSrc` as a function to modify the response to be in this format.

    Kevin

  • ycusoyycusoy Posts: 27Questions: 5Answers: 0

    Can this data structure not be supported?

        {
                    "data": {
                        "AS": [
                            ["Boston", "15", "15", "center", "80.2%", "north"],
                            ["DC", "6", "6", "left", "0.12%", "south"]
                        ],
                        "England": [
                            ["Burn", "15", "15", "right", "80.2%", "west"],
                            ["Listle", "6", "6", "center", "0.12%", "east"]
                        ],
    
                    }
                }
    
  • colincolin Posts: 15,143Questions: 1Answers: 2,586
    Answer ✓

    No. Kevin gave an example of what's expected - your unique keys, i.e. "AS", "England", would make it impossible to navigate.

  • ycusoyycusoy Posts: 27Questions: 5Answers: 0

    thank you @colin. I did not stop there, then I applied it in the select option function outside the table and it turned out to work, then called the data below.

This discussion has been closed.