Skip to content
Advertisement

No params on DataTable server rendering

I’m trying to use DataTables server rendering to show thousand of data in a view with this script:

$('#info-comisiones').DataTable({

   "processing": true,
   "serverside": true,
   ajax: {
     type: "POST",
     url: "/Sales/GetSalesByPaymentPeriodID",
     contentType: "application/json; charset=utf-8",
     dataType: "json",
     data: function (dtParms) {
       console.log(dtParms);
       return JSON.stringify(dtParms);
     },
     dataFilter: function (res) {
       var parsed = JSON.parse(res);
       return JSON.stringify(parsed.d);
     },
     error: function (x, y) {
       console.log(x);
     }
   },
   "filter": true,
   columns: [
     {"data":"#"},
     {"data":"F.Registro"},
     {"data":"Hora"},
     {"data":"Confirmación"},
     {"data":"Habitación"},
     {"data":"# Colaborador"},
     {"data":"Nombre Colaborador"},
     {"data":"Puesto"},
     {"data":"Centro Consumo"},
     {"data":"Misceláneo"},
     {"data":"Capturista"},
     {"data":"Puesto C."},
     {"data":"Fecha Salida"},
     {"data":"Descripción"},
     {"data":"Moneda"},
     {"data":"Acciones"},
     {"data":"Cantidad"},
     {"data":"T.Cambio"},
     {"data":"% Descuento"},
     {"data":"Impuestos"},
     {"data":"Precio Unitario MXN"},
     {"data":"Precio Unitario USD"},
     {"data":"Total Neto"},
     {"data":"% Comsión"},
     {"data":"Total Comision MXN"}
   ]
});

But the issue here is that in the line where I’m trying to see the data that is being sent to the server: console.log(dtParms); is showing an empty JSON object, and the server side is not receiving any data.

What could be the problem?

Advertisement

Answer

Here are some notes. I think item (1) may be the only problem, but I mention the others, just in case they are also useful.


  1. The dataFilter function in your Ajax call – why do you need that?

It throws away the response and only keeps parsed.d. I don’t know what parsed.d is supposed to be – but it’s probably empty or invalid.

You need to keep all of the response as it contains (or it should contain) all of the data needed by DataTables. That is not only the actual table data but also related metadata as described in the Returned Data section of the Server-Side manual.

I think you need to remove that dataFilter piece completely.

And make sure the response from the server matches the expected JSON response structure.


  1. What is "filter": true? That is not a valid DataTables option. It won’t cause any harm. It will just be ignored.

  1. The table’s columns settings. This may not actually be a problem, but it looks very unusual. You have column settings such as this:

    {“data”:”Nombre Colaborador”}

The value Nombre Colaborador should be a key in the data section of the JSON response – and maybe it is. But it’s more typical to see JSON keys such as nombreColaborador instead. Like I say, maybe this is OK.

I mention this because the JSON keys are usually generated from your server-side data model structures, where the fields you are using do not permit spaces in their names.


It’s possible that there may be more (or different) issues not shown in the question, and not covered in my answer. If so, then you may need to add a sample of your JSON to the question.

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement