How do I extract the datatables data, without the metadata (that apparently accompanies it) ?

How do I extract the datatables data, without the metadata (that apparently accompanies it) ?

sairnsairn Posts: 3Questions: 2Answers: 0

I want to post the contents of my datatable to the server with an ajax call.

To gather the datatable "data" for submission, I am using the function:

.rows().data()

The actual statement looks like this:

var page0griddata = JSON.stringify(page0grid.rows().data());

The contents of page0griddata looks like this (I have truncated it because the "metadata" portion is too long)...i.e.,

{"0":{"id":0,"fieldAStrg":"XXX0","fieldBStrg":"YYY0"},"1":{"id":1,"fieldAStrg":"XXX1","fieldBStrg":"YYY1"},"context":[{"oFeatures":{"bAutoWidth":true,"bDeferRender":false,"bFilter":false,"bInfo":false,"bLengthChange":false,"bPaginate":false,"bProcessing":false,"bServerSide":false,"bSort":false,"bSortMulti":true,"bSortClasses":true,"bStateSave":null},"oScroll":{"bCollapse":true,"iBarWidth":17,"sX":"","sXInner":"","sY":600},"oLanguage":{"fnInfoCallback":null,"oAria":{"sSortAscending":": activate to sort column ascending","sSortDescending":": activate to sort column descending","_hungarianMap":{"sortAscending":"sSortAscending","sortDescending":"sSortDescending"}},"oPaginate":{"sFirst":"First","sLast":"Last","sNext":"Next","sPrevious":"Previous","_hungarianMap":{"first":"sFirst","last":"sLast","next":"sNext","previous":"sPrevious"}},"sEmptyTable":"No data available in table","sInfo":"Showing _START_ to _END_ of _TOTAL_ entries","sInfoEmpty":...(REST IS TRUNCATED)...

The portion below (the actual data I am processing)... is all I really need...i.e.,

{"0":{"id":0,"fieldAStrg":"XXX0","fieldBStrg":"YYY0"},"1":{"id":1,"fieldAStrg":"XXX1","fieldBStrg":"YYY1"},

Is there a DataTables api method that will only return me this data? (aforementioned)?

FYI - in the event the datatable definition is at issue, below is the table definition...i.e.,

var page0grid = jq('#page0grid').DataTable({
    "ajax": $contextPath + "/page0/testGridList",
    "columns": [
        {"title": "ID",      "data": "id",          "visible": false    },          
        {"title": "Field A", "data": "fieldAStrg"                       },
        {"title": "Field B", "data": "fieldBStrg"                       }
    ],
    "columnDefs": [{
            "targets": 3,
            "data": null,
            "defaultContent": "<button class='edit'>Edit</button>"
        }],
    "info": false,
    "searching": false,
    "bPaginate": false,
    "scrollY": 600,
    "bLengthChange": false,
    "bScrollCollapse": true,
    "autoWidth": true,
    "order": [[ 0, 'desc' ]],
    "bSort": false
});

Thanks for your help!

  • please comment if my question is [still] not clear :-)

This question has an accepted answers - jump to answer

Answers

  • allanallan Posts: 61,716Questions: 1Answers: 10,108 Site admin
    Answer ✓

    Use the toArray() method. Currently you are JSON encoding a DataTables API instance. i.e.:

    var page0griddata = JSON.stringify(page0grid.rows().data().toArray());
    

    Allan

  • sairnsairn Posts: 3Questions: 2Answers: 0

    Thank you, Allan! I'd been trying on StackOverflow with no results.

This discussion has been closed.