looping throuhcolumns?

looping throuhcolumns?

kerberonixkerberonix Posts: 7Questions: 5Answers: 0
edited July 2018 in Free community support

I am new to datatables and have json data like this:
[
0: {
Customers [
{
Name: "Harry",
Code: 1234
},
{
Name: "James",
Code: 5678
}
]
},
1: {
Customers [
{
Name: "Ben",
Code: 7890
},
{
Name: "karl",
Code: 4457
}
]
},
....
]

I need to be able to display this data but I don't necessarily know how many entries in the array there are

so far I have this:

    $("#customers-table").DataTable({
        type: "GET",
        ajax: {
            url: "myurl/something",
            dataSrc: ""
        },
        "columns": [
            {
                data: "Customers.0",
                render: function (value, type, row) {
                    return value.Name;
                }
            },
            { data: "Name" }
           ]
    });

I need to make "Customers.0" increment somehow, is this possible?

This question has an accepted answers - jump to answer

Answers

  • kthorngrenkthorngren Posts: 20,309Questions: 26Answers: 4,770
    Answer ✓

    I need to make "Customers.0" increment somehow, is this possible?

    No.

    But you could use ajax.dataSrc as a function to loop through the ajax response and create something more like this:

    [
    {
    Name: "Harry",
    Code: 1234
    },
    {
    Name: "James",
    Code: 5678
    },
    {
    Name: "Ben",
    Code: 7890
    },
    {
    Name: "karl",
    Code: 4457
    }
    ]
    

    There is a function example in the docs. This can be returned in your function then you just need this in your columns.data config:

        "columns": [
            { data: "Name" },
            { data: "Code" },
           ]
    

    Kevin

This discussion has been closed.