0

Trying to pass some parameters in JQuery Datatables, its working fine on init. But when I try to refresh the datatable the values are received as empty on controller side. Here is the code:

 $("#SearchResultsTable").DataTable({
            "processing": true, 
            "serverSide": true, 
            "bLengthChange": false,
            "filter": false, 
            "pageLength": @Model.PageSize,
            "orderMulti": false, 
            "ajax": {
                "url": "/Request/SearchRequest",
                "type": "POST",
                "datatype": "json",
                "data": {
                    "Date_To": document.getElementById("Date_To").value,
                    "Date_From": document.getElementById("Date_From").value
                }
            },
            "columns": [
                { "data": "ABC", "name": "ABC", "autoWidth": true },
                { "data": "DEF", "name": "DEF", "autoWidth": true }
            ]
        }); 

Refresh Datatable:

$("#Search_Btn").click(function () {
            $("#SearchResultsTable").DataTable().clear();
            $("#SearchResultsTable").DataTable().draw();
        });

Also tried:

$("#Search_Btn").click(function () {
                var table = $('#SearchResultsTable').DataTable();
                table.ajax.reload();
            });

I would like to refresh the table in such a way the updated vales of 'Date_To' & 'Date_From' are available on controller side.

1
  • 1
    In your DataTable ajax option, instead of using data: { ... }, you can use a function: "data": function () {...}. There is an example in this answer. Since you are also using "serverSide": true, please note the warning in that answer, and use jQuery's extend. Commented May 23, 2021 at 22:18

1 Answer 1

1

You will need to pass the parameters via jquery datatable like below:

"data": function (data) {

           data.Date_To = document.getElementById("Date_To").value;
           data.Date_From = document.getElementById("Date_From").value;

        }

Please take a look at the following tutorial for more details

https://www.codeproject.com/Articles/1170086/Grid-with-Server-Side-Advanced-Search-using-JQuery

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.