Issues with Ajax array of objects

Issues with Ajax array of objects

dkoydkoy Posts: 3Questions: 1Answers: 0

Link to test case: http://dkoy.org/ql/index.html?guild=active%20players&server=GLOBAL
Debugger code (-): https://debug.datatables.net/ojuwuc
Error messages shown: DataTables warning: table id=example - Requested unknown parameter '0' for row 0, column 0. For more information about this error, please see http://datatables.net/tn/4
Description of problem:
I am fairly new to js and JSON and have been smashing my face against this trial-and-error style for 2 nights and it's time to concede and ask for assistance. I feel like my raw JSON is formatted similarly to the example on https://datatables.net/examples/ajax/objects. The correct number of rows are being returned but I am receiving the above error. I am able to log the returned json to the console and access individual attributes using:

table.on( 'xhr', function () {
        var json = table.ajax.json();
        console.log(json);
        console.log(json.heroPlans[0].id);
        alert( json.heroPlans.length +' row(s) were loaded' );
    } );

Here is the js

  var table = $('#example').DataTable({

        "ajax": {
            "url": "js/guild_plan.json" + "?server=" + server + "&guild=" + guild,
            "dataSrc": "heroPlans",
        },
        columns: [
            {"heroPlans": "id"},
            {"heroPlans": "name"},
            {"heroPlans": "heroPower"},
            {"heroPlans": "health"},
            {"heroPlans": "battleEventMulti"},
            {"heroPlans": "row1Bonus"},
            {"heroPlans": "row2Bonus"},
            {"heroPlans": "row3Bonus"},
            {"heroPlans": "row4Bonus"}            
        ],
        paging: false,
    });

Here is a sample of the JSON

{
    "guildId": 52073,
    "name": "ACTIVE PLAYERS",
    "heroPlans": [{
        "id": 1876241,
        "name": "SIRSARGE",
        "heroPower": 3940131,
        "health": 1073890,
        "attack": 1276726,
        "defense": 762637,
        "magic": 826878,
        "battleEventMulti": 0.0,
        "row1Bonus": null,
        "row2Bonus": null,
        "row3Bonus": null,
        "row4Bonus": null
    }, {
        "id": 2073569,
        "name": "ODDPANDA",
        "heroPower": 3138061,
        "health": 856366,
        "attack": 979995,
        "defense": 614617,
        "magic": 687083,
        "battleEventMulti": 0.0,
        "row1Bonus": null,
        "row2Bonus": null,
        "row3Bonus": null,
        "row4Bonus": null
    }
    ...
}

Thanks much in advance

Replies

  • colincolin Posts: 15,237Questions: 1Answers: 2,599

    Yep, you were close, and you're probably going to kick yourself - you rcolumns declaration should look like this:

          columns: [
              {"data": "id"},
              {"data": "name"},
              {"data": "heroPower"},
              {"data": "health"},
              {"data": "battleEventMulti"},
              {"data": "row1Bonus"},
              {"data": "row2Bonus"},
              {"data": "row3Bonus"},
              {"data": "row4Bonus"}           
          ]
    

    See here,

    Colin

  • dkoydkoy Posts: 3Questions: 1Answers: 0

    Thank you very much and I did kick myself. I wrongly assumed when we set the dataSrc to something, we needed to reference it in columns.

This discussion has been closed.