php – How can I get data from a datatable?-ThrowExceptions

Exception or error:

Hi I have a serverside datatable wich it works witj jQuery and PHP, my datatable is this:

$('#datatable_selling_collection_report').DataTable({
    "processing": true,
    "serverSide": true,
    "searching" : false,
    "paging" : false,
    "language": {
        "sProcessing":     "Procesando...",
        "sLengthMenu":     "Mostrar _MENU_ registros",
        "sZeroRecords":    "No se encontraron resultados",
        "sEmptyTable":     "Ningún dato disponible en esta tabla",
        "sInfo":           "Mostrando registros del _START_ al _END_ de un total de _TOTAL_ registros",
        "sInfoEmpty":      "Mostrando registros del 0 al 0 de un total de 0 registros",
        "sInfoFiltered":   "(filtrado de un total de _MAX_ registros)",
        "sInfoPostFix":    "",
        "sSearch":         "Buscar: ",
        "sUrl":            "",
        "sInfoThousands":  ",",
        "sLoadingRecords": "Cargando...",
         "oPaginate": {
            "sFirst":    "Primero",
            "sLast":     "Último",
            "sNext":     "Siguiente",
            "sPrevious": "Anterior"
        },
        "oAria": {
            "sSortAscending":  ": Activar para ordenar la columna de manera ascendente",
            "sSortDescending": ": Activar para ordenar la columna de manera descendente"
        },
    },
    "ajax":{
         "url": "{{ url('kpi/sellingcollectionfetch') }}",
         "dataType": "json",
         "type": "POST",
         "data":{
             _token: "{{csrf_token()}}",
             supervisor_filter: supervisor_filter,
             branch_office_filter: branch_office_filter,
             month_filter: month_filter,
             year_filter: year_filter
        }
    },
    "columns": [
        { "data": "branch_office" },
        { "data": "income" },
        { "data": "average_ticket" },
        { "data": "sss" },
        { "data": "p_variation" },
        { "data": "q_variation" },
        { "data": "permanence" },
        { "data": "sellings_spots" }
    ]
});

And I know that datatable returns with PHP because this is serversite the data here:

$json_data = array(
    "draw"            => intval($request->input('draw')),  
    "recordsTotal"    => intval($totalData),  
    "recordsFiltered" => intval($totalFiltered), 
    "data"            => $data   
);

echo json_encode($json_data);

How can I put a variable in the json_data and then catch it in the datatable javascript. Thanks!

How to solve:

It depends on what you want to do with the data.

If you want to use it outside of jQuery DataTables, you can utilize xhr event and ajax.json() API method.

$('#datatable_selling_collection_report').on( 'xhr.dt', function () {
    var json = table.ajax.json();
    console.log('Data:', json);
});

If you need to use additional data when rendering each cell, third argument in a callback defined by columns.render option will allow you to access each row data.

Leave a Reply

Your email address will not be published. Required fields are marked *