Draw and Length Params Not Being Sent in Server Side Processing

Draw and Length Params Not Being Sent in Server Side Processing

sjmcartersjmcarter Posts: 5Questions: 2Answers: 0

I was initially having difficulty getting my table to refresh with new data form new parameters using ajax.reload(). The problem appeared to be that it was using the same parameters that I had set during initialization, and the fix was to make the ajax data parameter a function.

After doing this, I see that the updated parameters are being passed in the ajax call, however my query on the back-end is failing because draw and length are no longer being passed. According to the server side manual (https://datatables.net/manual/server-side), these are two params that DataTables itself will send out. However, it looks like that is not the case when data is made a function.

Will I have to manually set up the start,m length, and draw options, and then keep track of them, or is there something I am missing here?

Original code segment:

    var table = $('#example').DataTable({
        cache: false,
        scrollCollapse: false,
        paging: true,
        pagingType: 'simple_numbers',
        lengthMenu: [[10, 25, 50, 100, -1], [10, 25, 50, 100, 'All']],
        searching: false,
        info: false,
        processing: true,
        serverSide: true,
        ajax: {
            url: myURL,
            data: {
                campus: $("#campus").val(), 
                                college: $("#college").val(),  
                                year: $("#year").val() };
            }
        }
    });

Updated code segment:

    var table = $('#example').DataTable({
        cache: false,
        scrollCollapse: false,
        paging: true,
        pagingType: 'simple_numbers',
        lengthMenu: [[10, 25, 50, 100, -1], [10, 25, 50, 100, 'All']],
        searching: false,
        info: false,
        processing: true,
        serverSide: true,
        ajax: {
            url: myURL,
            data: function () {
                return {campus: $("#campus").val(), college: $("#college").val(), year: $("#year").val() };
            }
        }
    });

This question has an accepted answers - jump to answer

Answers

  • colincolin Posts: 15,240Questions: 1Answers: 2,599
    Answer ✓

    Hi @sjmcarter ,

    I suspect it's because your function is over-writing the data, not adding to it.

    Try,

    data: function () {
      d..campus = $("#campus").val();
      d.college = $("#college").val();
      d.year = $("#year").val() };
    }
    

    Cheers,

    Colin

  • sjmcartersjmcarter Posts: 5Questions: 2Answers: 0

    Thanks Colin. So is d the data object that gets sent in server side requests?

  • sjmcartersjmcarter Posts: 5Questions: 2Answers: 0
    edited November 2018

    Thanks, Colin. I think that was the piece I was missing!

This discussion has been closed.