JSON data that does not have aaData node

JSON data that does not have aaData node

PaoloValladolidPaoloValladolid Posts: 35Questions: 0Answers: 0
edited August 2010 in General
I am using Datatables 1.6. The JSON data I am working with was generated by Java/Spring 3.x. Spring 3.x has built in capability to transform Java objects into JSON. In the JSON example below, the JSON was generated from 3 Java objects:

[
{"description":"Contamination (soil or product)","code":"C","isActive":"Y"},
{"description":"Domestic Animal (pre 1998 reg or undetermined)","code":"D","isActive":"Y"},
{"description":"Domestic Animal - Fatality","code":"DA","isActive":"Y"}
]

Note there is no aaData node. I figured out how to hack around the lack of an aaData so that I can display the above in Datatables. However, is there some Datatables 1.6 or Datatables 1.7 functionality that I overlooked that can parse JSON data like the above into an aaData array?

Replies

  • allanallan Posts: 63,520Questions: 1Answers: 10,473 Site admin
    You need to convert your array of objects in an array of arrays. There is no way for DataTables to know about object properties such as "description" or "code" at the moment, since it will be different for every application - so I decided to go for the approach of a generic 2D array.

    Here is an example of how it can be done for the OpenSearch format http://datatables.net/plug-ins/server-data-formats . Your own plug-in can be much simpler - just a couple of lines will do it.

    Allan
  • xiongxaoxiongxao Posts: 6Questions: 0Answers: 0
    PaoloValladolid,

    Can you post your hack for not needing the aaData?
  • allanallan Posts: 63,520Questions: 1Answers: 10,473 Site admin
    There is the following line in _fnInitComplete:

    [code]
    _fnAddData( oSettings, json.aaData[i] );
    [/code]
    that defines the property used for Ajax source (there are a few other instances of aaData, depending on your data source - server-side processing for example). I'm thinking about making this configurable in 1.8.

    Allan
  • xiongxaoxiongxao Posts: 6Questions: 0Answers: 0
    Thank you for the solution Allan. By the way, you've got a great project going here. I've seen this project used in a couple of live sites. I like the idea that it can be configurable. Also, will the future releases be able to handle multiple levels of arrays of objects?

    For instance:
    [code]
    {"aaData":[{"custID":999,"code":"CA","isActive":"Y", "orders":[{orderID:22,amount:44,productID:443},{orderID:66,amount:90,productID:567}]},
    {"custID":123,"code":"DA","isActive":"Y", "orders":[{orderID:1,amount:50,productID:123},{orderID:45,amount:10,productID:456}]}
    ]}
    [/code]
  • allanallan Posts: 63,520Questions: 1Answers: 10,473 Site admin
    Yes! I've actually just implemented that for 1.8 and I'm quite pleased with how it has come out. I've written the unit tests for it, so it is stable, but I've yet to write the examples. I think I'll probably also write a tutorial on how this kind of integration can be done with DataTables.

    Regards,
    Allan
  • xiongxaoxiongxao Posts: 6Questions: 0Answers: 0
    When do you plan to release 1.8? Possible I can get an early copy? :)
  • allanallan Posts: 63,520Questions: 1Answers: 10,473 Site admin
    Hoping to do a beta in the next week or two (depending on how on particular feature goes) :-). You can follow development on Github: https://github.com/DataTables/DataTables , or there is a nightly build from the development head which is available for download here: http://datatables.net/download/ .

    Regards,
    Allan
  • xiongxaoxiongxao Posts: 6Questions: 0Answers: 0
    Thank you Sir.
  • allanallan Posts: 63,520Questions: 1Answers: 10,473 Site admin
    I've just committed the change for the aaData property customisation. The default can now be overridden using the initialisation parameter "sAjaxDataProp". It can be used with Ajax sourced data for client-side processing, or server-side processing - when just Ajax sourced data it can be an empty string, which tells DataTables that it is just a plain array. Also quite cool is that you can use dotted notation to get nested information. For example "sAjaxDataProp": "my.data" - to get the data array from the 'my' property. Unit tests also committed :-)

    Any feedback on the new feature is welcome!

    Regards,
    Allan
This discussion has been closed.