json format for DataTables

json format for DataTables

abramhumabramhum Posts: 5Questions: 3Answers: 0
edited May 2017 in Free community support

Hi:
deal all, I have a problem about the json format for DataTable.
The following is the txt which the DataTables needed:

{
    "data": [
        {
            "name": "Tiger Nixon",
            "position": "System Architect",
            "salary": "$320,800",
            "start_date": "2011\/04\/25",
            "office": "Edinburgh",
            "extn": "5421"
        },
        {
            "name": "Garrett Winters",
            "position": "Accountant",
            "salary": "$170,750",
            "start_date": "2011\/07\/25",
            "office": "Tokyo",
            "extn": "8422"
        }
    ]
}

And the code snippet is as below:

 $('#example').DataTable({
                "ajax": "simple.txt",
                "columns": [
                    { "data": "name" },
                    { "data": "position" },
                    { "data": "office" },
                    { "data": "extn" },
                    { "data": "start_date" },
                    { "data": "salary" }
                ]
            });

And the result is I need to add the '{"data:"' and ' }" to my json data each time,
why not just use the txt without the data tag, like following:

[
        {
            "name": "Tiger Nixon",
            "position": "System Architect",
            "salary": "$320,800",
            "start_date": "2011\/04\/25",
            "office": "Edinburgh",
            "extn": "5421"
        },
        {
            "name": "Garrett Winters",
            "position": "Accountant",
            "salary": "$170,750",
            "start_date": "2011\/07\/25",
            "office": "Tokyo",
            "extn": "8422"
        }
]

Is that possible to do that? Thanks a lot.

This question has an accepted answers - jump to answer

Answers

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

    You can use ajax.dataSrc. Take a look at the second example.

    Kevin

  • abramhumabramhum Posts: 5Questions: 3Answers: 0
    edited May 2017

    Dear Kevin:

    It seems not working. I have tried following:

       $('#example').DataTable({
                    "ajax": {
                        "url": "simple.txt",
                        "dataSrc": ""
                    }
    

    And the simple.txt is as below

    [
            {
                "name": "Tiger Nixon",
                "position": "System Architect",
                "salary": "$320,800",
                "start_date": "2011\/04\/25",
                "office": "Edinburgh",
                "extn": "5421"
            },
            {
                "name": "Garrett Winters",
                "position": "Accountant",
                "salary": "$170,750",
                "start_date": "2011\/07\/25",
                "office": "Tokyo",
                "extn": "8422"
            }
    ]
    

    It can not read the correct data.
    Is there anything I need to modify, thanks a lot.

  • allanallan Posts: 61,715Questions: 1Answers: 10,108 Site admin
    Answer ✓

    That looks absolutely spot on to me. Can you link to a test case showing the issue please?

    This example shows it working.

    Allan

  • abramhumabramhum Posts: 5Questions: 3Answers: 0

    Thanks a lot.

This discussion has been closed.