Get row child data as JSON

Get row child data as JSON

Marco SullaMarco Sulla Posts: 2Questions: 1Answers: 0

Hello all. I can I get the row child data as JSON? I tried with row.child.data, but it gets me a DataTables.Api object, and it does not have the toArray() method.

Answers

  • kthorngrenkthorngren Posts: 21,299Questions: 26Answers: 4,945

    Thats an interesting question. Since this is a dynamically created display which you can format however you like there aren't any API's to get the data. However I suspect the data is available to you as a Javascript variable that you used to generate the display.

    Kevin

  • Marco SullaMarco Sulla Posts: 2Questions: 1Answers: 0

    This is true, but it's not so simple.

    In my case, I need the data of the clicked row. Since there's no way to get the data with datatables, I had to attach them to the DOM element using Jquery data().
    But since I need to wait the subtable is added and there's no child.shown event, I had to use a MutationObserver on the table.

    Really overcomplicated for an apparently simple operation.

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

    Hi Marco,

    row().child() returns a jQuery object for the child rows - so you can parse that, either manually or with jQuery, to get the internal data, something like:

    table.row(2).child().html()
    "<td colspan="5"><table cellpadding="5" cellspacing="0" border="0" style="padding-left:50px;"><tbody><tr><td>Full name:</td><td>Ashton Cox</td></tr><tr><td>Extension number:</td><td>1562</td></tr><tr><td>Extra info:</td><td>And any further details here (images etc)...</td></tr></tbody></table></td>"
    

    Cheers,

    Colin

  • kthorngrenkthorngren Posts: 21,299Questions: 26Answers: 4,945
    edited May 2018

    Is your child an HTML table or a Datatable?

    You should be able to get the data using your favorite table methods if its an HTML table or with something like the Select extension if using Datatables for the child.

    If you need help maybe you can post an example of what you are doing:
    https://datatables.net/manual/tech-notes/10#How-to-provide-a-test-case

    Kevin

This discussion has been closed.