Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Suppress unnecessary parameters in datatables ajax request?

I'm using the jquery datatables plugin with the ajax and serverSide options. The documentation specifies the fields that are included with each request. There are a lot of them. And most don't really add anything to my use case. Here's a short excerpt of the query string parameters from a request.

columns[0][data]:0
columns[0][name]:
columns[0][searchable]:true
columns[0][orderable]:true
columns[0][search][value]:
columns[0][search][regex]:false
columns[1][data]:1
columns[1][name]:
columns[1][searchable]:true
columns[1][orderable]:true
columns[1][search][value]:
columns[1][search][regex]:false

This goes on for every column in the table. This creates a very long query string when using 10 columns. In fact, I'm running into url length limits in my web server.

Is there any way to suppress certain properties from being included, or suppress properties that are empty or have a default value? The documentation doesn't have any obvious way to limit the size of the query string.

like image 658
recursive Avatar asked May 05 '15 19:05

recursive


2 Answers

I figured out a decent way to resolve this issue. Datatables provides a built-in method to mutate the ajax query parameters called just prior to making the request. You can specify it like this. In my case, I don't care about the entire columns array attribute, so I just remove it.

    var options = {
        sDom: "lftip", 
        /* set your options to suit your taste */
    };

    options.ajax = {
        url: ajaxUrl,
        data: function(data) {
            // manipulate data used in ajax request prior to server call
            delete data.columns;
        }
    };

    $el.dataTable(options);
like image 92
recursive Avatar answered Nov 04 '22 05:11

recursive


It's also possible to send the query via POST to avoid any URL length limits

$(document).ready(function() {
    $('#example').DataTable( {
        "processing": true,
        "serverSide": true,
        "ajax": {
            "url": "scripts/post.php",
            "type": "POST"
        }
    } );
} );

https://datatables.net/examples/server_side/post.html

like image 42
Moby Duck Avatar answered Nov 04 '22 06:11

Moby Duck