Getting data back out of a datatable and submitting it as json

Getting data back out of a datatable and submitting it as json

bodrickbodrick Posts: 3Questions: 1Answers: 0

I have a datatable that can be populated either via an existing array or by adding data dynamically. I would like to get the data out of the datatable so I can then submit it to the server side.

My issue is when I try to get the data from the table either by using DataTable().data() or DataTable().rows().data() when I try to convert this into JSON using JSON.stringify() I get a TypeError converting circular structure to JSON.

If I inspect the DataTable().data() object that is returned,I can see my data in objects, but also the rest of the api etc, which I assume is where the JSON stringification is having problems.

My question is therefore, what is the easiest way to get the data back as an array or even JSON?

An example of what I mean is here : http://live.datatables.net/novezix/1/edit

This question has an accepted answers - jump to answer

Answers

  • RpiechuraRpiechura Posts: 98Questions: 3Answers: 14

    So I'll give you what I did to do this with the forewarning that it is written in 1.9.4 syntax so you'll have to do some translation to the new stuff. http://datatables.net/reference/option/ajax should help you with that.

    fnServerData: function (sSource, aoData, fnCallback, oSettings){
        oSettings.jqXHR = $.ajax({
            dataType: 'json',
            url: sSource,
            accepts: "application/json",
            headers: {
                 Accept: "application/json"
            },
            success: function (json) {
                // Code to execute when the ajax comes back.
            }
        }
    }
    

    fnServerData is an initialization option when creating the datatable, and according to the documentation found http://legacy.datatables.net/usage/callbacks it takes those parameters. I do a bit more prep work before the request that is application specific and I do a whole lot of application specific stuff when the request comes back, but hopefully that gives you an idea of where to start looking.

  • bodrickbodrick Posts: 3Questions: 1Answers: 0

    Unfortunately this isn't quite what I was looking for. If I already have data in my datatable, I would like to extract it and convert it to json so I can submit it as part of an existing request. From reading the docs you should be able to get this data from the data object on the table, which does indeed contain the data I'm looking for, but I can't seem to figure out how to get just the data and not the rest of the api along with it, JSON.stringify I assume is trying to convert everything and hence the circular reference part.

  • RpiechuraRpiechura Posts: 98Questions: 3Answers: 14
    edited June 2014

    I think you may be looking for http://datatables.net/reference/option/ajax.dataSrc. It's a way to tell the table to look at a specific object in the data instead of the default.

  • bodrickbodrick Posts: 3Questions: 1Answers: 0

    So I finally figured it out myself, to just get the data out of a datatable I just had to do a $.each on the rows().data() and append any items to my object that I'm passing to my ajax call.

  • allanallan Posts: 61,726Questions: 1Answers: 10,109 Site admin
    Answer ✓

    My issue is when I try to get the data from the table either by using DataTable().data() or DataTable().rows().data() when I try to convert this into JSON using JSON.stringify() I get a TypeError converting circular structure to JSON.

    I suspect the issue is that you are trying to JSON encode a DataTables API instance. What you probably want to do is call the toArray() method. Example: http://live.datatables.net/gowobin/1/edit

    Allan

This discussion has been closed.