How to get servserside processing to work

How to get servserside processing to work

tableguytableguy Posts: 5Questions: 3Answers: 0

I need pagination support with AJAX, so I came upon this page:

However, I cannot understand how it works. See the below link and snippet from same link:

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

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

That Javascript tells nothing of how it's sending the parameters to the server. Where are the page number, results per page, and column names for servserside ordering?

This question has an accepted answers - jump to answer

Answers

  • tangerinetangerine Posts: 3,365Questions: 39Answers: 395
    Answer ✓

    Server-side parameters are explained in the docs.
    https://datatables.net/manual/server-side

  • tableguytableguy Posts: 5Questions: 3Answers: 0
    edited April 2017

    Thanks for the response, tangerine. The key is { serverSide: true } so that it includes the parameters in the query string for GET or in the body for POST.

    The only obstacle at this point seems to be how the API requires knowledge of the columns prior to retrieving the data. I am going to post that as another question.

    Here's my code for anyone else who may benefit from it:

    var resultsTable = $('#results').DataTable({
        serverSide: true,
        ajax: {
            type: "GET",
            url: "@Url.Content("~/api/search/")" + getEntityType(),
            data: function (data) {
                //This code intercepts the parameters sent to
                //the server so I can return custom parameters
                //to match those of my service endpoint
                var page = data.Start / data.length;
                var params = {
                    query: getQuery(),
                    page: isNaN(page) ? 0 : page,
                    resultsPerPage: data.length
                };
                return params;
            },
            dataSrc: function (d) {
                //I attempt to set the columns dynamically here, but
                //it doesn't seem to work.  Will ask separate question
                resultsTable.columns = getColumnsFromJson(d.Results);
                return d.Results;
            }
        }
    });
    
This discussion has been closed.