looping through an array of json objects with keys to other json objects

looping through an array of json objects with keys to other json objects

collincouchcollincouch Posts: 3Questions: 2Answers: 0
edited December 2013 in DataTables 1.9
My test page can be found at http://live.datatables.net/opizil/54/edit#source. The jQuery code is below. I have an array of three objects with keys containing other json objects containing the columns I want to display. I've created an inner and outer loop to try to access the Data and aTarget properties, but I'm only able to render the first object in the array correctly and get the [quote]unknown parameter for the data source for row one[/quote] error message upon loading. Can anyone provide some insight into what's wrong with my code?

[code]
$(document).ready(function() {

var my_cols = [ 'xxx', 'yyy','zzz'];
var my_cols1 = ['DateCreated','DateModified', 'Description', 'Name'];
var cols = [];

for ( var i=0, iLen=my_cols.length ; i

Replies

  • allanallan Posts: 63,498Questions: 1Answers: 10,470 Site admin
    So the problem here is that you are actually defining a table with 12 columns:

    [code]
    [{
    "mData": "xxx.DateCreated",
    "aTargets": [0]
    }, {
    "mData": "xxx.DateModified",
    "aTargets": [1]
    }, {
    "mData": "xxx.Description",
    "aTargets": [2]
    }, {
    "mData": "xxx.Name",
    "aTargets": [3]
    }, {
    "mData": "yyy.DateCreated",
    "aTargets": [0]
    }, {
    "mData": "yyy.DateModified",
    "aTargets": [1]
    }, {
    "mData": "yyy.Description",
    "aTargets": [2]
    }, {
    "mData": "yyy.Name",
    "aTargets": [3]
    }, {
    "mData": "zzz.DateCreated",
    "aTargets": [0]
    }, {
    "mData": "zzz.DateModified",
    "aTargets": [1]
    }, {
    "mData": "zzz.Description",
    "aTargets": [2]
    }, {
    "mData": "zzz.Name",
    "aTargets": [3]
    }]
    [/code]

    I'm presuming that isn't really want you want to do, but rather to have a table of four columns.

    Is this the format of your JSON data - a different key for every row entry in a nested object? If so, I think what will be needed is to modify the data slightly to collapse it to just a common property. Is the data being Ajax loaded, or is it coming from Javascript?

    Thanks,
    Allan
This discussion has been closed.