How do I get an extra parameter from AJAX with server-processing?

How do I get an extra parameter from AJAX with server-processing?

Albert83BCNAlbert83BCN Posts: 1Questions: 1Answers: 0
edited February 2015 in Free community support

Hi there!

First of all, thank you so much for creating such an amazing plugin! I'm new here, I've been playing with DataTables 1.10 for several weeks now (integrated with SmartAdmin bootstrap theme, that's how I got to know about DataTables) and managed to get almost everything I needed so far, but right now I'm struggling with getting a custom field from the server.

I'm trying to get a custom data field from the server request that I don't want to be part of the data rows, something like this in Laravel:

    $result = array
            (
                "draw" => $draw,
                "recordsTotal" => $count,
                "recordsFiltered" => $count,
                "totalTranslated" => 345, // test value, will be fetch from count(*)
                "data" => array()  // I fill this in a foreach bucle afterwards
            );

So far so good with the data, now I only need to show my custom metadata field it in the info section of the table. To do this, I found two examples in the documentation that gave me the idea of getting the ajax.json().data.totalTranslated value and put it in the info with $('.dataTables_info').append().

        var json = otable.ajax.json();
        alert( json.recordsTotal +' row(s) were loaded' );

I thought I'd just need to change the recordsTotal for my custom field. The problem is that I'd need to include the field in the response. To achieve this, I came up with a solution in another question in this forum:

    var otable = $('#datatable_fixed_column').DataTable({
         ...
        "ajax": function(data, callback, settings){
            $.post("{{ URL::asset('api/food/get/table') }}", data, function(response){
                callback({
                    recordsTotal: response.recordsTotal,
                    recordsFiltered: response.recordsFiltered,
                    // here I'd do something like totalTranslated : response.totalTranslated ?
                    data: response.data,
                });
            });
        },    
       ...

The problem is that if I do this, I don't know why but I cannot access the otable.ajax.json(), it is undefined. Up until otable.ajax an alert returns an object but not with the json() method. If I do not implement my custom ajax function and do only something like this:

          ajax" : {
           "type" : "POST",
               "url" : "{{ URL::asset('api/food/get/table') }}",
       } 

... then it works fine and I can access the ajax.json() correctly and I can see the correct alert message so I suspect there's something I'm missing when intercepting the ajax request, or could it be a bug? On both cases it does not interfere with the data response, as I can render the HTML table with no problem, it seems as if only getting the ajax.json() is not behaving correctly.

Any ideas? Thank you so much, any help will be really appreciated :)

This discussion has been closed.