JSON - accessing returned data other than 'data'

JSON - accessing returned data other than 'data'

needmorecoffeeneedmorecoffee Posts: 2Questions: 1Answers: 0

Hi - I have a table working well from a POST and JSON response.

var myTable=$('#table_id').DataTable({
"ajax":{
"url": "lookup-ajax.php",
"type": "POST",
"dataType": "json",
"data": function ( d ) {return $('#stats_search').serialize();}
},
"columns": [
    {"data": "age"},
    {"data": "height"},
    {"data": "weight"}
    ],
"order": [[ 1, "desc" ]],
"pagingType": "full_numbers",
"lengthMenu": [[50, 100, -1], [50, 100, "All"]]
});

The JSON is set out exactly as shown in your example Object data source

"columns" are used to access the "data" - but how can the other info returned be accessed such as "recordsTotal": 57 ?

This one is doing my head in?:-) Any suggestions welcome.

This question has accepted answers - jump to:

Answers

  • allanallan Posts: 63,535Questions: 1Answers: 10,475 Site admin
    Answer ✓

    recordsTotal and the like are only used when server-side processing is enabled.

    Allan

  • crwdzrcrwdzr Posts: 31Questions: 5Answers: 6
    Answer ✓

    if you really need to you could use mytable.ajax.json() and that should give you the entire json response filled with anytihng you needed.. adding it in should be pretty simple if you do server side scripting, just create new entries in your response array and fill them with whatever you want (tho you shouldn't really need that without server-side, like allan said)

  • needmorecoffeeneedmorecoffee Posts: 2Questions: 1Answers: 0

    Thanks for aiming me in the right direction. I did find a solution that works very well!

    We are searching a database of about 2Mil records, so we limit the results to 250, but still need to indicate to the client the total records available - we can now return this custom data as part of the JSON like follows - keep in mind I'm a self taught novice:-) So there is probably a better way, but this does work very well and very fast! It's amazing just how much faster these searches are without having to refresh the screen!

    var data = $('#myForm').serializeArray();
        $.ajax({
          type: "POST",
          url: "the-ajax.php",
          data: data,
          dataType: "json",
          success: function(json) {
            var rec_found = json.found_rec;
            var total_rec = json.total_rec;
            var av_price = json.av_price;
            $('#found_rec').html(rec_found);
            $('#total_rec').html(total_rec);
            $('#average').html(av_price)
            var myTable=$('#tablest_id').DataTable();
            myTable.destroy();
            var myTable=$('#tablest_id').DataTable({
            "data": json.data,
            "columns": [
            {"data": "id"},
            {"data": "the_item_name"},
            {"data": "sold_price"}
            ],
            "order": [[ 3, "desc" ]],
            "pagingType": "full_numbers",
            "lengthMenu": [[50, 100, -1], [50, 100, "All"]]
            }); 
         },
        error: function() {
            $('#error').html('Something has gone wrong..');
        }
        });
    
    
    Returned JSON from PHP:
     echo $result = '{"found_rec": "'.$found_rec.'", "total_rec": "'.number_format($total_rec).'", "av_price": "'.$av_price.'", "data": '.json_encode($result_array).'}';
    
    

    So happy with the results - I'm sending another contribution - just a little thanks for the great datatables resource!

This discussion has been closed.