Deep, deep data

Deep, deep data

mmeadow5mmeadow5 Posts: 5Questions: 0Answers: 0
edited December 2012 in DataTables 1.9
I am trying to map a JSON structure where a subarray of hashes determine the row. I suspect this is too complex, and that I will need to format the data for datatables server-side, but here goes. . .

Sample JSON:
[code]
{
"arrayOuter": [{
"name": "bob",
"date": "today",
"arrayInner": [{
"hashInner0.0": "zero",
"hashInner0.1": "one",
}, {
"hashInner1.0": "zero",
"hashInner1.1": "one",
}, { . . .
[/code]

The table columns would then be:
| arrayOuter.name | arrayOuter.date | arrayInner.hashInner0.0 | arrayInner.hashInner0.1 |
| arrayOuter.name | arrayOuter.date | arrayInner.hashInner1.0 | arrayInner.hashInner2.1 |
| arrayOuter.name | arrayOuter.date | arrayInner.hashInner3.0 | arrayInner.hashInner3.1 |

I have tried several options, including sAjaxDataProp to set the source as arrayInner, but keep getting an error of:
"Cannot read property 'length' of undefined"

Is there any way for this JSON structure to map directly onto a datatables object?

Replies

  • allanallan Posts: 63,210Questions: 1Answers: 10,415 Site admin
    edited December 2012
    DataTables _almost_ copes with that - the one thing that trips it up is the `.` in your `hashInner` object parameter names. DataTables uses a `.` in the same way as dotted Javascript notation, including array indexes - i.e. how to address nested objects. So `arrayOuter.0.arrayInner.0.hashInner0.0' is looking for:

    [code]
    {
    "arrayOuter": [{
    "name": "bob",
    "date": "today",
    "arrayInner": [{
    "hashInner0": [ "zero" ]
    ...
    [/code]

    There is a `[]` option which will effectively pluck properties from arrays - i.e.: `arrayOuter[, ].name` would pluck all `name` parameters and comma separate them.

    So yes the basic idea works, but the `.` is already used, so that won't work at the moment (I'll look at adding an escape option in future). If you can change it to `hashInner0-0` then that would work.

    Allan
  • mmeadow5mmeadow5 Posts: 5Questions: 0Answers: 0
    Sorry, I posted a bad contrived example. I don't need hashInner3.0. I am just trying to use an array of hashes (the outerArray), where a member of the hash is also an array of hashes (the innerArray). It is the innerArray that should determine the number of rows, but there also a member of the outerArray that is needed - this value will be repeated in the table for the length of the innerArray.

    Using "[]" option is almost exactly what is needed, except instead of comma separated, each member should start a new row.
  • allanallan Posts: 63,210Questions: 1Answers: 10,415 Site admin
    > Using "[]" option is almost exactly what is needed, except instead of comma separated

    The characters found between the `[` and `]` are used as the delimited, so they can easily be changed.

    However, if you want each on a different row - that isn't possible with multi-dimentional arrays. However, based on your data, I guess you might only need the sAjaxDataProp option - set it to `arrayOuter.arrayInner` and it should work just fine.

    Allan
  • mmeadow5mmeadow5 Posts: 5Questions: 0Answers: 0
    [code]arrayOuter.arrayInner[/code] is what is resulting in the "Cannot read property 'length' of undefined" error.
  • allanallan Posts: 63,210Questions: 1Answers: 10,415 Site admin
    Can you link me to a test case please so I'm not guessing what the parameters will be :-)

    Allan
  • mmeadow5mmeadow5 Posts: 5Questions: 0Answers: 0
    [quote]However, if you want each on a different row - that isn't possible with multi-dimentional arrays. [/quote]

    I think you have already answered my question, but here is a less confusing example:
    [code]{
    "aaData": [{
    "engine": "Trident",
    "browsers: [{ "browser": "Internet Explorer 4.0",
    "platform": "Win 95+",
    },{
    "browser": "Internet Explorer 5.0",
    "platform": "Win 95+",
    }]
    },{
    "engine": "Gecko",
    "browsers: [{ "browser": "Firefox 6.0",
    "platform": "Linux",
    },{
    "browser": "Firefox 7.0",
    "platform": "Linux",
    }]
    }]
    }
    [/code]

    The resulting table headers would be:
    | Engine | Browser | Platform |

    Thanks for all of your help and feedback!
  • allanallan Posts: 63,210Questions: 1Answers: 10,415 Site admin
    You'd need to flatten that into an array with an entry for each row. Parsing multiple rows like that, while possible, probably wouldn't be easy to describe in the initialisation options.

    Allan
  • mmeadow5mmeadow5 Posts: 5Questions: 0Answers: 0
    I thought that might be the answer, but I wanted to check to be sure. Thanks!
This discussion has been closed.