How to handle situations to convert ooData to aoData

How to handle situations to convert ooData to aoData

greendevelopergreendeveloper Posts: 4Questions: 0Answers: 0
edited April 2013 in General
Hi. I'm stuck in a current quandry at the moment in developing my website. I am using DataTables to display certain responses from my database and give the user more control over interaction. A major update was made to the database and it is no longer returning arrays of objects that DataTables normally would use to parse and construct rows out of. This update was necessary for other elements of the site to work more efficiently, but I really want to make DataTables work with it as well.

Is there any built in feature of DataTables that fires immediately upon success of an ajax request that I could use to convert this object to a format that DataTables would actually accept?

Thanks for any help I can get.

Replies

  • allanallan Posts: 63,208Questions: 1Answers: 10,415 Site admin
    fnServerData is probably want you want as that allows complete control over the Ajax call and you can modify the data as you wish.

    How is the data now being returned? An object?

    Allan
  • greendevelopergreendeveloper Posts: 4Questions: 0Answers: 0
    Ah, you are completely correct. I had for some reason thought the fnCallback on fnServerData was supposed to be a function I was providing, not that it was the hook back to the table's mainline.

    Yeah, the data is returned as an object of the rough form:

    [code]
    { "data": {
    obj1: {stuff},
    obj2: {stuff},
    //etc
    }
    }
    [/code]

    Currently, I'm copying data over and forming a new object from scratch, but I know that won't be feasible as the results list keeps growing over time. I'll try to find some more efficient way of handling it, but I can also find tips on that in other forums.

    Thanks Allan!
  • allanallan Posts: 63,208Questions: 1Answers: 10,415 Site admin
    Looks like you might be able to use $.map:

    [code]
    var data = $.map( json.data, function (el, idx) {
    return el;
    } );
    [/code]

    And then feed `data` into DataTables, since DataTables expects an array.

    Allan
This discussion has been closed.